├── Convoy_lane.sql ├── HBO_Count_Time_Duration.sql ├── L 196 Delete Duplicated Emails.sql ├── L 197 Rising Temperature.md ├── L 262 Trips and Users.md ├── L 569 Median Employee Salary.md ├── L 570 Managers with at least 5 Direct Reports.md ├── L 571 Find Median Given Frequency of Numbers.md ├── L 574 Winning Candidate.md ├── L 577 Employee Bonus.md ├── L 578 Get Highest Answer Rate.sql ├── L 596 Classes More Than 5 Students.md ├── L 601 Human Traffic of Stadium.sql ├── L 620 Not Boring Movies.md ├── L 626 Exchange Seats.md ├── L 627 Swap Salary.md ├── L1083_Sales_Analysis.sql ├── L1107_New Users Daily Count.sql ├── L1164_Product Price at a Given Date.sql ├── L1179_ Reformat Department Table.sql ├── L1204_Last Person to Fit in the Elevator.sql ├── L1212_Team_Scores_in_Football_Tournament.sql ├── L1264_Page Recommendations.sql ├── L1321_Restaurant Growth.sql ├── L175 Combine Two Tables.md ├── L176 Second Highest Salary.md ├── L178 Rank Scores.sql ├── L180 Consecutive Numbers.md ├── L181 Emplyee Earning More Than Their Manager.md ├── L182 Duplicate Emails.md ├── L183 Customers Who Never Order.md ├── L184 Department Highest Salary.md ├── L185 Department Top Three Salaries.sql ├── L534_Game_Play_Analysis III.sql ├── L550_Game Play Analysis IV.sql ├── L579_Find Cumulative Salary of an Employee.sql ├── L580_Count Student Number in Departments.sql ├── L584_Find_Customer_Referee.sql ├── L585_Investments in 2016.sql ├── L597_Friend Requests I: Overall Acceptance Rate.sql ├── L602_Friend Requests II: Who Has the Most FriendsII.py ├── L603_Consecutive_Available_Seats.sql ├── L607_Sales_Person.sql ├── L608_Tree_Node.sql ├── L610_Triangle_Judgement.sql ├── L612_Shortest Distance in a Plane.sql ├── L613_Shortest Distance in a Line .sql ├── L614_Second_Degree_Follower.sql ├── L615_Average Salary: Departments VS Company.sql ├── L618_Students Report By Geography.sql ├── L619_Biggest_Single_Number.sql ├── README.md ├── SQL_Functions ├── Date_function.sql ├── Delete_Duplicated_Two_Cases.sql ├── Different_Joins_Definition ├── Row_Number().sql ├── SQL_Rank_Function_Example.sql ├── Substring_CharIndex_use.sql ├── Union_VS_Union_All ├── With_AS_Syntax.sql └── window_function_aggregation_cumulation.sql ├── Second_High_Salary_In_Each_Dept.sql ├── Wayfair_Company_Export_Import.sql └── replace_null_value_with_value_previous_value.sql /Convoy_lane.sql: -------------------------------------------------------------------------------- 1 | Find in Feb, (a->b,b->a) count 1, calculate count(shipmentID) happened. 2 | 3 | +------------+-----------+------------+--------------+---------------+ 4 | | shipmentID | shipperID | date_time | pickup_state | dropoff_state | 5 | +------------+-----------+------------+--------------+---------------+ 6 | | 1 | A3 | 2019-02-01 | A | B | 7 | | 2 | B3 | 2019-02-01 | B | A | 8 | | 3 | A5 | 2019-02-01 | A | C | 9 | | 4 | A6 | 2019-02-02 | C | D | 10 | | 5 | A7 | 2019-02-03 | D | C | 11 | | 6 | C3 | 2019-03-01 | A | E | 12 | +------------+-----------+------------+--------------+---------------+ 13 | 14 | Result: 15 | +-------+------+-----------------+ 16 | | month | lane | num_of_shipment | 17 | +-------+------+-----------------+ 18 | | 2 | A-B | 2 | 19 | | 2 | A-C | 1 | 20 | | 2 | C-D | 2 | 21 | +-------+------+-----------------+ 22 | 23 | 24 | /*drop table shipments; 25 | 26 | create table shipments (shipmentID int, shipperID Varchar(10), date_time date, pickup_state varchar(10), dropoff_state varchar(10)); 27 | 28 | insert into shipments values(1,'A3','2019-02-01','A','B'); 29 | insert into shipments values(2,'B3','2019-02-01','B','A'); 30 | insert into shipments values(3,'A5','2019-02-01','A','C'); 31 | insert into shipments values(4,'A6','2019-02-02','C','D'); 32 | insert into shipments values(5,'A7','2019-02-03','D','C'); 33 | insert into shipments values(6,'C3','2019-03-01','A','E'); 34 | */ 35 | 36 | select * from shipments; 37 | 38 | 39 | select month,concat(lane1,"-",lane2) as lane, num_of_shipment 40 | from 41 | ( 42 | select month(date_time) as month, 43 | case when tb1.pickup_statep2.Id; 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /L 197 Rising Temperature.md: -------------------------------------------------------------------------------- 1 | Given a Weather table, write a SQL query to find all dates' Ids with higher temperature compared to its previous (yesterday's) dates. 2 | 3 | +---------+------------------+------------------+ 4 | | Id(INT) | RecordDate(DATE) | Temperature(INT) | 5 | +---------+------------------+------------------+ 6 | | 1 | 2015-01-01 | 10 | 7 | | 2 | 2015-01-02 | 25 | 8 | | 3 | 2015-01-03 | 20 | 9 | | 4 | 2015-01-04 | 30 | 10 | +---------+------------------+------------------+ 11 | For example, return the following Ids for the above Weather table: 12 | 13 | +----+ 14 | | Id | 15 | +----+ 16 | | 2 | 17 | | 4 | 18 | +----+ 19 | 20 | - solution: 21 | 22 | <1> Regular method: 23 | 24 | SELECT wt1.Id 25 | FROM Weather wt1, Weather wt2 26 | WHERE wt1.Temperature > wt2.Temperature 27 | AND 28 | TO_DAYS(wt1.RecordDate)-TO_DAYS(wt2.RecordDate)=1; 29 | 30 | 31 | <2> Join & DATEDIFF() <*> 32 | 33 | SELECT tb1.Id as Id 34 | FROM Weather tb1 35 | INNER JOIN Weather tb2 36 | ON tb1.Temperature > tb2.Temperature AND DATEDIFF(tb1.RecordDate,tb2.RecordDate)=1; 37 | 38 | <3> Analytic functions: 39 | 40 | select t.Id 41 | from 42 | (select Id, Temperature, 43 | lag(Temperature,1,null) over (order by RecordDate) as previous_temperature, 44 | RecordDate, 45 | lag(RecordDate,1,null) over (order by RecordDate) as previous_date, 46 | DATEDIFF(day, lag(RecordDate,1,null) over (order by RecordDate), RecordDate) as gap 47 | from Weather) t 48 | where t.Temperature>t.previous_temperature and 49 | t.gap=1 50 | 51 | Thank you 52 | -------------------------------------------------------------------------------- /L 262 Trips and Users.md: -------------------------------------------------------------------------------- 1 | The Trips table holds all taxi trips. Each trip has a unique Id, while Client_Id and Driver_Id are both foreign keys to the Users_Id at the Users table. Status is an ENUM type of (‘completed’, ‘cancelled_by_driver’, ‘cancelled_by_client’). 2 | 3 | +----+-----------+-----------+---------+--------------------+----------+ 4 | | Id | Client_Id | Driver_Id | City_Id | Status |Request_at| 5 | +----+-----------+-----------+---------+--------------------+----------+ 6 | | 1 | 1 | 10 | 1 | completed |2013-10-01| 7 | | 2 | 2 | 11 | 1 | cancelled_by_driver|2013-10-01| 8 | | 3 | 3 | 12 | 6 | completed |2013-10-01| 9 | | 4 | 4 | 13 | 6 | cancelled_by_client|2013-10-01| 10 | | 5 | 1 | 10 | 1 | completed |2013-10-02| 11 | | 6 | 2 | 11 | 6 | completed |2013-10-02| 12 | | 7 | 3 | 12 | 6 | completed |2013-10-02| 13 | | 8 | 2 | 12 | 12 | completed |2013-10-03| 14 | | 9 | 3 | 10 | 12 | completed |2013-10-03| 15 | | 10 | 4 | 13 | 12 | cancelled_by_driver|2013-10-03| 16 | +----+-----------+-----------+---------+--------------------+----------+ 17 | The Users table holds all users. Each user has an unique Users_Id, and Role is an ENUM type of (‘client’, ‘driver’, ‘partner’). 18 | 19 | +----------+--------+--------+ 20 | | Users_Id | Banned | Role | 21 | +----------+--------+--------+ 22 | | 1 | No | client | 23 | | 2 | Yes | client | 24 | | 3 | No | client | 25 | | 4 | No | client | 26 | | 10 | No | driver | 27 | | 11 | No | driver | 28 | | 12 | No | driver | 29 | | 13 | No | driver | 30 | +----------+--------+--------+ 31 | Write a SQL query to find the cancellation rate of requests made by unbanned users between Oct 1, 2013 and Oct 3, 2013. For the above tables, your SQL query should return the following rows with the cancellation rate being rounded to two decimal places. 32 | 33 | +------------+-------------------+ 34 | | Day | Cancellation Rate | 35 | +------------+-------------------+ 36 | | 2013-10-01 | 0.33 | 37 | | 2013-10-02 | 0.00 | 38 | | 2013-10-03 | 0.50 | 39 | +------------+-------------------+ 40 | 41 | 42 | - solution: 43 | 44 | select Trips.request_at as Day, 45 | Round(Count(case when Trips.Status = "completed" then null else 1 end)/count(*), 2) as "Cancellation Rate" 46 | from Users join Trips 47 | on Users.Users_id = Trips.Client_id 48 | where Users.Banned = "No" 49 | and 50 | Trips.request_at between "2013-10-01" and "2013-10-03" 51 | group by Trips.request_at; -------------------------------------------------------------------------------- /L 569 Median Employee Salary.md: -------------------------------------------------------------------------------- 1 | The Employee table holds all employees. The employee table has three columns: Employee Id, Company Name, and Salary. 2 | 3 | +-----+------------+--------+ 4 | |Id | Company | Salary | 5 | +-----+------------+--------+ 6 | |1 | A | 2341 | 7 | |2 | A | 341 | 8 | |3 | A | 15 | 9 | |4 | A | 15314 | 10 | |5 | A | 451 | 11 | |6 | A | 513 | 12 | |7 | B | 15 | 13 | |8 | B | 13 | 14 | |9 | B | 1154 | 15 | |10 | B | 1345 | 16 | |11 | B | 1221 | 17 | |12 | B | 234 | 18 | |13 | C | 2345 | 19 | |14 | C | 2645 | 20 | |15 | C | 2645 | 21 | |16 | C | 2652 | 22 | |17 | C | 65 | 23 | +-----+------------+--------+ 24 | Write a SQL query to find the median salary of each company. Bonus points if you can solve it without using any built-in SQL functions. 25 | 26 | +-----+------------+--------+ 27 | |Id | Company | Salary | 28 | +-----+------------+--------+ 29 | |5 | A | 451 | 30 | |6 | A | 513 | 31 | |12 | B | 234 | 32 | |9 | B | 1154 | 33 | |14 | C | 2645 | 34 | +-----+------------+--------+ 35 | 36 | - solution: 37 | 38 | <* not use built in function> 39 | 40 | SELECT tb1.Id,tb1.Company,tb1.Salary 41 | FROM Employee as tb1 42 | LEFT JOIN Employee as tb2 43 | on tb1.Company=tb2.Company 44 | GROUP BY tb1.Company,tb1.Salary 45 | Having SUM(CASE WHEN tb1.Salary=tb2.Salary THEN 1 ELSE 0 END) 46 | >= ABS(SUM(CASE WHEN tb1.Salary>tb2.Salary then 1 when tb1.Salary median number is 3 and 4 50 | 3 appears one time, 4 appears one time 51 | greater than 3 has 3 numbers sum()=>3 smaller than 3 has 2 numbers sum()=> -2 total =1 thus 1>=abs(1) 52 | greater than 4 has 2 numbers sum()=>2 smaller than 4 has 3 numbers sum()=> -3 total =1 thus 1>=abs(1) 53 | 54 | 55 | <1> 56 | 57 | select t.Id as Id, 58 | t.Company as Company, 59 | t.Salary as Salary 60 | from ( 61 | select Id, 62 | Company, 63 | Salary, 64 | median(Salary) over (partition by Company order by Salary) as Median_Salary 65 | from employee) t 66 | where t.Salary = t.Median_Salary; 67 | 68 | 69 | <2> 70 | 71 | 72 | with cte as( 73 | select Id, 74 | Company, 75 | Salary 76 | ,ROW_NUMBER() over(partition by Company order by Salary) as rnk, 77 | count(*) over(partition by Company) as cnt 78 | from employees 79 | ) select Id,Company,Salary from cte 80 | where rnk in (CEILING(cnt/2.0),cnt/2+1) 81 | -------------------------------------------------------------------------------- /L 570 Managers with at least 5 Direct Reports.md: -------------------------------------------------------------------------------- 1 | The Employee table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id. 2 | 3 | +------+----------+-----------+----------+ 4 | |Id |Name |Department |ManagerId | 5 | +------+----------+-----------+----------+ 6 | |101 |John |A |null | 7 | |102 |Dan |A |101 | 8 | |103 |James |A |101 | 9 | |104 |Amy |A |101 | 10 | |105 |Anne |A |101 | 11 | |106 |Ron |B |101 | 12 | +------+----------+-----------+----------+ 13 | Given the Employee table, write a SQL query that finds out managers with at least 5 direct report. For the above table, your SQL query should return: 14 | 15 | +-------+ 16 | | Name | 17 | +-------+ 18 | | John | 19 | +-------+ 20 | 21 | - solution: 22 | 23 | select Name 24 | from Employee t1 25 | join 26 | (select ManagerId, 27 | from 28 | Employee 29 | group by ManagerId 30 | having count(ManagerId) >= 5) AS t2 31 | on t1.Id=t2.ManagerId -------------------------------------------------------------------------------- /L 571 Find Median Given Frequency of Numbers.md: -------------------------------------------------------------------------------- 1 | The Numbers table keeps the value of number and its frequency. 2 | 3 | +----------+-------------+ 4 | | Number | Frequency | 5 | +----------+-------------| 6 | | 0 | 7 | 7 | | 1 | 1 | 8 | | 2 | 3 | 9 | | 3 | 1 | 10 | +----------+-------------+ 11 | In this table, the numbers are 0, 0, 0, 0, 0, 0, 0, 1, 2, 2, 2, 3, so the median is (0 + 0) / 2 = 0. 12 | 13 | +--------+ 14 | | median | 15 | +--------| 16 | | 0.0000 | 17 | +--------+ 18 | 19 | - solution: 20 | 21 | select avg(t3.Number) as median 22 | from Numbers as t3 23 | inner join 24 | (select t1.Number, 25 | abs(sum(case when t1.Number>t2.Number then t2.Frequency else 0 end) - 26 | sum(case when t1.Number=t4.count_diff -------------------------------------------------------------------------------- /L 574 Winning Candidate.md: -------------------------------------------------------------------------------- 1 | Table: Candidate 2 | 3 | +-----+---------+ 4 | | id | Name | 5 | +-----+---------+ 6 | | 1 | A | 7 | | 2 | B | 8 | | 3 | C | 9 | | 4 | D | 10 | | 5 | E | 11 | +-----+---------+ 12 | Table: Vote 13 | 14 | +-----+--------------+ 15 | | id | CandidateId | 16 | +-----+--------------+ 17 | | 1 | 2 | 18 | | 2 | 4 | 19 | | 3 | 3 | 20 | | 4 | 2 | 21 | | 5 | 5 | 22 | +-----+--------------+ 23 | id is the auto-increment primary key, 24 | CandidateId is the id appeared in Candidate table. 25 | Write a sql to find the name of the winning candidate, the above example will return the winner B. 26 | 27 | +------+ 28 | | Name | 29 | +------+ 30 | | B | 31 | +------+ 32 | Notes: 33 | You may assume there is no tie, in other words there will be at most one winning candidate. 34 | 35 | - solution: 36 | 37 | select name as Name 38 | from Candidate 39 | join 40 | (select CandidateId, 41 | from Vote 42 | group by CandidateId 43 | order by count(*) desc 44 | limit 1) as winner 45 | on Candidate.id=Winner.CandidateId -------------------------------------------------------------------------------- /L 577 Employee Bonus.md: -------------------------------------------------------------------------------- 1 | Select all employee's name and bonus whose bonus is < 1000. 2 | 3 | Table:Employee 4 | 5 | +-------+--------+-----------+--------+ 6 | | empId | name | supervisor| salary | 7 | +-------+--------+-----------+--------+ 8 | | 1 | John | 3 | 1000 | 9 | | 2 | Dan | 3 | 2000 | 10 | | 3 | Brad | null | 4000 | 11 | | 4 | Thomas | 3 | 4000 | 12 | +-------+--------+-----------+--------+ 13 | empId is the primary key column for this table. 14 | Table: Bonus 15 | 16 | +-------+-------+ 17 | | empId | bonus | 18 | +-------+-------+ 19 | | 2 | 500 | 20 | | 4 | 2000 | 21 | +-------+-------+ 22 | empId is the primary key column for this table. 23 | Example ouput: 24 | 25 | +-------+-------+ 26 | | name | bonus | 27 | +-------+-------+ 28 | | John | null | 29 | | Dan | 500 | 30 | | Brad | null | 31 | +-------+-------+ 32 | 33 | - solution: 34 | 35 | select t1.name, 36 | t2.bonus 37 | from Employee t1 left join 38 | Bonus t2 39 | on t1.empId=t2.empId 40 | where t2.bonus<1000 or t2.bonus is null -------------------------------------------------------------------------------- /L 578 Get Highest Answer Rate.sql: -------------------------------------------------------------------------------- 1 | Get the highest answer rate question from a table survey_log with these columns: uid, action, question_id, answer_id, q_num, timestamp. 2 | 3 | uid means user id; action has these kind of values: "show", "answer", "skip"; answer_id is not null when action column is "answer", while is null for "show" and "skip"; q_num is the numeral order of the question in current session. 4 | 5 | Write a sql query to identify the question which has the highest answer rate. 6 | 7 | Example: 8 | Input: 9 | 10 | +------+-----------+--------------+------------+-----------+------------+ 11 | | uid | action | question_id | answer_id | q_num | timestamp | 12 | +------+-----------+--------------+------------+-----------+------------+ 13 | | 5 | show | 285 | null | 1 | 123 | 14 | | 5 | answer | 285 | 124124 | 1 | 124 | 15 | | 5 | show | 369 | null | 2 | 125 | 16 | | 5 | skip | 369 | null | 2 | 126 | 17 | +------+-----------+--------------+------------+-----------+------------+ 18 | Output: 19 | 20 | +-------------+ 21 | | survey_log | 22 | +-------------+ 23 | | 285 | 24 | +-------------+ 25 | Explanation: 26 | question 285 has answer rate 1/1, while question 369 has 0/1 answer rate, so output 285. 27 | 28 | 29 | - solution: 30 | 31 | select question_id as survey_log 32 | from 33 | (select question_id, 34 | sum(case when action='answer' then 1 else 0 end) as num_answer, 35 | sum(case when action='show' then 1 else 0 end) as num_show 36 | from survey_log 37 | group by question_id) as t 38 | order by (num_answer/num_show) desc 39 | limit 1 40 | -------------------------------------------------------------------------------- /L 596 Classes More Than 5 Students.md: -------------------------------------------------------------------------------- 1 | There is a table courses with columns: student and class 2 | 3 | Please list out all classes which have more than or equal to 5 students. 4 | 5 | For example, the table: 6 | 7 | +---------+------------+ 8 | | student | class | 9 | +---------+------------+ 10 | | A | Math | 11 | | B | English | 12 | | C | Math | 13 | | D | Biology | 14 | | E | Math | 15 | | F | Computer | 16 | | G | Math | 17 | | H | Math | 18 | | I | Math | 19 | +---------+------------+ 20 | Should output: 21 | 22 | +---------+ 23 | | class | 24 | +---------+ 25 | | Math | 26 | +---------+ 27 | Note: 28 | The students should not be counted duplicate in each course. 29 | 30 | - solution: 31 | 32 | select class from courses 33 | group by class 34 | having count(distinct(student)) >=5; -------------------------------------------------------------------------------- /L 601 Human Traffic of Stadium.sql: -------------------------------------------------------------------------------- 1 | X city built a new stadium, each day many people visit it and the stats are saved as these columns: id, date, people 2 | 3 | Please write a query to display the records which have 3 or more consecutive rows and the amount of people more than 100(inclusive). 4 | 5 | For example, the table stadium: 6 | 7 | +------+------------+-----------+ 8 | | id | date | people | 9 | +------+------------+-----------+ 10 | | 1 | 2017-01-01 | 10 | 11 | | 2 | 2017-01-02 | 109 | 12 | | 3 | 2017-01-03 | 150 | 13 | | 4 | 2017-01-04 | 99 | 14 | | 5 | 2017-01-05 | 145 | 15 | | 6 | 2017-01-06 | 1455 | 16 | | 7 | 2017-01-07 | 199 | 17 | | 8 | 2017-01-08 | 188 | 18 | +------+------------+-----------+ 19 | For the sample data above, the output is: 20 | 21 | 22 | +------+------------+-----------+ 23 | | id | date | people | 24 | +------+------------+-----------+ 25 | | 5 | 2017-01-05 | 145 | 26 | | 6 | 2017-01-06 | 1455 | 27 | | 7 | 2017-01-07 | 199 | 28 | | 8 | 2017-01-08 | 188 | 29 | +------+------------+-----------+ 30 | 31 | 32 | - solution: 33 | 34 | select distinct s1.id, s1.visit_date, s1.people 35 | from stadium as s1, stadium as s2, 36 | stadium as s3 37 | where ((s1.id+1=s2.id and s1.id+2=s3.id) 38 | or (s1.id-1=s2.id and s1.id+1=s3.id) or 39 | (s1.id-1=s2.id and s1.id-2=s3.id)) 40 | and s1.people>=100 41 | and s2.people>=100 42 | and s3.people>=100 43 | ORDER by s1.id 44 | -------------------------------------------------------------------------------- /L 620 Not Boring Movies.md: -------------------------------------------------------------------------------- 1 | Please write a SQL query to output movies with an odd numbered ID and a description that is not 'boring'. Order the result by rating. 2 | 3 | For example, table cinema: 4 | 5 | +---------+-----------+--------------+-----------+ 6 | | id | movie | description | rating | 7 | +---------+-----------+--------------+-----------+ 8 | | 1 | War | great 3D | 8.9 | 9 | | 2 | Science | fiction | 8.5 | 10 | | 3 | irish | boring | 6.2 | 11 | | 4 | Ice song | Fantacy | 8.6 | 12 | | 5 | House card| Interesting| 9.1 | 13 | +---------+-----------+--------------+-----------+ 14 | 15 | For the example above, the output should be: 16 | 17 | +---------+-----------+--------------+-----------+ 18 | | id | movie | description | rating | 19 | +---------+-----------+--------------+-----------+ 20 | | 5 | House card| Interesting| 9.1 | 21 | | 1 | War | great 3D | 8.9 | 22 | +---------+-----------+--------------+-----------+ 23 | 24 | - solution: 25 | 26 | select * 27 | from cinema 28 | where id%2 <> 0 and description != 'boring' 29 | order by rating desc -------------------------------------------------------------------------------- /L 626 Exchange Seats.md: -------------------------------------------------------------------------------- 1 | Mary is a teacher in a middle school and she has a table seat storing students' names and their corresponding seat ids. 2 | 3 | The column id is continuous increment. 4 | Mary wants to change seats for the adjacent students. 5 | Can you write a SQL query to output the result for Mary? 6 | 7 | +---------+---------+ 8 | | id | student | 9 | +---------+---------+ 10 | | 1 | Abbot | 11 | | 2 | Doris | 12 | | 3 | Emerson | 13 | | 4 | Green | 14 | | 5 | Jeames | 15 | +---------+---------+ 16 | 17 | For the sample input, the output is: 18 | 19 | +---------+---------+ 20 | | id | student | 21 | +---------+---------+ 22 | | 1 | Doris | 23 | | 2 | Abbot | 24 | | 3 | Green | 25 | | 4 | Emerson | 26 | | 5 | Jeames | 27 | +---------+---------+ 28 | 29 | 30 | - solution: 31 | 32 | <1> Using flow control statement CASE WHEN 33 | 34 | SELECT (CASE WHEN mod(id,2)=1 AND id=counts then id 35 | WHEN mod(id,2)=1 AND id!=counts then id+1 36 | ELSE id-1 37 | END) as id, 38 | student 39 | FROM seat, (SELECT count(*) as counts FROM seat) as counts 40 | ORDER BY id; 41 | 42 | 43 | <2> Using bit manipulation and COALESCE() 44 | 45 | SELECT 46 | s1.id, COALESCE(s2.student, s1.student) AS student 47 | FROM 48 | seat s1 49 | LEFT JOIN 50 | seat s2 ON ((s1.id + 1) ^ 1) - 1 = s2.id 51 | ORDER BY s1.id; 52 | -------------------------------------------------------------------------------- /L 627 Swap Salary.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | Given a table salary, such as the one below, that has m=male and f=female values. Swap all f and m values (i.e., change all f values to m and vice versa) with a single update query and no intermediate temp table. 4 | For example: 5 | 6 | | id | name | sex | salary | 7 | |----|------|-----|--------| 8 | | 1 | A | m | 2500 | 9 | | 2 | B | f | 1500 | 10 | | 3 | C | m | 5500 | 11 | | 4 | D | f | 500 | 12 | After running your query, the above salary table should have the following rows: 13 | 14 | | id | name | sex | salary | 15 | |----|------|-----|--------| 16 | | 1 | A | f | 2500 | 17 | | 2 | B | m | 1500 | 18 | | 3 | C | f | 5500 | 19 | | 4 | D | m | 500 | 20 | 21 | - solution: 22 | 23 | update salary 24 | set 25 | sex=case when sex='m' then 'f' 26 | else 'm' 27 | end -------------------------------------------------------------------------------- /L1083_Sales_Analysis.sql: -------------------------------------------------------------------------------- 1 | Table: Product 2 | 3 | +--------------+---------+ 4 | | Column Name | Type | 5 | +--------------+---------+ 6 | | product_id | int | 7 | | product_name | varchar | 8 | | unit_price | int | 9 | +--------------+---------+ 10 | product_id is the primary key of this table. 11 | Table: Sales 12 | 13 | +-------------+---------+ 14 | | Column Name | Type | 15 | +-------------+---------+ 16 | | seller_id | int | 17 | | product_id | int | 18 | | buyer_id | int | 19 | | sale_date | date | 20 | | quantity | int | 21 | | price | int | 22 | +------ ------+---------+ 23 | This table has no primary key, it can have repeated rows. 24 | product_id is a foreign key to Product table. 25 | 26 | 27 | Write an SQL query that reports the buyers who have bought S8 but not iPhone. Note that S8 and iPhone are products present in the Product table. 28 | 29 | The query result format is in the following example: 30 | 31 | Product table: 32 | +------------+--------------+------------+ 33 | | product_id | product_name | unit_price | 34 | +------------+--------------+------------+ 35 | | 1 | S8 | 1000 | 36 | | 2 | G4 | 800 | 37 | | 3 | iPhone | 1400 | 38 | +------------+--------------+------------+ 39 | 40 | Sales table: 41 | +-----------+------------+----------+------------+----------+-------+ 42 | | seller_id | product_id | buyer_id | sale_date | quantity | price | 43 | +-----------+------------+----------+------------+----------+-------+ 44 | | 1 | 1 | 1 | 2019-01-21 | 2 | 2000 | 45 | | 1 | 2 | 2 | 2019-02-17 | 1 | 800 | 46 | | 2 | 1 | 3 | 2019-06-02 | 1 | 800 | 47 | | 3 | 3 | 3 | 2019-05-13 | 2 | 2800 | 48 | +-----------+------------+----------+------------+----------+-------+ 49 | 50 | Result table: 51 | +-------------+ 52 | | buyer_id | 53 | +-------------+ 54 | | 1 | 55 | +-------------+ 56 | The buyer with id 1 bought an S8 but didn't buy an iPhone. The buyer with id 3 bought both. 57 | 58 | 59 | # solution1: 60 | 61 | select 62 | distinct s.buyer_id 63 | from Sales s 64 | join Product p 65 | on s.product_id=p.product_id 66 | where p.product_name='S8' 67 | and s.buyer_id not in ( 68 | select 69 | s1.buyer_id 70 | from Sales s1 71 | join Product p1 72 | on s1.product_id=p1.product_id 73 | where p1.product_name='iPhone' 74 | ) 75 | 76 | # solution2: 77 | 78 | SELECT s.buyer_id 79 | FROM Sales AS s INNER JOIN Product AS p 80 | ON s.product_id = p.product_id 81 | GROUP BY s.buyer_id 82 | HAVING SUM(CASE WHEN p.product_name = 'S8' THEN 1 ELSE 0 END) > 0 83 | AND SUM(CASE WHEN p.product_name = 'iPhone' THEN 1 ELSE 0 END) = 0; 84 | 85 | 86 | -------------------------------------------------------------------------------- /L1107_New Users Daily Count.sql: -------------------------------------------------------------------------------- 1 | Table: Traffic 2 | 3 | +---------------+---------+ 4 | | Column Name | Type | 5 | +---------------+---------+ 6 | | user_id | int | 7 | | activity | enum | 8 | | activity_date | date | 9 | +---------------+---------+ 10 | There is no primary key for this table, it may have duplicate rows. 11 | The activity column is an ENUM type of ('login', 'logout', 'jobs', 'groups', 'homepage'). 12 | 13 | 14 | Write an SQL query that reports for every date within at most 90 days from today, the number of users that logged in for the first time on that date. Assume today is 2019-06-30. 15 | 16 | The query result format is in the following example: 17 | 18 | Traffic table: 19 | +---------+----------+---------------+ 20 | | user_id | activity | activity_date | 21 | +---------+----------+---------------+ 22 | | 1 | login | 2019-05-01 | 23 | | 1 | homepage | 2019-05-01 | 24 | | 1 | logout | 2019-05-01 | 25 | | 2 | login | 2019-06-21 | 26 | | 2 | logout | 2019-06-21 | 27 | | 3 | login | 2019-01-01 | 28 | | 3 | jobs | 2019-01-01 | 29 | | 3 | logout | 2019-01-01 | 30 | | 4 | login | 2019-06-21 | 31 | | 4 | groups | 2019-06-21 | 32 | | 4 | logout | 2019-06-21 | 33 | | 5 | login | 2019-03-01 | 34 | | 5 | logout | 2019-03-01 | 35 | | 5 | login | 2019-06-21 | 36 | | 5 | logout | 2019-06-21 | 37 | +---------+----------+---------------+ 38 | 39 | Result table: 40 | +------------+-------------+ 41 | | login_date | user_count | 42 | +------------+-------------+ 43 | | 2019-05-01 | 1 | 44 | | 2019-06-21 | 2 | 45 | +------------+-------------+ 46 | Note that we only care about dates with non zero user count. 47 | The user with id 5 first logged in on 2019-03-01 so he's not counted on 2019-06-21. 48 | 49 | 50 | # Write your MySQL query statement below 51 | 52 | 53 | select 54 | first_login_time as login_date, 55 | count(distinct user_id) as user_count 56 | from 57 | ( 58 | select 59 | user_id, 60 | first_login_time 61 | from 62 | ( 63 | select user_id, min(activity_date) as first_login_time 64 | from Traffic 65 | where activity='login' 66 | group by user_id 67 | ) t 68 | where first_login_time between date_sub('2019-06-30', interval 90 day) and '2019-06-30' 69 | ) t2 70 | group by first_login_time 71 | -------------------------------------------------------------------------------- /L1164_Product Price at a Given Date.sql: -------------------------------------------------------------------------------- 1 | Table: Products 2 | 3 | +---------------+---------+ 4 | | Column Name | Type | 5 | +---------------+---------+ 6 | | product_id | int | 7 | | new_price | int | 8 | | change_date | date | 9 | +---------------+---------+ 10 | (product_id, change_date) is the primary key of this table. 11 | Each row of this table indicates that the price of some product was changed to a new price at some date. 12 | 13 | 14 | Write an SQL query to find the prices of all products on 2019-08-16. Assume the price of all products before any change is 10. 15 | 16 | The query result format is in the following example: 17 | 18 | Products table: 19 | +------------+-----------+-------------+ 20 | | product_id | new_price | change_date | 21 | +------------+-----------+-------------+ 22 | | 1 | 20 | 2019-08-14 | 23 | | 2 | 50 | 2019-08-14 | 24 | | 1 | 30 | 2019-08-15 | 25 | | 1 | 35 | 2019-08-16 | 26 | | 2 | 65 | 2019-08-17 | 27 | | 3 | 20 | 2019-08-18 | 28 | +------------+-----------+-------------+ 29 | 30 | Result table: 31 | +------------+-------+ 32 | | product_id | price | 33 | +------------+-------+ 34 | | 2 | 50 | 35 | | 1 | 35 | 36 | | 3 | 10 | 37 | +------------+-------+ 38 | 39 | 40 | select 41 | p.product_id, 42 | coalesce(t3.price,10) as price 43 | from 44 | (select distinct product_id 45 | from Products 46 | ) p 47 | left join 48 | ( 49 | select t1.product_id, 50 | t2.new_price as price, 51 | t1.latest_change 52 | from 53 | ( 54 | select product_id, 55 | max(change_date) as latest_change 56 | from 57 | Products 58 | where change_date<='2019-08-16' 59 | group by product_id 60 | ) t1 61 | join 62 | Products t2 63 | on t1.product_id=t2.product_id and t1.latest_change=t2.change_date 64 | ) t3 65 | on p.product_id=t3.product_id 66 | 67 | -------------------------------------------------------------------------------- /L1179_ Reformat Department Table.sql: -------------------------------------------------------------------------------- 1 | Table: Department 2 | 3 | +---------------+---------+ 4 | | Column Name | Type | 5 | +---------------+---------+ 6 | | id | int | 7 | | revenue | int | 8 | | month | varchar | 9 | +---------------+---------+ 10 | (id, month) is the primary key of this table. 11 | The table has information about the revenue of each department per month. 12 | The month has values in ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]. 13 | 14 | 15 | Write an SQL query to reformat the table such that there is a department id column and a revenue column for each month. 16 | 17 | The query result format is in the following example: 18 | 19 | Department table: 20 | +------+---------+-------+ 21 | | id | revenue | month | 22 | +------+---------+-------+ 23 | | 1 | 8000 | Jan | 24 | | 2 | 9000 | Jan | 25 | | 3 | 10000 | Feb | 26 | | 1 | 7000 | Feb | 27 | | 1 | 6000 | Mar | 28 | +------+---------+-------+ 29 | 30 | Result table: 31 | +------+-------------+-------------+-------------+-----+-------------+ 32 | | id | Jan_Revenue | Feb_Revenue | Mar_Revenue | ... | Dec_Revenue | 33 | +------+-------------+-------------+-------------+-----+-------------+ 34 | | 1 | 8000 | 7000 | 6000 | ... | null | 35 | | 2 | 9000 | null | null | ... | null | 36 | | 3 | null | 10000 | null | ... | null | 37 | +------+-------------+-------------+-------------+-----+-------------+ 38 | 39 | Note that the result table has 13 columns (1 for the department id + 12 for the months). 40 | 41 | 42 | # Write your MySQL query statement below 43 | 44 | select 45 | id, 46 | sum(case when month='Jan' then revenue end) as Jan_Revenue, 47 | sum(case when month='Feb' then revenue end) as Feb_Revenue, 48 | sum(case when month='Mar' then revenue end) as Mar_Revenue, 49 | sum(case when month='Apr' then revenue end) as Apr_Revenue, 50 | sum(case when month='May' then revenue end) as May_Revenue, 51 | sum(case when month='Jun' then revenue end) as Jun_Revenue, 52 | sum(case when month='Jul' then revenue end) as Jul_Revenue, 53 | sum(case when month='Aug' then revenue end) as Aug_Revenue, 54 | sum(case when month='Sep' then revenue end) as Sep_Revenue, 55 | sum(case when month='Oct' then revenue end) as Oct_Revenue, 56 | sum(case when month='Nov' then revenue end) as Nov_Revenue, 57 | sum(case when month='Dec' then revenue end) as Dec_Revenue 58 | from Department 59 | group by id 60 | 61 | 62 | -------------------------------------------------------------------------------- /L1204_Last Person to Fit in the Elevator.sql: -------------------------------------------------------------------------------- 1 | Table: Queue 2 | 3 | +-------------+---------+ 4 | | Column Name | Type | 5 | +-------------+---------+ 6 | | person_id | int | 7 | | person_name | varchar | 8 | | weight | int | 9 | | turn | int | 10 | +-------------+---------+ 11 | person_id is the primary key column for this table. 12 | This table has the information about all people waiting for an elevator. 13 | The person_id and turn columns will contain all numbers from 1 to n, where n is the number of rows in the table. 14 | 15 | 16 | The maximum weight the elevator can hold is 1000. 17 | 18 | Write an SQL query to find the person_name of the last person who will fit in the elevator without exceeding the weight limit. It is guaranteed that the person who is first in the queue can fit in the elevator. 19 | 20 | The query result format is in the following example: 21 | 22 | Queue table 23 | +-----------+-------------------+--------+------+ 24 | | person_id | person_name | weight | turn | 25 | +-----------+-------------------+--------+------+ 26 | | 5 | George Washington | 250 | 1 | 27 | | 3 | John Adams | 350 | 2 | 28 | | 6 | Thomas Jefferson | 400 | 3 | 29 | | 2 | Will Johnliams | 200 | 4 | 30 | | 4 | Thomas Jefferson | 175 | 5 | 31 | | 1 | James Elephant | 500 | 6 | 32 | +-----------+-------------------+--------+------+ 33 | 34 | Result table 35 | +-------------------+ 36 | | person_name | 37 | +-------------------+ 38 | | Thomas Jefferson | 39 | +-------------------+ 40 | 41 | Queue table is ordered by turn in the example for simplicity. 42 | In the example George Washington(id 5), John Adams(id 3) and Thomas Jefferson(id 6) will enter the elevator as their weight sum is 250 + 350 + 400 = 1000. 43 | Thomas Jefferson(id 6) is the last person to fit in the elevator because he has the last turn in these three people. 44 | 45 | -- using aggregation method 46 | 47 | SELECT q1.person_name 48 | FROM Queue q1 JOIN Queue q2 ON q1.turn >= q2.turn 49 | GROUP BY q1.turn 50 | HAVING SUM(q2.weight) <= 1000 51 | ORDER BY SUM(q2.weight) DESC 52 | LIMIT 1 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /L1212_Team_Scores_in_Football_Tournament.sql: -------------------------------------------------------------------------------- 1 | Table: Teams 2 | 3 | +---------------+----------+ 4 | | Column Name | Type | 5 | +---------------+----------+ 6 | | team_id | int | 7 | | team_name | varchar | 8 | +---------------+----------+ 9 | team_id is the primary key of this table. 10 | Each row of this table represents a single football team. 11 | Table: Matches 12 | 13 | +---------------+---------+ 14 | | Column Name | Type | 15 | +---------------+---------+ 16 | | match_id | int | 17 | | host_team | int | 18 | | guest_team | int | 19 | | host_goals | int | 20 | | guest_goals | int | 21 | +---------------+---------+ 22 | match_id is the primary key of this table. 23 | Each row is a record of a finished match between two different teams. 24 | Teams host_team and guest_team are represented by their IDs in the teams table (team_id) and they scored host_goals and guest_goals goals respectively. 25 | 26 | 27 | You would like to compute the scores of all teams after all matches. Points are awarded as follows: 28 | A team receives three points if they win a match (Score strictly more goals than the opponent team). 29 | A team receives one point if they draw a match (Same number of goals as the opponent team). 30 | A team receives no points if they lose a match (Score less goals than the opponent team). 31 | Write an SQL query that selects the team_id, team_name and num_points of each team in the tournament after all described matches. Result table should be ordered by num_points (decreasing order). In case of a tie, order the records by team_id (increasing order). 32 | 33 | The query result format is in the following example: 34 | 35 | Teams table: 36 | +-----------+--------------+ 37 | | team_id | team_name | 38 | +-----------+--------------+ 39 | | 10 | Leetcode FC | 40 | | 20 | NewYork FC | 41 | | 30 | Atlanta FC | 42 | | 40 | Chicago FC | 43 | | 50 | Toronto FC | 44 | +-----------+--------------+ 45 | 46 | Matches table: 47 | +------------+--------------+---------------+-------------+--------------+ 48 | | match_id | host_team | guest_team | host_goals | guest_goals | 49 | +------------+--------------+---------------+-------------+--------------+ 50 | | 1 | 10 | 20 | 3 | 0 | 51 | | 2 | 30 | 10 | 2 | 2 | 52 | | 3 | 10 | 50 | 5 | 1 | 53 | | 4 | 20 | 30 | 1 | 0 | 54 | | 5 | 50 | 30 | 1 | 0 | 55 | +------------+--------------+---------------+-------------+--------------+ 56 | 57 | Result table: 58 | +------------+--------------+---------------+ 59 | | team_id | team_name | num_points | 60 | +------------+--------------+---------------+ 61 | | 10 | Leetcode FC | 7 | 62 | | 20 | NewYork FC | 3 | 63 | | 50 | Toronto FC | 3 | 64 | | 30 | Atlanta FC | 1 | 65 | | 40 | Chicago FC | 0 | 66 | +------------+--------------+---------------+ 67 | 68 | Solution: 69 | 70 | 71 | select 72 | Teams.team_id, 73 | Teams.team_name, 74 | sum(case when t.hg>t.gg then 3 75 | when t.hg=t.gg then 1 76 | else 0 end) as num_points 77 | from Teams 78 | left join 79 | ( 80 | select 81 | host_team, 82 | guest_team, 83 | host_goals as hg, 84 | guest_goals as gg 85 | from Matches m1 86 | union all 87 | select 88 | guest_team, 89 | host_team, 90 | guest_goals, 91 | host_goals 92 | from Matches m2 93 | ) t 94 | on t.host_team=Teams.team_id 95 | group by t.host_team, Teams.team_name 96 | order by num_points desc, team_id asc 97 | -------------------------------------------------------------------------------- /L1264_Page Recommendations.sql: -------------------------------------------------------------------------------- 1 | Table: Friendship 2 | 3 | +---------------+---------+ 4 | | Column Name | Type | 5 | +---------------+---------+ 6 | | user1_id | int | 7 | | user2_id | int | 8 | +---------------+---------+ 9 | (user1_id, user2_id) is the primary key for this table. 10 | Each row of this table indicates that there is a friendship relation between user1_id and user2_id. 11 | 12 | 13 | Table: Likes 14 | 15 | +-------------+---------+ 16 | | Column Name | Type | 17 | +-------------+---------+ 18 | | user_id | int | 19 | | page_id | int | 20 | +-------------+---------+ 21 | (user_id, page_id) is the primary key for this table. 22 | Each row of this table indicates that user_id likes page_id. 23 | 24 | 25 | Write an SQL query to recommend pages to the user with user_id = 1 using the pages that your friends liked. It should not recommend pages you already liked. 26 | 27 | Return result table in any order without duplicates. 28 | 29 | The query result format is in the following example: 30 | 31 | Friendship table: 32 | +----------+----------+ 33 | | user1_id | user2_id | 34 | +----------+----------+ 35 | | 1 | 2 | 36 | | 1 | 3 | 37 | | 1 | 4 | 38 | | 2 | 3 | 39 | | 2 | 4 | 40 | | 2 | 5 | 41 | | 6 | 1 | 42 | +----------+----------+ 43 | 44 | Likes table: 45 | +---------+---------+ 46 | | user_id | page_id | 47 | +---------+---------+ 48 | | 1 | 88 | 49 | | 2 | 23 | 50 | | 3 | 24 | 51 | | 4 | 56 | 52 | | 5 | 11 | 53 | | 6 | 33 | 54 | | 2 | 77 | 55 | | 3 | 77 | 56 | | 6 | 88 | 57 | +---------+---------+ 58 | 59 | Result table: 60 | +------------------+ 61 | | recommended_page | 62 | +------------------+ 63 | | 23 | 64 | | 24 | 65 | | 56 | 66 | | 33 | 67 | | 77 | 68 | +------------------+ 69 | User one is friend with users 2, 3, 4 and 6. 70 | Suggested pages are 23 from user 2, 24 from user 3, 56 from user 3 and 33 from user 6. 71 | Page 77 is suggested from both user 2 and user 3. 72 | Page 88 is not suggested because user 1 already likes it. 73 | 74 | 75 | # Write your MySQL query statement below 76 | 77 | select distinct l.page_id as recommended_page 78 | from 79 | ( 80 | select user2_id 81 | from Friendship 82 | where user1_id=1 83 | union 84 | select user1_id 85 | from Friendship 86 | where user2_id=1 87 | ) t 88 | join 89 | Likes l 90 | on t.user2_id=l.user_id 91 | where l.page_id not in (select distinct page_id 92 | from Likes 93 | where user_id=1) 94 | 95 | 96 | -------------------------------------------------------------------------------- /L1321_Restaurant Growth.sql: -------------------------------------------------------------------------------- 1 | Table: Customer 2 | 3 | +---------------+---------+ 4 | | Column Name | Type | 5 | +---------------+---------+ 6 | | customer_id | int | 7 | | name | varchar | 8 | | visited_on | date | 9 | | amount | int | 10 | +---------------+---------+ 11 | (customer_id, visited_on) is the primary key for this table. 12 | This table contains data about customer transactions in a restaurant. 13 | visited_on is the date on which the customer with ID (customer_id) have visited the restaurant. 14 | amount is the total paid by a customer. 15 | 16 | 17 | You are the restaurant owner and you want to analyze a possible expansion (there will be at least one customer every day). 18 | 19 | Write an SQL query to compute moving average of how much customer paid in a 7 days window (current day + 6 days before) . 20 | 21 | The query result format is in the following example: 22 | 23 | Return result table ordered by visited_on. 24 | 25 | average_amount should be rounded to 2 decimal places, all dates are in the format ('YYYY-MM-DD'). 26 | 27 | 28 | 29 | Customer table: 30 | +-------------+--------------+--------------+-------------+ 31 | | customer_id | name | visited_on | amount | 32 | +-------------+--------------+--------------+-------------+ 33 | | 1 | Jhon | 2019-01-01 | 100 | 34 | | 2 | Daniel | 2019-01-02 | 110 | 35 | | 3 | Jade | 2019-01-03 | 120 | 36 | | 4 | Khaled | 2019-01-04 | 130 | 37 | | 5 | Winston | 2019-01-05 | 110 | 38 | | 6 | Elvis | 2019-01-06 | 140 | 39 | | 7 | Anna | 2019-01-07 | 150 | 40 | | 8 | Maria | 2019-01-08 | 80 | 41 | | 9 | Jaze | 2019-01-09 | 110 | 42 | | 1 | Jhon | 2019-01-10 | 130 | 43 | | 3 | Jade | 2019-01-10 | 150 | 44 | +-------------+--------------+--------------+-------------+ 45 | 46 | Result table: 47 | +--------------+--------------+----------------+ 48 | | visited_on | amount | average_amount | 49 | +--------------+--------------+----------------+ 50 | | 2019-01-07 | 860 | 122.86 | 51 | | 2019-01-08 | 840 | 120 | 52 | | 2019-01-09 | 840 | 120 | 53 | | 2019-01-10 | 1000 | 142.86 | 54 | +--------------+--------------+----------------+ 55 | 56 | 1st moving average from 2019-01-01 to 2019-01-07 has an average_amount of (100 + 110 + 120 + 130 + 110 + 140 + 150)/7 = 122.86 57 | 2nd moving average from 2019-01-02 to 2019-01-08 has an average_amount of (110 + 120 + 130 + 110 + 140 + 150 + 80)/7 = 120 58 | 3rd moving average from 2019-01-03 to 2019-01-09 has an average_amount of (120 + 130 + 110 + 140 + 150 + 80 + 110)/7 = 120 59 | 4th moving average from 2019-01-04 to 2019-01-10 has an average_amount of (130 + 110 + 140 + 150 + 80 + 110 + 130 + 150)/7 = 142.86 60 | 61 | 62 | # Write your MySQL query statement below 63 | 64 | 65 | SELECT a.visited_on AS visited_on, SUM(b.day_sum) AS amount, 66 | ROUND(AVG(b.day_sum), 2) AS average_amount 67 | FROM 68 | (SELECT visited_on, SUM(amount) AS day_sum FROM Customer GROUP BY visited_on ) a, 69 | (SELECT visited_on, SUM(amount) AS day_sum FROM Customer GROUP BY visited_on ) b 70 | WHERE DATEDIFF(a.visited_on, b.visited_on) BETWEEN 0 AND 6 71 | GROUP BY a.visited_on 72 | HAVING COUNT(b.visited_on) = 7 73 | 74 | -------------------------------------------------------------------------------- /L175 Combine Two Tables.md: -------------------------------------------------------------------------------- 1 | Table: Person 2 | 3 | +-------------+---------+ 4 | | Column Name | Type | 5 | +-------------+---------+ 6 | | PersonId | int | 7 | | FirstName | varchar | 8 | | LastName | varchar | 9 | +-------------+---------+ 10 | PersonId is the primary key column for this table. 11 | Table: Address 12 | 13 | +-------------+---------+ 14 | | Column Name | Type | 15 | +-------------+---------+ 16 | | AddressId | int | 17 | | PersonId | int | 18 | | City | varchar | 19 | | State | varchar | 20 | +-------------+---------+ 21 | AddressId is the primary key column for this table. 22 | 23 | 24 | Write a SQL query for a report that provides the following information for each person in the Person table, regardless if there is an address for each of those people: 25 | 26 | FirstName, LastName, City, State 27 | 28 | - Solution: 29 | 30 | select p.FirstName, p.LastName, a.City,a.State 31 | from Person p left join Address a 32 | on p.PersonId=a.PersonId 33 | -------------------------------------------------------------------------------- /L176 Second Highest Salary.md: -------------------------------------------------------------------------------- 1 | Write a SQL query to get the second highest salary from the Employee table. 2 | 3 | +----+--------+ 4 | | Id | Salary | 5 | +----+--------+ 6 | | 1 | 100 | 7 | | 2 | 200 | 8 | | 3 | 300 | 9 | +----+--------+ 10 | For example, given the above Employee table, the query should return 200 as the second highest salary. If there is no second highest salary, then the query should return null. 11 | 12 | +---------------------+ 13 | | SecondHighestSalary | 14 | +---------------------+ 15 | | 200 | 16 | +---------------------+ 17 | 18 | - solution: 19 | 20 | (1) 21 | 22 | select max(Salary) as SecondHighestSalary 23 | from Employee 24 | where Salary < (select max(Salary) from Employee); 25 | 26 | (2) 27 | 28 | select ifnull( 29 | ( 30 | select distinct Salary 31 | from 32 | ( 33 | select Salary, 34 | dense_rank() over (order by Salary desc) as r 35 | from Employee 36 | ) t 37 | where r=2 38 | ),null) as SecondHighestSalary 39 | 40 | (3) 41 | 42 | select 43 | IFNULL( 44 | (select distinct Salary from Employee order by Salary Desc limit 1 offset 1),null) 45 | as SecondHighestSalary 46 | -------------------------------------------------------------------------------- /L178 Rank Scores.sql: -------------------------------------------------------------------------------- 1 | Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ranking. Note that after a tie, the next ranking number should be the next consecutive integer value. In other words, there should be no "holes" between ranks. 2 | 3 | +----+-------+ 4 | | Id | Score | 5 | +----+-------+ 6 | | 1 | 3.50 | 7 | | 2 | 3.65 | 8 | | 3 | 4.00 | 9 | | 4 | 3.85 | 10 | | 5 | 4.00 | 11 | | 6 | 3.65 | 12 | +----+-------+ 13 | For example, given the above Scores table, your query should generate the following report (order by highest score): 14 | 15 | +-------+------+ 16 | | Score | Rank | 17 | +-------+------+ 18 | | 4.00 | 1 | 19 | | 4.00 | 1 | 20 | | 3.85 | 2 | 21 | | 3.65 | 3 | 22 | | 3.65 | 3 | 23 | | 3.50 | 4 | 24 | +-------+------+ 25 | 26 | - solution: 27 | 28 | (MySQL) 29 | 30 | SELECT tb1.Score as Score, (SELECT COUNT(DISTINCT tb2.Score) 31 | FROM scores as tb2 32 | WHERE tb2.Score> tb1.Score) +1 as 'rank' 33 | FROM scores as tb1 34 | ORDER BY tb1.Score DESC 35 | 36 | 37 | (MSQL Server) 38 | 39 | Idea: using analytic function dense_rank() 40 | 41 | select Score, dense_rank() over (order by Score desc) as Rank 42 | from Scores 43 | 44 | 45 | 46 | create table if not exists scores(Id integer, Score double); 47 | 48 | insert into scores(Id,Score) values (1,3.50); 49 | insert into scores(Id,Score) values (2,3.65); 50 | insert into scores(Id,Score) values (3,4.00); 51 | insert into scores(Id,Score) values (4,3.85); 52 | insert into scores(Id,Score) values (5,4.00); 53 | insert into scores(Id,Score) values (6,3.65); 54 | -------------------------------------------------------------------------------- /L180 Consecutive Numbers.md: -------------------------------------------------------------------------------- 1 | Write a SQL query to find all numbers that appear at least three times consecutively. 2 | 3 | +----+-----+ 4 | | Id | Num | 5 | +----+-----+ 6 | | 1 | 1 | 7 | | 2 | 1 | 8 | | 3 | 1 | 9 | | 4 | 2 | 10 | | 5 | 1 | 11 | | 6 | 2 | 12 | | 7 | 2 | 13 | +----+-----+ 14 | For example, given the above Logs table, 1 is the only number that appears consecutively for at least three times. 15 | 16 | +-----------------+ 17 | | ConsecutiveNums | 18 | +-----------------+ 19 | | 1 | 20 | +-----------------+ 21 | 22 | - solution: 23 | 24 | <1> Analytic function: 25 | 26 | select distinct Num as ConsecutiveNums 27 | from 28 | (select Num, lead(Num,1,null) over (order by Id) as l1, 29 | lag(Num,1,null) over (order by Id) as l2 30 | from Logs) t 31 | where Num=l1 and l1=l2 32 | 33 | <2> Regular method: 34 | 35 | select distinct l1.Num as ConsecutiveNums 36 | from logs l1 join 37 | logs l2 on l1.Id=l2.Id-1 38 | join logs l3 on l1.Id=l3.Id-2 39 | where l1.Num=l2.Num and l2.Num=l3.Num -------------------------------------------------------------------------------- /L181 Emplyee Earning More Than Their Manager.md: -------------------------------------------------------------------------------- 1 | The Employee table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id. 2 | 3 | +----+-------+--------+-----------+ 4 | | Id | Name | Salary | ManagerId | 5 | +----+-------+--------+-----------+ 6 | | 1 | Joe | 70000 | 3 | 7 | | 2 | Henry | 80000 | 4 | 8 | | 3 | Sam | 60000 | NULL | 9 | | 4 | Max | 90000 | NULL | 10 | +----+-------+--------+-----------+ 11 | 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. 12 | 13 | +----------+ 14 | | Employee | 15 | +----------+ 16 | | Joe | 17 | +----------+ 18 | 19 | - solution: 20 | 21 | select e1.Name as Employee 22 | from Employee e1 23 | join Employee e2 on e1.ManagerId=e2.Id 24 | where e1.Salary > e2.Salary -------------------------------------------------------------------------------- /L182 Duplicate Emails.md: -------------------------------------------------------------------------------- 1 | SQL Schema 2 | Write a SQL query to find all duplicate emails in a table named Person. 3 | 4 | +----+---------+ 5 | | Id | Email | 6 | +----+---------+ 7 | | 1 | a@b.com | 8 | | 2 | c@d.com | 9 | | 3 | a@b.com | 10 | +----+---------+ 11 | For example, your query should return the following for the above table: 12 | 13 | +---------+ 14 | | Email | 15 | +---------+ 16 | | a@b.com | 17 | +---------+ 18 | Note: All emails are in lowercase. 19 | 20 | - solution: 21 | 22 | select Email 23 | from Person 24 | group by Email 25 | having count(*)>1 -------------------------------------------------------------------------------- /L183 Customers Who Never Order.md: -------------------------------------------------------------------------------- 1 | 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. 2 | 3 | Table: Customers. 4 | 5 | +----+-------+ 6 | | Id | Name | 7 | +----+-------+ 8 | | 1 | Joe | 9 | | 2 | Henry | 10 | | 3 | Sam | 11 | | 4 | Max | 12 | +----+-------+ 13 | Table: Orders. 14 | 15 | +----+------------+ 16 | | Id | CustomerId | 17 | +----+------------+ 18 | | 1 | 3 | 19 | | 2 | 1 | 20 | +----+------------+ 21 | Using the above tables as example, return the following: 22 | 23 | +-----------+ 24 | | Customers | 25 | +-----------+ 26 | | Henry | 27 | | Max | 28 | +-----------+ 29 | - solution: 30 | 31 | select Name as Customers 32 | from Customers 33 | where Id not in (select CustomerId from Orders) -------------------------------------------------------------------------------- /L184 Department Highest Salary.md: -------------------------------------------------------------------------------- 1 | The Employee table holds all employees. Every employee has an Id, a salary, and there is also a column for the department Id. 2 | 3 | +----+-------+--------+--------------+ 4 | | Id | Name | Salary | DepartmentId | 5 | +----+-------+--------+--------------+ 6 | | 1 | Joe | 70000 | 1 | 7 | | 2 | Henry | 80000 | 2 | 8 | | 3 | Sam | 60000 | 2 | 9 | | 4 | Max | 90000 | 1 | 10 | +----+-------+--------+--------------+ 11 | The Department table holds all departments of the company. 12 | 13 | +----+----------+ 14 | | Id | Name | 15 | +----+----------+ 16 | | 1 | IT | 17 | | 2 | Sales | 18 | +----+----------+ 19 | Write a SQL query to find employees who have the highest salary in each of the departments. For the above tables, Max has the highest salary in the IT department and Henry has the highest salary in the Sales department. 20 | 21 | +------------+----------+--------+ 22 | | Department | Employee | Salary | 23 | +------------+----------+--------+ 24 | | IT | Max | 90000 | 25 | | Sales | Henry | 80000 | 26 | +------------+----------+--------+ 27 | 28 | - solution: 29 | 30 | <1> Regular method: 31 | 32 | 33 | SELECT tb2.Name as Department, tb1.Name as Employee, tb1.Salary as Salary 34 | FROM Employee as tb1 35 | LEFT JOIN Department tb2 36 | ON tb1.DepartmentId=tb2.Id 37 | WHERE (tb2.Id,tb1.Salary) in (SELECT DepartmentId,max(Salary) 38 | FROM Employee 39 | GROUP BY DepartmentId); 40 | 41 | 42 | <2> Analytic function: 43 | 44 | select t.Department as Department, 45 | t.Name as Employee, 46 | t.Salary as Salary 47 | from 48 | (select d.Name as Department, 49 | e.Name as Name, 50 | e.Salary as Salary, 51 | (dense_rank() over 52 | (partition by e.DepartmentId order by e.Salary desc) ) as sal_rank 53 | from Department d join Employee e 54 | on d.Id=e.DepartmentId) t 55 | where t.sal_rank <2 56 | -------------------------------------------------------------------------------- /L185 Department Top Three Salaries.sql: -------------------------------------------------------------------------------- 1 | The Employee table holds all employees. Every employee has an Id, and there is also a column for the department Id. 2 | 3 | +----+-------+--------+--------------+ 4 | | Id | Name | Salary | DepartmentId | 5 | +----+-------+--------+--------------+ 6 | | 1 | Joe | 70000 | 1 | 7 | | 2 | Henry | 80000 | 2 | 8 | | 3 | Sam | 60000 | 2 | 9 | | 4 | Max | 90000 | 1 | 10 | | 5 | Janet | 69000 | 1 | 11 | | 6 | Randy | 85000 | 1 | 12 | +----+-------+--------+--------------+ 13 | The Department table holds all departments of the company. 14 | 15 | +----+----------+ 16 | | Id | Name | 17 | +----+----------+ 18 | | 1 | IT | 19 | | 2 | Sales | 20 | +----+----------+ 21 | 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. 22 | 23 | +------------+----------+--------+ 24 | | Department | Employee | Salary | 25 | +------------+----------+--------+ 26 | | IT | Max | 90000 | 27 | | IT | Randy | 85000 | 28 | | IT | Joe | 70000 | 29 | | Sales | Henry | 80000 | 30 | | Sales | Sam | 60000 | 31 | +------------+----------+--------+ 32 | 33 | - solution: 34 | 35 | <1> Regular method: 36 | 37 | select d.Name Department, e1.Name Employee, e1.Salary 38 | from Employee e1 39 | join Department d 40 | on e1.DepartmentId = d.Id 41 | where (select count(distinct(e2.Salary)) 42 | from Employee e2 43 | where e2.Salary > e1.Salary 44 | and e1.DepartmentId = e2.DepartmentId 45 | ) < 3 46 | 47 | <1.2> Method without using subquery: 48 | 49 | WITH top_three_employees AS ( 50 | SELECT 51 | e1.ID 52 | , e1.Name 53 | , e1.DepartmentId 54 | , e1.Salary 55 | , COUNT(DISTINCT e2.Salary) AS rk 56 | FROM Employee AS e1 57 | INNER JOIN Employee AS e2 58 | ON e1.DepartmentId = e2.DepartmentId AND e1.Salary <= e2.Salary 59 | GROUP BY e1.ID, e1.Name, e1.DepartmentId, e1.Salary 60 | ) 61 | SELECT 62 | d.Name AS Department 63 | , t.Name AS Employee 64 | , t.Salary AS Salary 65 | FROM Department AS d 66 | LEFT JOIN top_three_employees AS t 67 | ON d.Id = t.DepartmentId 68 | WHERE t.rk <= 3 69 | 70 | <2> Analytic function: 71 | 72 | SELECT a.Department, 73 | a.Employee, 74 | a.Salary 75 | FROM 76 | (Select b.Name as Department, 77 | a.Name as Employee, 78 | a.salary As Salary, 79 | DENSE_RANK() over 80 | (PARTITION BY b.Name ORDER BY a.Salary DESC) as dept_sal_Rank 81 | from Employee a 82 | INNER JOIN Department b 83 | ON a.DepartmentId = b.Id ) a 84 | where a.dept_sal_Rank <=3; 85 | -------------------------------------------------------------------------------- /L534_Game_Play_Analysis III.sql: -------------------------------------------------------------------------------- 1 | Table: Activity 2 | 3 | +--------------+---------+ 4 | | Column Name | Type | 5 | +--------------+---------+ 6 | | player_id | int | 7 | | device_id | int | 8 | | event_date | date | 9 | | games_played | int | 10 | +--------------+---------+ 11 | (player_id, event_date) is the primary key of this table. 12 | This table shows the activity of players of some game. 13 | Each row is a record of a player who logged in and played a number of games (possibly 0) before logging out on some day using some device. 14 | 15 | 16 | Write an SQL query that reports for each player and date, how many games played so far by the player. That is, the total number of games played by the player until that date. Check the example for clarity. 17 | 18 | The query result format is in the following example: 19 | 20 | Activity table: 21 | +-----------+-----------+------------+--------------+ 22 | | player_id | device_id | event_date | games_played | 23 | +-----------+-----------+------------+--------------+ 24 | | 1 | 2 | 2016-03-01 | 5 | 25 | | 1 | 2 | 2016-05-02 | 6 | 26 | | 1 | 3 | 2017-06-25 | 1 | 27 | | 3 | 1 | 2016-03-02 | 0 | 28 | | 3 | 4 | 2018-07-03 | 5 | 29 | +-----------+-----------+------------+--------------+ 30 | 31 | Result table: 32 | +-----------+------------+---------------------+ 33 | | player_id | event_date | games_played_so_far | 34 | +-----------+------------+---------------------+ 35 | | 1 | 2016-03-01 | 5 | 36 | | 1 | 2016-05-02 | 11 | 37 | | 1 | 2017-06-25 | 12 | 38 | | 3 | 2016-03-02 | 0 | 39 | | 3 | 2018-07-03 | 5 | 40 | +-----------+------------+---------------------+ 41 | For the player with id 1, 5 + 6 = 11 games played by 2016-05-02, and 5 + 6 + 1 = 12 games played by 2017-06-25. 42 | For the player with id 3, 0 + 5 = 5 games played by 2018-07-03. 43 | Note that for each player we only care about the days when the player logged in. 44 | 45 | 46 | # Write your MySQL query statement below 47 | 48 | 49 | select a1.player_id, a1.event_date, sum(a2.games_played) as games_played_so_far 50 | from activity as a1 51 | inner join activity as a2 52 | on a1.event_date >= a2.event_date 53 | and a1.player_id = a2.player_id 54 | group by a1.player_id, a1.event_date 55 | 56 | -------------------------------------------------------------------------------- /L550_Game Play Analysis IV.sql: -------------------------------------------------------------------------------- 1 | Table: Activity 2 | 3 | +--------------+---------+ 4 | | Column Name | Type | 5 | +--------------+---------+ 6 | | player_id | int | 7 | | device_id | int | 8 | | event_date | date | 9 | | games_played | int | 10 | +--------------+---------+ 11 | (player_id, event_date) is the primary key of this table. 12 | This table shows the activity of players of some game. 13 | Each row is a record of a player who logged in and played a number of games (possibly 0) before logging out on some day using some device. 14 | 15 | 16 | Write an SQL query that reports the fraction of players that logged in again on the day after the day they first logged in, rounded to 2 decimal places. In other words, you need to count the number of players that logged in for at least two consecutive days starting from their first login date, then divide that number by the total number of players. 17 | 18 | The query result format is in the following example: 19 | 20 | Activity table: 21 | +-----------+-----------+------------+--------------+ 22 | | player_id | device_id | event_date | games_played | 23 | +-----------+-----------+------------+--------------+ 24 | | 1 | 2 | 2016-03-01 | 5 | 25 | | 1 | 2 | 2016-03-02 | 6 | 26 | | 2 | 3 | 2017-06-25 | 1 | 27 | | 3 | 1 | 2016-03-02 | 0 | 28 | | 3 | 4 | 2018-07-03 | 5 | 29 | +-----------+-----------+------------+--------------+ 30 | 31 | Result table: 32 | +-----------+ 33 | | fraction | 34 | +-----------+ 35 | | 0.33 | 36 | +-----------+ 37 | Only the player with id 1 logged back in after the first day he had logged in so the answer is 1/3 = 0.33 38 | 39 | 40 | # Write your MySQL query statement below 41 | 42 | 43 | select round(sum(case when temp.min_date + 1 = a.event_date then 1 else 0 end) 44 | / 45 | count(distinct temp.player_id), 2) as fraction 46 | from (select player_id, min(event_date) as min_date from activity group by player_id) as temp 47 | join activity a 48 | on temp.player_id = a.player_id 49 | 50 | -------------------------------------------------------------------------------- /L579_Find Cumulative Salary of an Employee.sql: -------------------------------------------------------------------------------- 1 | 2 | The Employee table holds the salary information in a year. 3 | 4 | Write a SQL to get the cumulative sum of an employee's salary over a period of 3 months but exclude the most recent month. 5 | 6 | The result should be displayed by 'Id' ascending, and then by 'Month' descending. 7 | 8 | Example 9 | Input 10 | 11 | | Id | Month | Salary | 12 | |----|-------|--------| 13 | | 1 | 1 | 20 | 14 | | 2 | 1 | 20 | 15 | | 1 | 2 | 30 | 16 | | 2 | 2 | 30 | 17 | | 3 | 2 | 40 | 18 | | 1 | 3 | 40 | 19 | | 3 | 3 | 60 | 20 | | 1 | 4 | 60 | 21 | | 3 | 4 | 70 | 22 | 23 | Output 24 | 25 | | Id | Month | Salary | 26 | |----|-------|--------| 27 | | 1 | 3 | 90 | 28 | | 1 | 2 | 50 | 29 | | 1 | 1 | 20 | 30 | | 2 | 1 | 20 | 31 | | 3 | 3 | 100 | 32 | | 3 | 2 | 40 | 33 | 34 | 35 | with cumulativeSalary as( 36 | select e1.Id,e1.Month, 37 | ifnull(e1.salary,0)+IfNULL(e2.salary,0)+IfNULL(e3.salary,0) as CumulativeSalary 38 | from employees2 e1 39 | left join employees2 e2 on e1.Id=e2.Id and e2.Month=e1.Month-1 40 | left join employees2 e3 on e3.Id=e2.Id and e3.Month=e2.Month-1 41 | ),MostRecentMonth as 42 | ( 43 | select id,max(Month) as MaxMonth from employees2 group by Id having(count(*)>1) 44 | ) 45 | select c.Id,c.Month,c.CumulativeSalary from cumulativeSalary c join MostRecentMonth m on c.Id=m.Id 46 | WHERE m.MaxMonth>c.Month 47 | order by Id asc, Month desc 48 | 49 | 50 | ### 51 | Create table employees2(Id int, Month int,salary int); 52 | 53 | insert into employees2 values(1,1,20); 54 | insert into employees2 values(2,1,20); 55 | insert into employees2 values(1,2,30); 56 | insert into employees2 values(2,2,30); 57 | insert into employees2 values(3,2,40); 58 | insert into employees2 values(1,3,40); 59 | insert into employees2 values(3,3,60); 60 | insert into employees2 values(1,4,60); 61 | insert into employees2 values(3,4,70); 62 | -------------------------------------------------------------------------------- /L580_Count Student Number in Departments.sql: -------------------------------------------------------------------------------- 1 | 2 | student 表: 3 | 4 | student_id student_name gender dept_id 5 | 1 Jack M 1 6 | 2 Jane F 1 7 | 3 Mark M 2 8 | 9 | 10 | department 表: 11 | 12 | dept_id dept_name 13 | 1 Engineering 14 | 2 Science 15 | 3 Law 16 | 17 | 输出: 18 | 19 | dept_name student_number 20 | Engineering 2 21 | Science 1 22 | Law 0 23 | 24 | 25 | 26 | SELECT d.dept_name, count(s.student_id) as student_number 27 | FROM department d 28 | LEFT JOIN student s 29 | ON d.dept_id=s.dept_id 30 | GROUP BY d.dept_name 31 | 32 | 33 | /* 34 | create table student (student_id int, student_name varchar(20), gender varchar(10), dept_id int); 35 | 36 | insert into student values (1,'Jack','M',1); 37 | insert into student values (2,'jane','F',1); 38 | insert into student values (3,'Mark','M',2); 39 | 40 | 41 | create table department (dept_id int, dept_name varchar(20)); 42 | 43 | insert into department values (1,'Engineering'); 44 | insert into department values (2,'Science'); 45 | insert into department values (3,'Law'); 46 | */ 47 | 48 | -------------------------------------------------------------------------------- /L584_Find_Customer_Referee.sql: -------------------------------------------------------------------------------- 1 | Given a table customer holding customers information and the referee. 2 | 3 | +------+------+-----------+ 4 | | id | name | referee_id| 5 | +------+------+-----------+ 6 | | 1 | Will | NULL | 7 | | 2 | Jane | NULL | 8 | | 3 | Alex | 2 | 9 | | 4 | Bill | NULL | 10 | | 5 | Zack | 1 | 11 | | 6 | Mark | 2 | 12 | +------+------+-----------+ 13 | Write a query to return the list of customers NOT referred by the person with id '2'. 14 | 15 | For the sample data above, the result is: 16 | 17 | +------+ 18 | | name | 19 | +------+ 20 | | Will | 21 | | Jane | 22 | | Bill | 23 | | Zack | 24 | +------+ 25 | 26 | Solution: 子query先找到referee_id=2的对应的id,然后从主句中选择id不等于这个id的 27 | 28 | SELECT tb1.name 29 | FROM customer tb1 30 | WHERE tb1.id not in 31 | ( 32 | SELECT id 33 | FROM customer 34 | WHERE referee_id=2 35 | ); 36 | 37 | 38 | /* 39 | create table customer (id int, name varchar(20), referee_id int); 40 | 41 | insert into customer values (1,'Will',NULL); 42 | insert into customer values (2,'Jane',NULL); 43 | insert into customer values (3,'Alex',2); 44 | insert into customer values (4,'Bill',NULL); 45 | insert into customer values (5,'Zack',1); 46 | insert into customer values (6,'Mark',2); 47 | */ 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /L585_Investments in 2016.sql: -------------------------------------------------------------------------------- 1 | Write a query to print the sum of all total investment values in 2016 (TIV_2016), to a scale of 2 decimal places, for all policy holders who meet the following criteria: 2 | 3 | Have the same TIV_2015 value as one or more other policyholders. 4 | Are not located in the same city as any other policyholder (i.e.: the (latitude, longitude) attribute pairs must be unique). 5 | Input Format: 6 | The insurance table is described as follows: 7 | 8 | | Column Name | Type | 9 | |-------------|---------------| 10 | | PID | INTEGER(11) | 11 | | TIV_2015 | NUMERIC(15,2) | 12 | | TIV_2016 | NUMERIC(15,2) | 13 | | LAT | NUMERIC(5,2) | 14 | | LON | NUMERIC(5,2) | 15 | where PID is the policyholder's policy ID, TIV_2015 is the total investment value in 2015, TIV_2016 is the total investment value in 2016, LAT is the latitude of the policy holder's city, and LON is the longitude of the policy holder's city. 16 | 17 | Sample Input 18 | 19 | | PID | TIV_2015 | TIV_2016 | LAT | LON | 20 | |-----|----------|----------|-----|-----| 21 | | 1 | 10 | 5 | 10 | 10 | 22 | | 2 | 20 | 20 | 20 | 20 | 23 | | 3 | 10 | 30 | 20 | 20 | 24 | | 4 | 10 | 40 | 40 | 40 | 25 | Sample Output 26 | 27 | | TIV_2016 | 28 | |----------| 29 | | 45.00 | 30 | 31 | 32 | SELECT SUM(tb1.TIV_2016) as TIV_2016 33 | FROM insurance as tb1 34 | WHERE tb1.TIV_2015 IN 35 | ( 36 | SELECT TIV_2015 37 | FROM insurance 38 | GROUP BY TIV_2015 39 | HAVING COUNT(*)>1 40 | ) 41 | AND 42 | CONCAT(tb1.LAT,tb1.LON) IN 43 | ( 44 | SELECT CONCAT(LAT,LON) as position 45 | FROM insurance 46 | GROUP BY LAT,LON 47 | HAVING COUNT(*)=1 48 | ); 49 | 50 | /* 51 | create table insurance (PID int, TIV_2015 int, TIV_2016 int, LAT int, LON int); 52 | 53 | insert into insurance values (1,10,5,10,10); 54 | insert into insurance values (2,20,20,20,20); 55 | insert into insurance values (3,10,30,20,20); 56 | insert into insurance values (4,10,40,40,40); 57 | */ 58 | 59 | 60 | -------------------------------------------------------------------------------- /L597_Friend Requests I: Overall Acceptance Rate.sql: -------------------------------------------------------------------------------- 1 | Round() and isnull,  round() can be used directly. 2 | In social network like Facebook or Twitter, people send friend requests and accept others’ requests as well. Now given two tables as below: 3 | Table: friend_request 4 | | sender_id | send_to_id |request_date| 5 | |-----------|------------|------------| 6 | | 1 | 2 | 2016_06-01 | 7 | | 1 | 3 | 2016_06-01 | 8 | | 1 | 4 | 2016_06-01 | 9 | | 2 | 3 | 2016_06-02 | 10 | | 3 | 4 | 2016-06-09 | 11 | 12 | Table: request_accepted 13 | | requester_id | accepter_id |accept_date | 14 | |--------------|-------------|------------| 15 | | 1 | 2 | 2016_06-03 | 16 | | 1 | 3 | 2016-06-08 | 17 | | 2 | 3 | 2016-06-08 | 18 | | 3 | 4 | 2016-06-09 | 19 | | 3 | 4 | 2016-06-10 | 20 | Write a query to find the overall acceptance rate of requests rounded to 2 decimals, which is the number of acceptance divide the number of requests. 21 | For the sample data above, your query should return the following result. 22 | |accept_rate| 23 | |-----------| 24 | | 0.80| 25 | --------------------- 26 | 27 | 28 | -- solution 1: 29 | 30 | # Write your MySQL query statement below 31 | select 32 | round( 33 | ifnull( 34 | ( 35 | select count(*) 36 | from 37 | ( 38 | select distinct requester_id, accepter_id from request_accepted 39 | ) a 40 | ) 41 | / 42 | ( 43 | select count(*) 44 | from 45 | ( 46 | select distinct sender_id, send_to_id from friend_request 47 | ) b 48 | ) 49 | ,0) 50 | ,2) as 'accept_rate' 51 | 52 | 53 | -- solution 2: 54 | 55 | select coalesce(round 56 | (count(distinct requester_id, accepter_id) 57 | / 58 | count(distinct sender_id, send_to_id),2), 59 | 0) 60 | as accept_rate 61 | from friend_request, request_accepted 62 | 63 | Follow up: 64 | Can you write a query to return the accept rate but for every month? 65 | 66 | select t2.request_month, t1.accept_total / t2.request_total as accept_rate_by_month from 67 | (select month(accept_date) as accept_month, count(distinct requester_id,accepter_id) as accept_total 68 | from request_accepted 69 | group by month(accept_date)) as t1 70 | join 71 | (select month(request_date) as request_month, count(distinct sender_id,send_to_id) as request_total 72 | from friend_request 73 | group by month(request_date)) as t2 74 | on t1.accept_month=t2.request_month; 75 | 76 | -------------------------------------------------------------------------------- /L602_Friend Requests II: Who Has the Most FriendsII.py: -------------------------------------------------------------------------------- 1 | Table request_accepted holds the data of friend acceptance, while requester_id and accepter_id both are the id of a person. 2 | 3 | 4 | 5 | | requester_id | accepter_id | accept_date| 6 | |--------------|-------------|------------| 7 | | 1 | 2 | 2016_06-03 | 8 | | 1 | 3 | 2016-06-08 | 9 | | 2 | 3 | 2016-06-08 | 10 | | 3 | 4 | 2016-06-09 | 11 | Write a query to find the the people who has most friends and the most friends number. For the sample data above, the result is: 12 | 13 | | id | num | 14 | |----|-----| 15 | | 3 | 3 | 16 | Note: 17 | 18 | It is guaranteed there is only 1 people having the most friends. 19 | The friend request could only been accepted once, which mean there is no multiple records with the same requester_id and accepter_id value. 20 | 21 | 22 | create table request_accepted(requester_id integer,accepter_id integer, accept_date date); 23 | insert into request_accepted(requester_id,accepter_id,accept_date) values (1,2,'2016-06-03'); 24 | insert into request_accepted(requester_id,accepter_id,accept_date) values (1,3,'2016-06-08'); 25 | insert into request_accepted(requester_id,accepter_id,accept_date) values (2,3,'2016-06-03'); 26 | insert into request_accepted(requester_id,accepter_id,accept_date) values (3,4,'2016-06-03'); 27 | 28 | 29 | select t.requester_id as id, count(*) as num 30 | from 31 | ( 32 | select requester_id, accepter_id 33 | from request_accepted 34 | 35 | union all 36 | 37 | select accepter_id, requester_id 38 | from request_accepted 39 | ) t 40 | group by 1 41 | order by 2 desc 42 | limit 1 43 | 44 | -------------------------------------------------------------------------------- /L603_Consecutive_Available_Seats.sql: -------------------------------------------------------------------------------- 1 | Several friends at a cinema ticket office would like to reserve consecutive available seats. 2 | Can you help to query all the consecutive available seats order by the seat_id using the following cinema table? 3 | | seat_id | free | 4 | |---------|------| 5 | | 1 | 1 | 6 | | 2 | 0 | 7 | | 3 | 1 | 8 | | 4 | 1 | 9 | | 5 | 1 | 10 | 11 | 12 | Your query should return the following result for the sample case above. 13 | 14 | 15 | | seat_id | 16 | |---------| 17 | | 3 | 18 | | 4 | 19 | | 5 | 20 | Note: 21 | The seat_id is an auto increment int, and free is bool ('1' means free, and '0' means occupied.). 22 | Consecutive available seats are more than 2(inclusive) seats consecutively available. 23 | 24 | 25 | 26 | SELECT DISTINCT a.seat_id 27 | FROM cinema a 28 | JOIN cinema b 29 | ON a.seat_id=b.seat_id-1 OR a.seat_id=b.seat_id+1 30 | WHERE a.free=1 and b.free=1 31 | ORDER BY a.seat_id; 32 | 33 | 34 | /* 35 | drop table cinema; 36 | 37 | create table cinema (seat_id int, free int); 38 | 39 | insert into cinema values (1,1); 40 | insert into cinema values (2,0); 41 | insert into cinema values (3,1); 42 | insert into cinema values (4,1); 43 | insert into cinema values (5,1); 44 | */ 45 | 46 | -------------------------------------------------------------------------------- /L607_Sales_Person.sql: -------------------------------------------------------------------------------- 1 | Description 2 | 3 | Given three tables: salesperson, company, orders. 4 | Output all the names in the table salesperson, who didn’t have sales to company 'RED'. 5 | 6 | Example 7 | Input 8 | 9 | Table: salesperson 10 | 11 | +----------+------+--------+-----------------+-----------+ 12 | | sales_id | name | salary | commission_rate | hire_date | 13 | +----------+------+--------+-----------------+-----------+ 14 | | 1 | John | 100000 | 6 | 4/1/2006 | 15 | | 2 | Amy | 120000 | 5 | 5/1/2010 | 16 | | 3 | Mark | 65000 | 12 | 12/25/2008| 17 | | 4 | Pam | 25000 | 25 | 1/1/2005 | 18 | | 5 | Alex | 50000 | 10 | 2/3/2007 | 19 | +----------+------+--------+-----------------+-----------+ 20 | The table salesperson holds the salesperson information. Every salesperson has a sales_id and a name. 21 | Table: company 22 | 23 | +---------+--------+------------+ 24 | | com_id | name | city | 25 | +---------+--------+------------+ 26 | | 1 | RED | Boston | 27 | | 2 | ORANGE | New York | 28 | | 3 | YELLOW | Boston | 29 | | 4 | GREEN | Austin | 30 | +---------+--------+------------+ 31 | The table company holds the company information. Every company has a com_id and a name. 32 | Table: orders 33 | 34 | +----------+------------+---------+----------+--------+ 35 | | order_id | order_date | com_id | sales_id | amount | 36 | +----------+------------+---------+----------+--------+ 37 | | 1 | 1/1/2014 | 3 | 4 | 100000 | 38 | | 2 | 2/1/2014 | 4 | 5 | 5000 | 39 | | 3 | 3/1/2014 | 1 | 1 | 50000 | 40 | | 4 | 4/1/2014 | 1 | 4 | 25000 | 41 | +----------+----------+---------+----------+--------+ 42 | The table orders holds the sales record information, salesperson and customer company are represented by sales_id and com_id. 43 | output 44 | 45 | +------+ 46 | | name | 47 | +------+ 48 | | Amy | 49 | | Mark | 50 | | Alex | 51 | +------+ 52 | 53 | 54 | SELECT 55 | s.name 56 | FROM 57 | salesperson s 58 | WHERE 59 | s.sales_id NOT IN (SELECT 60 | o.sales_id 61 | FROM 62 | orders o 63 | LEFT JOIN 64 | company c ON o.com_id = c.com_id 65 | WHERE 66 | c.name = 'RED'); 67 | -------------------------------------------------------------------------------- /L608_Tree_Node.sql: -------------------------------------------------------------------------------- 1 | Given a table tree, id is identifier of the tree node and p_id is its parent node's id. 2 | 3 | +----+------+ 4 | | id | p_id | 5 | +----+------+ 6 | | 1 | null | 7 | | 2 | 1 | 8 | | 3 | 1 | 9 | | 4 | 2 | 10 | | 5 | 2 | 11 | +----+------+ 12 | Each node in the tree can be one of three types: 13 | Leaf: if the node is a leaf node. 14 | Root: if the node is the root of the tree. 15 | Inner: If the node is neither a leaf node nor a root node. 16 | 17 | 18 | Write a query to print the node id and the type of the node. Sort your output by the node id. The result for the above sample is: 19 | 20 | 21 | +----+------+ 22 | | id | Type | 23 | +----+------+ 24 | | 1 | Root | 25 | | 2 | Inner| 26 | | 3 | Leaf | 27 | | 4 | Leaf | 28 | | 5 | Leaf | 29 | +----+------+ 30 | 31 | 32 | Solution: 33 | 34 | select 35 | tree.id, 36 | case when tree.id=(select t1.id from tree t1 where t1.p_id is null) 37 | then 'Root' 38 | when tree.id in (select t1.p_id from tree t1) 39 | then 'Inner' 40 | else 'Leaf' 41 | End as Type 42 | from tree 43 | 44 | /* 45 | create table tree (id int,p_id int); 46 | insert into tree values (1,null); 47 | insert into tree values (2,1); 48 | insert into tree values (3,1); 49 | insert into tree values (4,2); 50 | insert into tree values (5,2); 51 | */ 52 | -------------------------------------------------------------------------------- /L610_Triangle_Judgement.sql: -------------------------------------------------------------------------------- 1 | A pupil Tim gets homework to identify whether three line segments could possibly form a triangle. 2 | 3 | Could you help Tim by writing a query to judge whether these three sides can form a triangle, assuming table triangle holds the length of the three sides x, y and z. 4 | 5 | 6 | | x | y | z | 7 | |----|----|----| 8 | | 13 | 15 | 30 | 9 | | 10 | 20 | 15 | 10 | 11 | For the sample data above, your query should return the follow result: 12 | 13 | | x | y | z | triangle | 14 | |----|----|----|----------| 15 | | 13 | 15 | 30 | No | 16 | | 10 | 20 | 15 | Yes | 17 | 18 | Solution: 19 | idea: # apply case when actually to add one more column to indicate 'yes' or 'no' 20 | 21 | SELECT x, 22 | y, 23 | z, 24 | CASE WHEN x+y>z and x+z>y and y+z>x 25 | THEN 'YES' ELSE 'NO' 26 | END AS triangle 27 | FROM triangle; 28 | 29 | /* 30 | create table triangle (x int, y int, z int); 31 | 32 | insert into triangle values (13,15,30); 33 | insert into triangle values (10,20,15); 34 | */ 35 | -------------------------------------------------------------------------------- /L612_Shortest Distance in a Plane.sql: -------------------------------------------------------------------------------- 1 | Table point_2d holds the coordinates (x,y) of some unique points (more than two) in a plane. 2 | 3 | 4 | Write a query to find the shortest distance between these points rounded to 2 decimals. 5 | 6 | 7 | | x | y | 8 | |----|----| 9 | | -1 | -1 | 10 | | 0 | 0 | 11 | | -1 | -2 | 12 | 13 | 14 | The shortest distance is 1.00 from point (-1,-1) to (-1,2). So the output should be: 15 | 16 | 17 | | shortest | 18 | |----------| 19 | | 1.00 | 20 | 21 | 22 | 23 | select * from point_2d; 24 | 25 | SELECT Round(SQRT(MIN(POW(p1.x-p2.x,2)+POW(p1.y-p2.y,2))),2) as shortest 26 | FROM point_2d as p1 27 | JOIN point_2d as p2 28 | ON p1.x != p2.x OR p1.y != p2.y; 29 | 30 | 31 | 32 | # avoid to calculate the duplicated one 33 | SELECT 34 | ROUND(SQRT(MIN((POW(p1.x - p2.x, 2) + POW(p1.y - p2.y, 2)))),2) AS shortest 35 | FROM 36 | point_2d p1 37 | JOIN 38 | point_2d p2 ON (p1.x <= p2.x AND p1.y < p2.y) 39 | OR (p1.x <= p2.x AND p1.y > p2.y) 40 | OR (p1.x < p2.x AND p1.y = p2.y); 41 | 42 | 43 | /* 44 | create table point_2d (x int, y int); 45 | 46 | insert into point_2d values (-1,-1); 47 | insert into point_2d values (0,0); 48 | insert into point_2d values (-1,-2); 49 | */ 50 | -------------------------------------------------------------------------------- /L613_Shortest Distance in a Line .sql: -------------------------------------------------------------------------------- 1 | Table point holds the x coordinate of some points on x-axis in a plane, which are all integers. 2 | 3 | Write a query to find the shortest distance between two points in these points. 4 | 5 | x 6 | -1 7 | 0 8 | 2 9 | The shortest distance is ‘1’ obviously, which is from point ‘-1’ to ‘0’. So the output is as below: 10 | 11 | shortest 12 | 1 13 | Note: Every point is unique, which means there is no duplicates in table point. 14 | 15 | #idea: 先找到两者间的不同,然后用后面的减去前面的,取模找最小 16 | 17 | SELECT MIN(ABS(p2.x - p1.x)) AS shortest FROM point AS p1, point AS p2 WHERE p1.x <> p2.x; 18 | 19 | -------------------------------------------------------------------------------- /L614_Second_Degree_Follower.sql: -------------------------------------------------------------------------------- 1 | In facebook, there is a follow table with two columns: followee, follower. 2 | 3 | Please write a sql query to get the amount of each follower’s follower if he/she has one. 4 | 5 | For example: 6 | 7 | +-------------+------------+ 8 | | followee | follower | 9 | +-------------+------------+ 10 | | A | B | 11 | | B | C | 12 | | B | D | 13 | | D | E | 14 | +-------------+------------+ 15 | should output: 16 | 17 | +-------------+------------+ 18 | | follower | num | 19 | +-------------+------------+ 20 | | B | 2 | 21 | | D | 1 | 22 | +-------------+------------+ 23 | 24 | Select f1.follower, count(distinct f2.follower) as num 25 | from follow f1 26 | inner join follow f2 on f1.follower = f2.followee 27 | Group by f1.follower 28 | -------------------------------------------------------------------------------- /L615_Average Salary: Departments VS Company.sql: -------------------------------------------------------------------------------- 1 | Given two tables as below, write a query to display the comparison result (higher/lower/same) of the average salary of employees in a department to the company's average salary. 2 | 3 | 4 | Table: salary 5 | | id | employee_id | amount | pay_date | 6 | |----|-------------|--------|------------| 7 | | 1 | 1 | 9000 | 2017-03-31 | 8 | | 2 | 2 | 6000 | 2017-03-31 | 9 | | 3 | 3 | 10000 | 2017-03-31 | 10 | | 4 | 1 | 7000 | 2017-02-28 | 11 | | 5 | 2 | 6000 | 2017-02-28 | 12 | | 6 | 3 | 8000 | 2017-02-28 | 13 | 14 | 15 | The employee_id column refers to the employee_id in the following table employee. 16 | 17 | 18 | | employee_id | department_id | 19 | |-------------|---------------| 20 | | 1 | 1 | 21 | | 2 | 2 | 22 | | 3 | 2 | 23 | 24 | 25 | So for the sample data above, the result is: 26 | 27 | 28 | | pay_month | department_id | comparison | 29 | |-----------|---------------|-------------| 30 | | 2017-03 | 1 | higher | 31 | | 2017-03 | 2 | lower | 32 | | 2017-02 | 1 | same | 33 | | 2017-02 | 2 | same | 34 | 35 | Explain: 36 | In March, the company's average salary is (9000+6000+10000)/3 = 8333.33... 37 | The average salary for department '1' is 9000, which is the salary of employee_id '1' since there is only one employee in this department. So the comparison result is 'higher' since 9000 > 8333.33 obviously. 38 | The average salary of department '2' is (6000 + 10000)/2 = 8000, which is the average of employee_id '2' and '3'. So the comparison result is 'lower' since 8000 < 8333.33. 39 | With he same formula for the average salary comparison in February, the result is 'same' since both the department '1' and '2' have the same average salary with the company, which is 7000. 40 | 41 | Solution: 42 | // Method1 traditional writing two tables separately 43 | 44 | SELECT department_salary.pay_month, department_salary.department_id, 45 | (CASE WHEN department_salary.department_avg>company_salary.company_avg 46 | THEN 'higher' 47 | WHEN department_salary.department_avg AS (), AS () SELECT..... 68 | 69 | WITH department_salary AS ( 70 | SELECT e.department_id,date_format(s.pay_date,'%Y-%m') as pay_month,AVG(s.amount) as department_avg 71 | FROM Salary s 72 | JOIN employee e 73 | ON s.employee_id=e.employee_id 74 | GROUP BY e.department_id, date_format(s.pay_date,'%Y-%m')), 75 | company_salary AS ( 76 | SELECT date_format(pay_date,'%Y-%m') as pay_month, 77 | AVG(amount) as company_avg 78 | FROM Salary 79 | GROUP BY date_format(pay_date,'%Y-%m')) 80 | SELECT department_salary.pay_month, department_salary.department_id, 81 | (CASE WHEN department_salary.department_avg>company_salary.company_avg 82 | THEN 'higher' 83 | WHEN department_salary.department_avg AS (sql_subquery_statement) 6 | SELECT column_list FROM [,table_name] 7 | [WHERE ] 8 | 9 | * When using multiple sub-query aliases, the syntax is as follows. 10 | 11 | WITH AS (sql_subquery_statement), 12 | AS(sql_subquery_statement_from_alias_name_A 13 | or sql_subquery_statement ) 14 | SELECT 15 | FROM , [,table_names] 16 | [WHERE ] 17 | -------------------------------------------------------------------------------- /SQL_Functions/window_function_aggregation_cumulation.sql: -------------------------------------------------------------------------------- 1 | +------------+------------+----------+--------+ 2 | | account_id | day | type | amount | 3 | +------------+------------+----------+--------+ 4 | | 1 | 2021-11-07 | Deposit | 2000 | 5 | | 1 | 2021-11-09 | Withdraw | 1000 | 6 | | 1 | 2021-11-11 | Deposit | 3000 | 7 | | 2 | 2021-12-07 | Deposit | 7000 | 8 | | 2 | 2021-12-12 | Withdraw | 7000 | 9 | +------------+------------+----------+--------+ 10 | 11 | (1) aggregation function with partition by only 12 | 13 | select 14 | account_id, 15 | sum(amount) over (partition by account_id) as total_sum 16 | from transactions_2066 17 | 18 | +------------+-----------+ 19 | | account_id | total_sum | 20 | +------------+-----------+ 21 | | 1 | 6000 | 22 | | 1 | 6000 | 23 | | 1 | 6000 | 24 | | 2 | 14000 | 25 | | 2 | 14000 | 26 | +------------+-----------+ 27 | 28 | (2) aggregation function with over() without parameters 29 | 30 | select 31 | account_id, 32 | sum(amount) over () as total_sum 33 | from transactions_2066 34 | 35 | +------------+-----------+ 36 | | account_id | total_sum | 37 | +------------+-----------+ 38 | | 1 | 20000 | 39 | | 1 | 20000 | 40 | | 1 | 20000 | 41 | | 2 | 20000 | 42 | | 2 | 20000 | 43 | +------------+-----------+ 44 | 45 | (3) aggregation function with partition by and order by 46 | 47 | select 48 | account_id, 49 | sum(amount) over (partition by account_id order by day) as cumulative_sum 50 | from transactions_2066 51 | 52 | +------------+----------------+ 53 | | account_id | cumulative_sum | 54 | +------------+----------------+ 55 | | 1 | 2000 | 56 | | 1 | 3000 | 57 | | 1 | 6000 | 58 | | 2 | 7000 | 59 | | 2 | 14000 | 60 | +------------+----------------+ 61 | 62 | -------------------------------------------------------------------------------- /Second_High_Salary_In_Each_Dept.sql: -------------------------------------------------------------------------------- 1 | /* 2 | CoderPad provides a basic SQL sandbox with the following schema. 3 | You can also use commands like `show tables` and `desc employees` 4 | 5 | employees projects 6 | +---------------+---------+ +---------------+---------+ 7 | | id | int |<----+ +->| id | int | 8 | | first_name | varchar | | | | title | varchar | 9 | | last_name | varchar | | | | start_date | date | 10 | | salary | int | | | | end_date | date | 11 | | department_id | int |--+ | | | budget | int | 12 | +---------------+---------+ | | | +---------------+---------+ 13 | | | | 14 | departments | | | employees_projects 15 | +---------------+---------+ | | | +---------------+---------+ 16 | | id | int |<-+ | +--| project_id | int | 17 | | name | varchar | +-----| employee_id | int | 18 | +---------------+---------+ +---------------+---------+ 19 | */ 20 | 21 | 22 | 23 | SELECT tb1.* 24 | FROM employees tb1 25 | JOIN departments tb2 26 | ON tb1.department_id=tb2.id 27 | WHERE (select count(distinct(tb3.salary)) 28 | from employees tb3 29 | where tb3.salary > tb1.salary 30 | and tb1.department_id = tb3.department_id 31 | ) =1; 32 | -------------------------------------------------------------------------------- /Wayfair_Company_Export_Import.sql: -------------------------------------------------------------------------------- 1 | create table if not exists Companies(name varchar(100), nation varchar(100), PRIMARY KEY (name)); 2 | insert into Companies(name,nation) values ('Alice','Wonderland'); 3 | insert into Companies(name,nation) values ('Y-zap','Wonderland'); 4 | insert into Companies(name,nation) values ('Absolute','Mathland'); 5 | insert into Companies(name,nation) values ('Arcus','Mathland'); 6 | insert into Companies(name,nation) values ('Li','Underwater'); 7 | insert into Companies(name,nation) values ('None at all','Nothingland'); 8 | 9 | create table if not exists Trade(id integer, seller varchar(100), buyer varchar(100), value integer); 10 | insert into Trade(id,seller,buyer,value) values (20121107,'Li','Alice',10); 11 | insert into Trade(id,seller,buyer,value) values (20123112,'Arcus','Y-zap',30); 12 | insert into Trade(id,seller,buyer,value) values (20120125,'Alice','Arcus',100); 13 | insert into Trade(id,seller,buyer,value) values (20120216,'Li','Absolute',30); 14 | insert into Trade(id,seller,buyer,value) values (20120217,'Li','Absolute',50); 15 | 16 | select table1.country, table1.export, table2.import from 17 | (select c1.nation as country, coalesce(sum(t1.value),0) as export from 18 | Companies c1 left join Trade t1 19 | on c1.name=t1.seller 20 | group by 1 21 | ) table1 22 | join 23 | ( 24 | select c2.nation as country, coalesce(sum(t2.value),0) as import from 25 | Companies c2 left join Trade t2 26 | on c2.name=t2.buyer 27 | group by 1 28 | ) table2 29 | on table1.country=table2.country 30 | 31 | Answer: 32 | Country export import 33 | Mathland 30 180 34 | Nothingland 0 0 35 | Underwater 90 0 36 | Wonderland 100 40 37 | -------------------------------------------------------------------------------- /replace_null_value_with_value_previous_value.sql: -------------------------------------------------------------------------------- 1 | +------------+----------+ 2 | | mysequence | mynumber | 3 | +------------+----------+ 4 | | 1 | 3 | 5 | | 2 | NULL | 6 | | 3 | 5 | 7 | | 4 | NULL | 8 | | 5 | NULL | 9 | | 6 | 2 | 10 | +------------+----------+ 11 | 12 | Result: 13 | 14 | +------------+----------+------------+ 15 | | mysequence | ORIGINAL | CALCULATED | 16 | +------------+----------+------------+ 17 | | 1 | 3 | 3 | 18 | | 2 | NULL | 3 | 19 | | 3 | 5 | 5 | 20 | | 4 | NULL | 5 | 21 | | 5 | NULL | 5 | 22 | | 6 | 2 | 2 | 23 | +------------+----------+------------+ 24 | 25 | # Replace NULL value in a row with a value from the previous known value 26 | 27 | 28 | /*CREATE TABLE test(mysequence INT, mynumber INT); 29 | 30 | INSERT INTO test VALUES(1, 3); 31 | INSERT INTO test VALUES(2, NULL); 32 | INSERT INTO test VALUES(3, 5); 33 | INSERT INTO test VALUES(4, NULL); 34 | INSERT INTO test VALUES(5, NULL); 35 | INSERT INTO test VALUES(6, 2); 36 | */ 37 | 38 | select * from test; 39 | 40 | 41 | with cte_step1 as ( 42 | select mysequence 43 | , mynumber 44 | , sum(case when mynumber is NULL then 0 else 1 end) 45 | over (order by mysequence) as val 46 | from test 47 | ) 48 | select mysequence 49 | , mynumber 50 | , first_value(mynumber) 51 | over (partition by val order by mysequence) as res 52 | from cte_step1 53 | --------------------------------------------------------------------------------