Skip to content

Commit a93a324

Browse files
authored
Added 5 solutions
1 parent e8fe855 commit a93a324

5 files changed

+162
-0
lines changed

Easy/Customers who never order.sql

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
-- Question 13
2+
-- Suppose that a website contains two tables,
3+
-- the Customers table and the Orders table. Write a SQL query to find all customers who never order anything.
4+
5+
-- Table: Customers.
6+
7+
-- +----+-------+
8+
-- | Id | Name |
9+
-- +----+-------+
10+
-- | 1 | Joe |
11+
-- | 2 | Henry |
12+
-- | 3 | Sam |
13+
-- | 4 | Max |
14+
-- +----+-------+
15+
-- Table: Orders.
16+
17+
-- +----+------------+
18+
-- | Id | CustomerId |
19+
-- +----+------------+
20+
-- | 1 | 3 |
21+
-- | 2 | 1 |
22+
-- +----+------------+
23+
-- Using the above tables as example, return the following:
24+
25+
-- +-----------+
26+
-- | Customers |
27+
-- +-----------+
28+
-- | Henry |
29+
-- | Max |
30+
-- +-----------+
31+
32+
33+
-- Solution
34+
Select Name as Customers
35+
from Customers
36+
where id != All(select c.id
37+
from Customers c, Orders o
38+
where c.id = o.Customerid)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
-- Question 14
2+
-- The Employee table holds all employees. Every employee has an Id, and there is also a column for the department Id.
3+
4+
-- +----+-------+--------+--------------+
5+
-- | Id | Name | Salary | DepartmentId |
6+
-- +----+-------+--------+--------------+
7+
-- | 1 | Joe | 85000 | 1 |
8+
-- | 2 | Henry | 80000 | 2 |
9+
-- | 3 | Sam | 60000 | 2 |
10+
-- | 4 | Max | 90000 | 1 |
11+
-- | 5 | Janet | 69000 | 1 |
12+
-- | 6 | Randy | 85000 | 1 |
13+
-- | 7 | Will | 70000 | 1 |
14+
-- +----+-------+--------+--------------+
15+
-- The Department table holds all departments of the company.
16+
17+
-- +----+----------+
18+
-- | Id | Name |
19+
-- +----+----------+
20+
-- | 1 | IT |
21+
-- | 2 | Sales |
22+
-- +----+----------+
23+
-- Write a SQL query to find employees who earn the top three salaries in each of the department. For the above tables, your SQL query should return the following rows (order of rows does not matter).
24+
25+
-- +------------+----------+--------+
26+
-- | Department | Employee | Salary |
27+
-- +------------+----------+--------+
28+
-- | IT | Max | 90000 |
29+
-- | IT | Randy | 85000 |
30+
-- | IT | Joe | 85000 |
31+
-- | IT | Will | 70000 |
32+
-- | Sales | Henry | 80000 |
33+
-- | Sales | Sam | 60000 |
34+
-- +------------+----------+--------+
35+
-- Explanation:
36+
37+
-- In IT department, Max earns the highest salary, both Randy and Joe earn the second highest salary,
38+
-- and Will earns the third highest salary.
39+
-- There are only two employees in the Sales department,
40+
-- Henry earns the highest salary while Sam earns the second highest salary.
41+
42+
-- Solution
43+
select a.department, a.employee, a.salary
44+
from (
45+
select d.name as department, e.name as employee, salary,
46+
dense_rank() over(Partition by d.name order by salary desc) as rk
47+
from Employee e join Department d
48+
on e.departmentid = d.id) a
49+
where a.rk<4

Easy/Duplicate Emails.sql

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
-- Question 11
2+
-- Write a SQL query to find all duplicate emails in a table named Person.
3+
4+
-- +----+---------+
5+
-- | Id | Email |
6+
-- +----+---------+
7+
8+
9+
10+
-- +----+---------+
11+
-- For example, your query should return the following for the above table:
12+
13+
-- +---------+
14+
-- | Email |
15+
-- +---------+
16+
17+
-- +---------+
18+
19+
20+
-- Solution
21+
Select Email
22+
from
23+
(Select Email, count(Email)
24+
from person
25+
group by Email
26+
having count(Email)>1) a
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
-- Question 15
2+
-- The Employee table holds all employees including their managers.
3+
-- Every employee has an Id, and there is also a column for the manager Id.
4+
5+
-- +----+-------+--------+-----------+
6+
-- | Id | Name | Salary | ManagerId |
7+
-- +----+-------+--------+-----------+
8+
-- | 1 | Joe | 70000 | 3 |
9+
-- | 2 | Henry | 80000 | 4 |
10+
-- | 3 | Sam | 60000 | NULL |
11+
-- | 4 | Max | 90000 | NULL |
12+
-- +----+-------+--------+-----------+
13+
-- Given the Employee table, write a SQL query that finds out employees who earn more than their managers.
14+
-- For the above table, Joe is the only employee who earns more than his manager.
15+
16+
-- +----------+
17+
-- | Employee |
18+
-- +----------+
19+
-- | Joe |
20+
-- +----------+
21+
22+
-- Solution
23+
select a.Name as Employee
24+
from employee a, employee b
25+
where a.salary>b.salary and a.managerid=b.id

Easy/Rising Temperature.sql

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
-- Question 12
2+
-- Given a Weather table, write a SQL query to find all dates' Ids with higher temperature compared to its previous (yesterday's) dates.
3+
4+
-- +---------+------------------+------------------+
5+
-- | Id(INT) | RecordDate(DATE) | Temperature(INT) |
6+
-- +---------+------------------+------------------+
7+
-- | 1 | 2015-01-01 | 10 |
8+
-- | 2 | 2015-01-02 | 25 |
9+
-- | 3 | 2015-01-03 | 20 |
10+
-- | 4 | 2015-01-04 | 30 |
11+
-- +---------+------------------+------------------+
12+
-- For example, return the following Ids for the above Weather table:
13+
14+
-- +----+
15+
-- | Id |
16+
-- +----+
17+
-- | 2 |
18+
-- | 4 |
19+
-- +----+
20+
21+
-- Solution
22+
select a.Id
23+
from weather a, weather b
24+
where a.Temperature>b.Temperature and datediff(a.recorddate,b.recorddate)=1

0 commit comments

Comments
 (0)