Skip to content

Commit c2bbdee

Browse files
Merge pull request ignacio-chiazzo#41 from ignacio-chiazzo/maxAreaOfIsland
Added basic SQL problems
2 parents c412f46 + 0097a80 commit c2bbdee

10 files changed

+444
-2
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Classes More Than 5 Students
3+
https://leetcode.com/problems/classes-more-than-5-students/
4+
5+
There is a table courses with columns: student and class
6+
7+
Please list out all classes which have more than or equal to 5 students.
8+
9+
For example, the table:
10+
11+
+---------+------------+
12+
| student | class |
13+
+---------+------------+
14+
| A | Math |
15+
| B | English |
16+
| C | Math |
17+
| D | Biology |
18+
| E | Math |
19+
| F | Computer |
20+
| G | Math |
21+
| H | Math |
22+
| I | Math |
23+
+---------+------------+
24+
Should output:
25+
26+
+---------+
27+
| class |
28+
+---------+
29+
| Math |
30+
+---------+
31+
32+
33+
Note:
34+
The students should not be counted duplicate in each course.
35+
*/
36+
37+
SELECT class
38+
FROM courses
39+
GROUP BY class
40+
HAVING count(DISTINCT(student)) > 4
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
Consecutive Numbers
3+
https://leetcode.com/problems/consecutive-numbers/
4+
5+
Write a SQL query to find all numbers that appear at least three times consecutively.
6+
7+
+----+-----+
8+
| Id | Num |
9+
+----+-----+
10+
| 1 | 1 |
11+
| 2 | 1 |
12+
| 3 | 1 |
13+
| 4 | 2 |
14+
| 5 | 1 |
15+
| 6 | 2 |
16+
| 7 | 2 |
17+
+----+-----+
18+
For example, given the above Logs table, 1 is the only number that appears consecutively for at least three times.
19+
20+
+-----------------+
21+
| ConsecutiveNums |
22+
+-----------------+
23+
| 1 |
24+
+-----------------+
25+
*/
26+
27+
SELECT DISTINCT(l1.Num) AS ConsecutiveNums
28+
FROM
29+
Logs l1, Logs l2, Logs l3
30+
WHERE (
31+
l1.Num = l2.Num AND l1.Id = (l2.Id + 1) AND
32+
l2.Num = l3.Num AND l2.Id = (l3.Id + 1)
33+
)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
Customers Who Never Order
3+
4+
SQL Schema
5+
Suppose that a website contains two tables, the Customers table and the Orders table. Write a SQL query to find all customers who never order anything.
6+
7+
Table: Customers.
8+
9+
+----+-------+
10+
| Id | Name |
11+
+----+-------+
12+
| 1 | Joe |
13+
| 2 | Henry |
14+
| 3 | Sam |
15+
| 4 | Max |
16+
+----+-------+
17+
Table: Orders.
18+
19+
+----+------------+
20+
| Id | CustomerId |
21+
+----+------------+
22+
| 1 | 3 |
23+
| 2 | 1 |
24+
+----+------------+
25+
Using the above tables as example, return the following:
26+
27+
+-----------+
28+
| Customers |
29+
+-----------+
30+
| Henry |
31+
| Max |
32+
+-----------+
33+
*/
34+
35+
SELECT Customers.Name as Customers
36+
FROM Customers LEFT JOIN Orders ON customers.id = Orders.CustomerId
37+
WHERE Orders.id IS NULL
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Delete Duplicate Emails
3+
https://leetcode.com/problems/delete-duplicate-emails/
4+
5+
Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based on its smallest Id.
6+
7+
+----+------------------+
8+
| Id | Email |
9+
+----+------------------+
10+
11+
12+
13+
+----+------------------+
14+
Id is the primary key column for this table.
15+
For example, after running your query, the above Person table should have the following rows:
16+
17+
+----+------------------+
18+
| Id | Email |
19+
+----+------------------+
20+
21+
22+
+----+------------------+
23+
Note:
24+
25+
Your output is the whole Person table after executing your sql. Use delete statement.
26+
*/
27+
28+
DELETE person FROM Person person, Person person2
29+
WHERE person.email = person2.email AND person.id > person2.id;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Department Highest Salary
3+
https://leetcode.com/problems/department-highest-salary/
4+
5+
The Employee table holds all employees. Every employee has an Id, a salary, and there is also a column for the department Id.
6+
7+
+----+-------+--------+--------------+
8+
| Id | Name | Salary | DepartmentId |
9+
+----+-------+--------+--------------+
10+
| 1 | Joe | 70000 | 1 |
11+
| 2 | Jim | 90000 | 1 |
12+
| 3 | Henry | 80000 | 2 |
13+
| 4 | Sam | 60000 | 2 |
14+
| 5 | Max | 90000 | 1 |
15+
+----+-------+--------+--------------+
16+
The Department table holds all departments of the company.
17+
18+
+----+----------+
19+
| Id | Name |
20+
+----+----------+
21+
| 1 | IT |
22+
| 2 | Sales |
23+
+----+----------+
24+
Write a SQL query to find employees who have the highest salary in each of the departments. For the above tables, your SQL query should return the following rows (order of rows does not matter).
25+
26+
+------------+----------+--------+
27+
| Department | Employee | Salary |
28+
+------------+----------+--------+
29+
| IT | Max | 90000 |
30+
| IT | Jim | 90000 |
31+
| Sales | Henry | 80000 |
32+
+------------+----------+--------+
33+
Explanation:
34+
35+
Max and Jim both have the highest salary in the IT department and Henry has the highest salary in the Sales department.
36+
*/
37+
38+
SELECT DepartmentName AS Department, Employee.Name as Employee, Employee.Salary
39+
FROM Employee JOIN (
40+
SELECT Department.Name AS DepartmentName, Department.Id AS DepartmentId, Max(Salary) as Salary
41+
FROM Employee JOIN Department ON Employee.DepartmentId = Department.Id
42+
GROUP BY Employee.DepartmentId
43+
) AS t ON Employee.Salary = t.Salary AND Employee.DepartmentId = t.DepartmentId
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Employees Earning More Than Their Managers
3+
4+
https://leetcode.com/problems/employees-earning-more-than-their-managers/
5+
6+
The Employee table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id.
7+
8+
+----+-------+--------+-----------+
9+
| Id | Name | Salary | ManagerId |
10+
+----+-------+--------+-----------+
11+
| 1 | Joe | 70000 | 3 |
12+
| 2 | Henry | 80000 | 4 |
13+
| 3 | Sam | 60000 | NULL |
14+
| 4 | Max | 90000 | NULL |
15+
+----+-------+--------+-----------+
16+
Given the Employee table, write a SQL query that finds out employees who earn more than their managers. For the above table, Joe is the only employee who earns more than his manager.
17+
18+
+----------+
19+
| Employee |
20+
+----------+
21+
| Joe |
22+
+----------+
23+
*/
24+
25+
SELECT e1.Name as "Employee"
26+
FROM Employee as e1 Join Employee as e2 ON e1.managerId = e2.id
27+
WHERE e1.Salary > e2.Salary
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Exchange Seats
3+
https://leetcode.com/problems/exchange-seats/
4+
5+
Mary is a teacher in a middle school and she has a table seat storing students' names and their corresponding seat ids.
6+
7+
The column id is continuous increment.
8+
9+
Mary wants to change seats for the adjacent students.
10+
Can you write a SQL query to output the result for Mary?
11+
12+
+---------+---------+
13+
| id | student |
14+
+---------+---------+
15+
| 1 | Abbot |
16+
| 2 | Doris |
17+
| 3 | Emerson |
18+
| 4 | Green |
19+
| 5 | Jeames |
20+
+---------+---------+
21+
For the sample input, the output is:
22+
23+
24+
+---------+---------+
25+
| id | student |
26+
+---------+---------+
27+
| 1 | Doris |
28+
| 2 | Abbot |
29+
| 3 | Green |
30+
| 4 | Emerson |
31+
| 5 | Jeames |
32+
+---------+---------+
33+
Note:
34+
If the number of students is odd, there is no need to change the last one's seat.
35+
*/
36+
37+
WITH paresAImpares AS (
38+
SELECT (id - 1) AS id, student
39+
FROM seat
40+
WHERE MOD(id, 2) = 0
41+
), imparesAPares AS (
42+
SELECT (id + 1) as id, student
43+
FROM seat
44+
WHERE MOD(id, 2) = 1
45+
)
46+
47+
SELECT id, coalesce(paresAImpares.student, imparesAPares.student, seat.student) AS student
48+
FROM seat LEFT JOIN paresAImpares USING(id) LEFT JOIN imparesAPares using(id)
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
Reformat Department Table
3+
https://leetcode.com/problems/reformat-department-table
4+
5+
Table: Department
6+
7+
+---------------+---------+
8+
| Column Name | Type |
9+
+---------------+---------+
10+
| id | int |
11+
| revenue | int |
12+
| month | varchar |
13+
+---------------+---------+
14+
(id, month) is the primary key of this table.
15+
The table has information about the revenue of each department per month.
16+
The month has values in ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"].
17+
18+
19+
Write an SQL query to reformat the table such that there is a department id column and a revenue column for each month.
20+
21+
The query result format is in the following example:
22+
23+
Department table:
24+
+------+---------+-------+
25+
| id | revenue | month |
26+
+------+---------+-------+
27+
| 1 | 8000 | Jan |
28+
| 2 | 9000 | Jan |
29+
| 3 | 10000 | Feb |
30+
| 1 | 7000 | Feb |
31+
| 1 | 6000 | Mar |
32+
+------+---------+-------+
33+
34+
Result table:
35+
+------+-------------+-------------+-------------+-----+-------------+
36+
| id | Jan_Revenue | Feb_Revenue | Mar_Revenue | ... | Dec_Revenue |
37+
+------+-------------+-------------+-------------+-----+-------------+
38+
| 1 | 8000 | 7000 | 6000 | ... | null |
39+
| 2 | 9000 | null | null | ... | null |
40+
| 3 | null | 10000 | null | ... | null |
41+
+------+-------------+-------------+-------------+-----+-------------+
42+
43+
Note that the result table has 13 columns (1 for the department id + 12 for the months).
44+
*/
45+
46+
47+
48+
49+
-- better solution
50+
SELECT id,
51+
sum(CASE WHEN month = "Jan" then revenue else NULL END) AS "Jan_Revenue",
52+
sum(CASE WHEN month = "Feb" then revenue else NULL END) AS "Feb_Revenue",
53+
sum(CASE WHEN month = "Mar" then revenue else NULL END) AS "Mar_Revenue",
54+
sum(CASE WHEN month = "Apr" then revenue else NULL END) AS "Apr_Revenue",
55+
sum(CASE WHEN month = "May" then revenue else NULL END) AS "May_Revenue",
56+
sum(CASE WHEN month = "Jun" then revenue else NULL END) AS "Jun_Revenue",
57+
sum(CASE WHEN month = "Jul" then revenue else NULL END) AS "Jul_Revenue",
58+
sum(CASE WHEN month = "Aug" then revenue else NULL END) AS "Aug_Revenue",
59+
sum(CASE WHEN month = "Sep" then revenue else NULL END) AS "Sep_Revenue",
60+
sum(CASE WHEN month = "Oct" then revenue else NULL END) AS "Oct_Revenue",
61+
sum(CASE WHEN month = "Nov" then revenue else NULL END) AS "Nov_Revenue",
62+
sum(CASE WHEN month = "Dec" then revenue else NULL END) AS "Dec_Revenue"
63+
FROM Department
64+
GROUP BY id
65+
66+
67+
-- bad solution
68+
with jan AS (
69+
SELECT id, revenue AS Jan_Revenue
70+
FROM Department
71+
WHERE month = "Jan"
72+
), feb AS (
73+
SELECT id, revenue AS Feb_Revenue
74+
FROM Department
75+
WHERE month = "Feb"
76+
), mar AS (
77+
SELECT id, revenue AS Mar_Revenue
78+
FROM Department
79+
WHERE month = "Mar"
80+
), apr AS (
81+
SELECT id, revenue AS Apr_Revenue
82+
FROM Department
83+
WHERE month = "Apr"
84+
), may AS (
85+
SELECT id, revenue AS May_Revenue
86+
FROM Department
87+
WHERE month = "May"
88+
), jun AS (
89+
SELECT id, revenue AS Jun_Revenue
90+
FROM Department
91+
WHERE month = "Jun"
92+
), jul AS (
93+
SELECT id, revenue AS Jul_Revenue
94+
FROM Department
95+
WHERE month = "Jul"
96+
), aug AS (
97+
SELECT id, revenue AS Aug_Revenue
98+
FROM Department
99+
WHERE month = "Aug"
100+
), sep AS (
101+
SELECT id, revenue AS Sep_Revenue
102+
FROM Department
103+
WHERE month = "Sep"
104+
), oct AS (
105+
SELECT id, revenue AS Oct_Revenue
106+
FROM Department
107+
WHERE month = "Oct"
108+
), nov AS (
109+
SELECT id, revenue AS Nov_Revenue
110+
FROM Department
111+
WHERE month = "Nov"
112+
), decemb AS (
113+
SELECT id, revenue AS Dec_Revenue
114+
FROM Department
115+
WHERE month = "Dec"
116+
)
117+
118+
SELECT Distinct(Department.id), jan.Jan_Revenue, feb.Feb_Revenue, mar.Mar_Revenue, apr.Apr_Revenue, may.May_Revenue, jun.Jun_Revenue, jul.Jul_Revenue, aug.Aug_Revenue, sep.Sep_Revenue, oct.Oct_Revenue, nov.Nov_Revenue, decemb.Dec_Revenue
119+
FROM Department LEFT JOIN jan using(id)
120+
LEFT JOIN feb using(id)
121+
LEFT JOIN mar using(id)
122+
LEFT JOIN apr using(id)
123+
LEFT JOIN may using(id)
124+
LEFT JOIN jun using(id)
125+
LEFT JOIN jul using(id)
126+
LEFT JOIN aug using(id)
127+
LEFT JOIN sep using(id)
128+
LEFT JOIN oct using(id)
129+
LEFT JOIN nov using(id)
130+
LEFT JOIN decemb using(id)
131+

0 commit comments

Comments
 (0)