-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcode Solutions.sql
More file actions
78 lines (45 loc) · 1.2 KB
/
Leetcode Solutions.sql
File metadata and controls
78 lines (45 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
-- 175. Combining two tables
SELECT p.firstName, p.lastName, a.city, a.state
FROM Person p
LEFT JOIN Address a
ON p.personId = a.personId
-- 181. Employees Earning More Than Their Managers
SELECT e1.name AS Employee
FROM Employee e1
LEFT JOIN Employee e2
#can be solved w/ INNER JOIN as well.
ON e1.managerId = e2.id
WHERE e1.salary > e2.salary
-- 182. Duplicate Emails
SELECT email as Email
FROM Person
GROUP BY email
HAVING COUNT(DISTINCT id) > 1
--Having is used instead of WHERE as the values are aggregate values
-- 183. Customers Who Never Order
SELECT c.name AS Customers
FROM Customers c
LEFT JOIN Orders o
ON c.id = o.customerId
WHERE o.id IS NULL
---------------alt way---------
SELECT c.name AS Customers
FROM Customers c
WHERE c.id NOT IN
(SELECT c.id
FROM Customers c
INNER JOIN Orders o
ON c.id = o.customerId)
-- 577. Employee Bonus
SELECT e.name, b.bonus
FROM Employee e
LEFT JOIN Bonus b
ON e.empId = b.empId
WHERE b.bonus < 1000
OR b.bonus IS NULL
-- 584. Find Customer Referee
SELECT name
FROM Customer
WHERE referee_id <> 2
-- <> is not equal to symbol in SQL
OR referee_id IS NULL