-- Table: Events
--+---------------+---------+--| Column Name | Type |--+---------------+---------+--| business_id |int|--| event_type | varchar |--| occurences |int|--+---------------+---------+-(business_id,event_type)是此表的主键。
-表中的每一行记录了在某项业务中多次发生某种类型的事件的信息。
-编写SQL查询以查找所有活跃企业。
-活跃的企业是指具有多个事件类型且其发生次数大于所有企业中该事件类型的平均发生次数的企业。
-查询结果格式如下例所示:
-- Events table:--+-------------+------------+------------+--| business_id | event_type | occurences |--+-------------+------------+------------+--|1| reviews |7|--|3| reviews |3|--|1| ads |11|--|2| ads |7|--|3| ads |6|--|1| page views |3|--|2| page views |12|--+-------------+------------+------------+-- Result table:--+-------------+--| business_id |--+-------------+--|1|--+-------------+-- Average for'reviews','ads' and 'page views' are (7+3)/2=5,(11+7+6)/3=8,(3+12)/2=7.5 respectively.-- Business with id 1 has 7'reviews' events (more than 5) and 11'ads' events (more than 8) so it is an active business.
select c.business_id
from(
select *
from events e
join
(select round(avg(occurence),2) as average from Events groupby event_type) b
on e.event_type= b.event_type) c
whhere c.occurence>=c.average
groupby c.business_id
having count(*)>1
2 Active Users.sql
-- Table Accounts:--+---------------+---------+--| Column Name | Type |--+---------------+---------+--| id |int|--| name | varchar |--+---------------+---------+-- the id is the primary key forthis table.-- This table contains the account id and the user name of each account.-- Table Logins:--+---------------+---------+--| Column Name | Type |--+---------------+---------+--| id |int|--| login_date | date |--+---------------+---------+-- There is no primary key forthis table, it may contain duplicates.-- This table contains the account id of the user who logged in and the login date. A user may log in multiple times in the day.-- Write an SQL query to find the id and the name of active users.-活跃用户是指连续5天或以上登录其帐户的用户。
-- Return the result table ordered by the id.-- The query result format is in the following example:-- Accounts table:--+----+----------+--| id | name |--+----+----------+--|1| Winston |--|7| Jonathan |--+----+----------+-- Logins table:--+----+------------+--| id | login_date |--+----+------------+--|7|2020-05-30|--|1|2020-05-30|--|7|2020-05-31|--|7|2020-06-01|--|7|2020-06-02|--|7|2020-06-02|--|7|2020-06-03|--|1|2020-06-07|--|7|2020-06-10|--+----+------------+-- Result table:--+----+----------+--| id | name |--+----+----------+--|7| Jonathan |--+----+----------+-- User Winston with id =1 logged in 2 times only in 2 different days, so, Winston is not an active user.-- User Jonathan with id =7 logged in 7 times in 6 different days, five of them were consecutive days, so, Jonathan is an active user.
1.
select c.id accounts.name
(select b.id
from(select a.id,date_sub(DATE_FORMAT(a.login_date,"%Y-%m-%d"),a.rank) as startday
from(select id, login_date,rank()over( partition by id sort by login_date) as rank
from logins)a ) b
group by b.id,b.startday
having count(*)>=5) c join accounts on c.id=accounts.id
)2.
with t1 as (
select id,login_date,lead(login_date,4)over(partition by id order by login_date) date_5
from (select distinct * from Logins) b
)
select distinct a.id, a.name from t1
inner join accounts a
on t1.id = a.id
where datediff(t1.date_5,login_date)=4
order by id
3 Activity Participants.sql
-- Table: Friends
--+---------------+---------+--| Column Name | Type |--+---------------+---------+--| id |int|--| name | varchar |--| activity | varchar |--+---------------+---------+-- id is the id of the friend and primary key forthis table.-- name is the name of the friend.-- activity is the name of the activity which the friend takes part in.-- Table: Activities
--+---------------+---------+--| Column Name | Type |--+---------------+---------+--| id |int|--| name | varchar |--+---------------+---------+-- id is the primary key forthis table.-- name is the name of the activity.-编写一个SQL查询来查找所有活动的名称,这些活动的参加人数既没有最大值也没有最小值。
-以任何顺序返回结果表。 表“活动”中的每个活动均由“朋友”表中的任何人执行。
-- The query result format is in the following example:-- Friends table:--+------+--------------+---------------+--| id | name | activity |--+------+--------------+---------------+--|1| Jonathan D.| Eating |--|2| Jade W.| Singing |--|3| Victor J.| Singing |--|4| Elvis Q.| Eating |--|5| Daniel A.| Eating |--|6| Bob B.| Horse Riding |--+------+--------------+---------------+-- Activities table:--+------+--------------+--| id | name |--+------+--------------+--|1| Eating |--|2| Singing |--|3| Horse Riding |--+------+--------------+-- Result table:--+--------------+--| activity |--+--------------+--| Singing |--+--------------+-- Eating activity is performed by 3 friends, maximum number of participants,(Jonathan D., Elvis Q. and Daniel A.)-- Horse Riding activity is performed by 1 friend, minimum number of participants,(Bob B.)-- Singing is performed by 2 friends (Victor J. and Jade W.)
with t1 as(
select max(a.total) as total
from(
select activity,count(*) as total
from friends
group by activity) a
union all
select min(b.total) as low
from(
select activity,count(*) as total
from friends
group by activity) b),
t2 as
(
select activity,count(*) as total
from friends
group by activity
)
select activity
from t1 right join t2
on t1.total = t2.total
where t1.total is null
4 All people report to the given manager.sql
-- Table: Employees
--+---------------+---------+--| Column Name | Type |--+---------------+---------+--| employee_id |int|--| employee_name | varchar |--| manager_id |int|--+---------------+---------+-- employee_id is the primary key forthis table.-- Each row of this table indicates that the employee with ID employee_id and name employee_name reports his
-- work to his/her direct manager with manager_id
-- The head of the company is the employee with employee_id =1.-编写SQL查询以查找直接或间接向公司负责人(employee_id =1)报告其工作的所有雇员的employee_id。
-由于公司规模较小,经理之间的间接关系将不超过3名经理。
-以任何顺序返回结果表,没有重复项。
-- The query result format is in the following example:-- Employees table:--+-------------+---------------+------------+--| employee_id | employee_name | manager_id |--+-------------+---------------+------------+--|1| Boss |1|--|3| Alice |3|--|2| Bob |1|--|4| Daniel |2|--|7| Luis |4|--|8| Jhon |3|--|9| Angela |8|--|77| Robert |1|--+-------------+---------------+------------+-- Result table:--+-------------+--| employee_id |--+-------------+--|2|--|77|--|4|--|7|--+-------------+-- The head of the company is the employee with employee_id 1.-- The employees with employee_id 2 and 77 report their work directly to the head of the company.-- The employee with employee_id 4 report his work indirectly to the head of the company 4-->2-->1.-- The employee with employee_id 7 report his work indirectly to the head of the company 7-->4-->2-->1.-- The employees with employee_id 3,8 and 9 don't report their work to head of company directly or indirectly.
select employee_id
from employees
where manager_id =1 and employee_id !=1
union
select employee_id
from employees
where manager_id = any (select employee_id
from employees
where manager_id =1 and employee_id !=1)
union
select employee_id
from employees
where manager_id = any (select employee_id
from employees
where manager_id = any (select employee_id
from employees
where manager_id =1 and employee_id !=1))
TODO 也就是一层一层往下找
5 Apples & Oranges.sql
-- Question 66-- Table: Sales
--+---------------+---------+--| Column Name | Type |--+---------------+---------+--| sale_date | date |--| fruit |enum|--| sold_num |int|--+---------------+---------+--(sale_date,fruit) is the primary key forthis table.-- This table contains the sales of "apples" and "oranges" sold each day.-编写SQL查询以报告每天售出的苹果和橙子数量之间的差异。
-以格式('YYYY-MM-DD')返回由sale_date排序的结果表。
-- The query result format is in the following example:-- Sales table:--+------------+------------+-------------+--| sale_date | fruit | sold_num |--+------------+------------+-------------+--|2020-05-01| apples |10|--|2020-05-01| oranges |8|--|2020-05-02| apples |15|--|2020-05-02| oranges |15|--|2020-05-03| apples |20|--|2020-05-03| oranges |0|--|2020-05-04| apples |15|--|2020-05-04| oranges |16|--+------------+------------+-------------+-- Result table:--+------------+--------------+--| sale_date | diff |--+------------+--------------+--|2020-05-01|2|--|2020-05-02|0|--|2020-05-03|20|--|2020-05-04|-1|--+------------+--------------+-- Day 2020-05-01,10 apples and 8 oranges were sold (Difference 10-8=2).-- Day 2020-05-02,15 apples and 15 oranges were sold (Difference 15-15=0).-- Day 2020-05-03,20 apples and 0 oranges were sold (Difference 20-0=20).-- Day 2020-05-04,15 apples and 16 oranges were sold (Difference 15-16=-1).
Select sale_date, sold_num-sold as diff
from
((select *
from sales
where fruit ='apples') a
join
(select sale_date as sale, fruit, sold_num as sold
from sales
where fruit ='oranges') b
on a.sale_date = b.sale)
6 Article Views 2.sql
-- Table: Views
--+---------------+---------+--| Column Name | Type |--+---------------+---------+--| article_id |int|--| author_id |int|--| viewer_id |int|--| view_date | date |--+---------------+---------+-- There is no primary key forthis table, it may have duplicate rows.-- Each row of this table indicates that some viewer viewed an article (written by some author) on some date.-- Note that equal author_id and viewer_id indicate the same person.-编写SQL查询来查找在同一日期观看过一篇以上文章的所有人,并按ID升序排列。
-- The query result format is in the following example:-- Views table:--+------------+-----------+-----------+------------+--| article_id | author_id | viewer_id | view_date |--+------------+-----------+-----------+------------+--|1|3|5|2019-08-01|--|3|4|5|2019-08-01|--|1|3|6|2019-08-02|--|2|7|7|2019-08-01|--|2|7|6|2019-08-02|--|4|7|1|2019-07-22|--|3|4|4|2019-07-21|--|3|4|4|2019-07-21|--+------------+-----------+-----------+------------+-- Result table:--+------+--| id |--+------+--|5|--|6|--+------+
-- Solution
select distinct viewer_id as id,count(distinct article_id) as total
from views
group by viewer_id, view_date
having count(distinct article_id)>1
order by 1
7 Calculate Salaries.sql
-- Question 74-- Table Salaries:--+---------------+---------+--| Column Name | Type |--+---------------+---------+--| company_id |int|--| employee_id |int|--| employee_name | varchar |--| salary |int|--+---------------+---------+--(company_id, employee_id) is the primary key forthis table.-- This table contains the company id, the id, the name and the salary for an employee.-编写SQL查询以查询加税后的员工工资。
-根据以下标准为每个公司计算税率:
-0%,如果公司中任何员工的最高工资低于1000 $。
-24%,如果公司中任何员工的最高薪水在[1000,10000](含)之间。
-49%(如果公司中任何员工的最高薪水超过10000 $)。
-以任何顺序返回结果表。 将薪水四舍五入到最接近的整数。
-- The query result format is in the following example:-- Salaries table:--+------------+-------------+---------------+--------+--| company_id | employee_id | employee_name | salary |--+------------+-------------+---------------+--------+--|1|1| Tony |2000|--|1|2| Pronub |21300|--|1|3| Tyrrox |10800|--|2|1| Pam |300|--|2|7| Bassem |450|--|2|9| Hermione |700|--|3|7| Bocaben |100|--|3|2| Ognjen |2200|--|3|13| Nyancat |3300|--|3|15| Morninngcat |1866|--+------------+-------------+---------------+--------+-- Result table:--+------------+-------------+---------------+--------+--| company_id | employee_id | employee_name | salary |--+------------+-------------+---------------+--------+--|1|1| Tony |1020|--|1|2| Pronub |10863|--|1|3| Tyrrox |5508|--|2|1| Pam |300|--|2|7| Bassem |450|--|2|9| Hermione |700|--|3|7| Bocaben |76|--|3|2| Ognjen |1672|--|3|13| Nyancat |2508|--|3|15| Morninngcat |5911|--+------------+-------------+---------------+--------+-- For company 1, Max salary is 21300. Employees in company 1 have taxes =49%-- For company 2, Max salary is 700. Employees in company 2 have taxes =0%-- For company 3, Max salary is 7777. Employees in company 3 have taxes =24%-- The salary after taxes = salary -(taxes percentage /100)* salary
-- For example, Salary for Morninngcat (3,15) after taxes =7777-7777*(24/100)=7777-1866.48=5910.52, which is rounded to 5911.
-- Solution
with t1 as (
select company_id, employee_id, employee_name, salary as sa,max(salary)over(partition by company_id) as maximum
from salaries)
select company_id, employee_id, employee_name,case when t1.maximum<1000 then t1.sa
when t1.maximum between 1000 and 10000 then round(t1.sa*.76,0)elseround(t1.sa*.51,0)
end as salary
from t1
8 Capital Gain.sql
-- Table: Stocks
--+---------------+---------+--| Column Name | Type |--+---------------+---------+--| stock_name | varchar |--| operation |enum|--| operation_day |int|--| price |int|--+---------------+---------+--(stock_name, day) is the primary key forthis table.-- The operation column is an ENUM of type ('Sell','Buy')-- Each row of this table indicates that the stock which has stock_name had an operation on the day operation_day with the price.-- It is guaranteed that each 'Sell' operation for a stock has a corresponding 'Buy' operation in a previous day.-编写SQL查询以报告每只股票的资本损益。
-股票的资本损益是买卖股票一次或多次后的总损益。
-以任何顺序返回结果表。
-- The query result format is in the following example:-- Stocks table:--+---------------+-----------+---------------+--------+--| stock_name | operation | operation_day | price |--+---------------+-----------+---------------+--------+--| Leetcode | Buy |1|1000|--| Corona Masks | Buy |2|10|--| Leetcode | Sell |5|9000|--| Handbags | Buy |17|30000|--| Corona Masks | Sell |3|1010|--| Corona Masks | Buy |4|1000|--| Corona Masks | Sell |5|500|--| Corona Masks | Buy |6|1000|--| Handbags | Sell |29|7000|--| Corona Masks | Sell |10|10000|--+---------------+-----------+---------------+--------+-- Result table:--+---------------+-------------------+--| stock_name | capital_gain_loss |--+---------------+-------------------+--| Corona Masks |9500|--| Leetcode |8000|--| Handbags |-23000|--+---------------+-------------------+-- Leetcode stock was bought at day 1for1000$ and was sold at day 5for9000$. Capital gain =9000-1000=8000$.-- Handbags stock was bought at day 17for30000$ and was sold at day 29for7000$. Capital loss =7000-30000=-23000$.-- Corona Masks stock was bought at day 1for10$ and was sold at day 3for1010$. It was bought again at day 4for1000$ and was sold at day 5for500$. At last, it was bought at day 6for1000$ and was sold at day 10for10000$. Capital gain/loss is the sum of capital gains/losses for each ('Buy'-->'Sell')-- operation =(1010-10)+(500-1000)+(10000-1000)=1000-500+9000=9500$.
-- Solution
select stock_name,(one-two) as capital_gain_loss
from((select stock_name,sum(price) as one
from stocks
where operation ='Sell'
group by stock_name) b
left join
(select stock_name as name,sum(price) as two
from stocks
where operation ='Buy'
group by stock_name) c
on b.stock_name = c.name)
order by capital_gain_loss desc
9 Consecutive Numbers.sql
-编写SQL查询以查找至少连续出现三次的所有数字。
--+----+-----+--| Id | Num |--+----+-----+--|1|1|--|2|1|--|3|1|--|4|2|--|5|1|--|6|2|--|7|2|--+----+-----+-- For example, given the above Logs table,1 is the only number that appears consecutively for at least three times.--+-----------------+--| ConsecutiveNums |--+-----------------+--|1|--+-----------------+
-- Solution
select distinct a.num as ConsecutiveNums
from(
select *,lag(num)over() as prev,lead(num)over() as next
from logs) a
where a.num = a.prev and a.num=a.next
9 Count student number in departments.sql
-- A university uses 2 data tables, student and department, to store data about its students
-- and the departments associated with each major.-编写查询以打印各自的部门名称和每个专业的学生人数
-部门表中包含所有部门(甚至没有当前学生的部门)。
-按学生人数递减的顺序对结果进行排序; 如果两个或两个以上部门的学生人数相同,
-然后按部门名称的字母顺序对这些部门进行排序。
-- The student is described as follow:--| Column Name | Type |--|--------------|-----------|--| student_id | Integer |--| student_name | String |--| gender | Character |--| dept_id | Integer |-- where student_id is the student's ID number, student_name is the student's name, gender is their gender, and dept_id is the department ID associated with their declared major.-- And the department table is described as below:--| Column Name | Type |--|-------------|---------|--| dept_id | Integer |--| dept_name | String |-- where dept_id is the department's ID number and dept_name is the department name.-- Here is an example input:-- student table:--| student_id | student_name | gender | dept_id |--|------------|--------------|--------|---------|--|1| Jack | M |1|--|2| Jane | F |1|--|3| Mark | M |2|-- department table:--| dept_id | dept_name |--|---------|-------------|--|1| Engineering |--|2| Science |--|3| Law |-- The Output should be:--| dept_name | student_number |--|-------------|----------------|--| Engineering |2|--| Science |1|--| Law |0|
-- Solution
select dept_name,count(s.dept_id) as student_number
from department d
left join student s
on d.dept_id = s.dept_id
group by d.dept_id
order by count(s.dept_id) desc, dept_name
10 Countries you can safely invest in.sql
-- Table Person:--+----------------+---------+--| Column Name | Type |--+----------------+---------+--| id |int|--| name | varchar |--| phone_number | varchar |--+----------------+---------+-- id is the primary key forthis table.-- Each row of this table contains the name of a person and their phone number.-- Phone number will be in the form 'xxx-yyyyyyy' where xxx is the country code (3 characters) and yyyyyyy is the
-- phone number (7 characters) where x and y are digits. Both can contain leading zeros.-- Table Country:--+----------------+---------+--| Column Name | Type |--+----------------+---------+--| name | varchar |--| country_code | varchar |--+----------------+---------+-- country_code is the primary key forthis table.-- Each row of this table contains the country name and its code. country_code will be in the form 'xxx' where x is digits.-- Table Calls:--+-------------+------+--| Column Name | Type |--+-------------+------+--| caller_id |int|--| callee_id |int|--| duration |int|--+-------------+------+-- There is no primary key forthis table, it may contain duplicates.-- Each row of this table contains the caller id, callee id and the duration of the call in minutes. caller_id != callee_id
-一家电信公司希望在新国家投资。 该国家打算在该国家的平均通话时长严格大于全球平均通话时长的国家进行投资。
-编写SQL查询以查找该公司可以投资的国家。
-以任何顺序返回结果表。
-- The query result format is in the following example.-- Person table:--+----+----------+--------------+--| id | name | phone_number |--+----+----------+--------------+--|3| Jonathan |051-1234567|--|12| Elvis |051-7654321|--|1| Moncef |212-1234567|--|2| Maroua |212-6523651|--|7| Meir |972-1234567|--|9| Rachel |972-0011100|--+----+----------+--------------+-- Country table:--+----------+--------------+--| name | country_code |--+----------+--------------+--| Peru |051|--| Israel |972|--| Morocco |212|--| Germany |049|--| Ethiopia |251|--+----------+--------------+-- Calls table:--+-----------+-----------+----------+--| caller_id | callee_id | duration |--+-----------+-----------+----------+--|1|9|33|--|2|9|4|--|1|2|59|--|3|12|102|--|3|12|330|--|12|3|5|--|7|9|13|--|7|1|3|--|9|7|1|--|1|7|7|--+-----------+-----------+----------+-- Result table:--+----------+--| country |--+----------+--| Peru |--+----------+-- The average call duration for Peru is (102+102+330+330+5+5)/6=145.666667-- The average call duration for Israel is (33+4+13+13+3+1+1+7)/8=9.37500-- The average call duration for Morocco is (33+4+59+59+3+7)/6=27.5000-- Global call duration average =(2*(33+4+59+102+330+5+13+3+1+7))/20=55.70000-- Since Peru is the only country where average call duration is greater than the global average, it's the only recommended country.
-- Solution
with t1 as(
select caller_id as id, duration as total
from
(select caller_id, duration
from calls
union all
select callee_id, duration
from calls) a
)
select name as country
from
(select distinct avg(total)over(partition by code) as avg_call,avg(total)over() as global_avg, c.name
from
((
select *,coalesce(total,0) as duration,substring(phone_number from 1for3) as code
from person right join t1 using (id)) b
join country c on c.country_code = b.code)) d
where avg_call > global_avg
11 Customers who bought a, b but not c.sql
-- Table: Customers
--+---------------------+---------+--| Column Name | Type |--+---------------------+---------+--| customer_id |int|--| customer_name | varchar |--+---------------------+---------+-- customer_id is the primary key forthis table.-- customer_name is the name of the customer.-- Table: Orders
--+---------------+---------+--| Column Name | Type |--+---------------+---------+--| order_id |int|--| customer_id |int|--| product_name | varchar |--+---------------+---------+-- order_id is the primary key forthis table.-- customer_id is the id of the customer who bought the product "product_name".-编写一个SQL查询以报告购买了产品“ A”,“ B”但未购买产品“ C”的顾客的customer_id和customer_name,因为我们要建议他们购买该产品。
-返回由customer_id排序的结果表。
-- The query result format is in the following example.-- Customers table:--+-------------+---------------+--| customer_id | customer_name |--+-------------+---------------+--|1| Daniel |--|2| Diana |--|3| Elizabeth |--|4| Jhon |--+-------------+---------------+-- Orders table:--+------------+--------------+---------------+--| order_id | customer_id | product_name |--+------------+--------------+---------------+--|10|1| A |--|20|1| B |--|30|1| D |--|40|1| C |--|50|2| A |--|60|3| A |--|70|3| B |--|80|3| D |--|90|4| C |--+------------+--------------+---------------+-- Result table:--+-------------+---------------+--| customer_id | customer_name |--+-------------+---------------+--|3| Elizabeth |--+-------------+---------------+-- Only the customer_id with id 3 bought the product A and B but not the product C.
-- Solution
with t1 as
(
select customer_id
from orders
where product_name ='B' and
customer_id in (select customer_id
from orders
where product_name ='A'))
Select t1.customer_id, c.customer_name
from t1 join customers c
on t1.customer_id = c.customer_id
where t1.customer_id !=all(select customer_id
from orders
where product_name ='C')
12 Customers who bought all products.sql
-- Table: Customer
--+-------------+---------+--| Column Name | Type |--+-------------+---------+--| customer_id |int|--| product_key |int|--+-------------+---------+-- product_key is a foreign key to Product table.-- Table: Product
--+-------------+---------+--| Column Name | Type |--+-------------+---------+--| product_key |int|--+-------------+---------+-- product_key is the primary key column forthis table.-为报告编写SQL查询,该报告提供从“客户”表中购买了“产品”表中所有产品的客户ID。
-- For example:-- Customer table:--+-------------+-------------+--| customer_id | product_key |--+-------------+-------------+--|1|5|--|2|6|--|3|5|--|3|6|--|1|6|--+-------------+-------------+-- Product table:--+-------------+--| product_key |--+-------------+--|5|--|6|--+-------------+-- Result table:--+-------------+--| customer_id |--+-------------+--|1|--|3|--+-------------+-- The customers who bought all the products (5 and 6) are customers with id 1 and 3.
-- Solution
select customer_id
from customer
group by customer_id
having count(distinct product_key)=(select COUNT(distinct product_key) from product)
13 Department Highest Salary.sql
-- The Employee table holds all employees. Every employee has an Id, a salary, and there is also a column for the department Id.--+----+-------+--------+--------------+--| Id | Name | Salary | DepartmentId |--+----+-------+--------+--------------+--|1| Joe |70000|1|--|2| Jim |90000|1|--|3| Henry |80000|2|--|4| Sam |60000|2|--|5| Max |90000|1|--+----+-------+--------+--------------+-- The Department table holds all departments of the company.--+----+----------+--| Id | Name |--+----+----------+--|1| IT |--|2| Sales |--+----+----------+-编写SQL查询以查找每个部门中薪水最高的员工。
-对于上述表,您的SQL查询应返回以下行(行的顺序无关紧要)。
--+------------+----------+--------+--| Department | Employee | Salary |--+------------+----------+--------+--| IT | Max |90000|--| IT | Jim |90000|--| Sales | Henry |80000|--+------------+----------+--------+-- Explanation:-- Max and Jim both have the highest salary in the IT department and Henry has the highest salary in the Sales department.
-- Solution
select a.Department, a.Employee, a.Salary
from(
select d.name as Department, e.name as Employee, Salary,rank()over(partition by d.name order by salary desc) as rk
from employee e
join department d
on e.departmentid = d.id) a
where a.rk=1
14 Evaluate Boolean Expressions.sql
-- Table Variables:--+---------------+---------+--| Column Name | Type |--+---------------+---------+--| name | varchar |--| value |int|--+---------------+---------+-- name is the primary key forthis table.-- This table contains the stored variables and their values.-- Table Expressions:--+---------------+---------+--| Column Name | Type |--+---------------+---------+--| left_operand | varchar |--| operator |enum|--| right_operand | varchar |--+---------------+---------+--(left_operand, operator, right_operand) is the primary key forthis table.-- This table contains a boolean expression that should be evaluated.-- operator is an enum that takes one of the values ('<','>','=')-- The values of left_operand and right_operand are guaranteed to be in the Variables table.-编写一个SQL查询以评估Expressions表中的布尔表达式。
-以任何顺序返回结果表。
-- The query result format is in the following example.-- Variables table:--+------+-------+--| name | value |--+------+-------+--| x |66|--| y |77|--+------+-------+-- Expressions table:--+--------------+----------+---------------+--| left_operand | operator | right_operand |--+--------------+----------+---------------+--| x |>| y |--| x |<| y |--| x |=| y |--| y |>| x |--| y |<| x |--| x |=| x |--+--------------+----------+---------------+-- Result table:--+--------------+----------+---------------+-------+--| left_operand | operator | right_operand | value |--+--------------+----------+---------------+-------+--| x |>| y |false|--| x |<| y |true|--| x |=| y |false|--| y |>| x |true|--| y |<| x |false|--| x |=| x |true|--+--------------+----------+---------------+-------+-- As shown, you need find the value of each boolean exprssion in the table using the variables table.
-- Solution
with t1 as(
select e.left_operand, e.operator, e.right_operand, v.value as left_val, v_1.value as right_val
from expressions e
join variables v
on v.name = e.left_operand
join variables v_1
on v_1.name = e.right_operand)
select t1.left_operand, t1.operator, t1.right_operand,case when t1.operator ='<' then (select t1.left_val< t1.right_val)
when t1.operator ='>' then (select t1.left_val > t1.right_val)
when t1.operator ='=' then (select t1.left_val = t1.right_val)else FALSE
END AS VALUE
from t1
15 Exchange Seats.sql
-- Question 56-- Mary is a teacher in a middle school and she has a table seat storing students' names and their corresponding seat ids.-- The column id is continuous increment.
玛丽想为相邻的学生换座位。
-您可以编写SQL查询来为Mary输出结果吗?
--+---------+---------+--| id | student |--+---------+---------+--|1| Abbot |--|2| Doris |--|3| Emerson |--|4| Green |--|5| Jeames |--+---------+---------+-- For the sample input, the output is:--+---------+---------+--| id | student |--+---------+---------+--|1| Doris |--|2| Abbot |--|3| Green |--|4| Emerson |--|5| Jeames |--+---------+---------+
select row_number() over (order by (if(id%2=1,id+1,id-1))) as id, student
from seat
16 Find the start and end number of continuous ranges.sql
-- Question 80-- Table: Logs
--+---------------+---------+--| Column Name | Type |--+---------------+---------+--| log_id |int|--+---------------+---------+-- id is the primary key forthis table.-- Each row of this table contains the ID in a log Table.-由于某些ID已从日志中删除。 编写SQL查询以在表Logs中查找连续范围的开始和结束数目。
-按start_id排序结果表。
-- The query result format is in the following example:-- Logs table:--+------------+--| log_id |--+------------+--|1|--|2|--|3|--|7|--|8|--|10|--+------------+-- Result table:--+------------+--------------+--| start_id | end_id |--+------------+--------------+--|1|3|--|7|8|--|10|10|--+------------+--------------+-- The result table should contain all ranges in table Logs.-- From 1 to 3 is contained in the table.-- From 4 to 6 is missing in the table
-- From 7 to 8 is contained in the table.-- Number 9 is missing in the table.-- Number 10 is contained in the table.
-- Solution
select min(log_id) as start_id,max(log_id) as end_id
from(
select log_id, log_id-row_number() over (order by log_id) as rk
from logs) a
group by rk
17 Friend Requests 2.sql
-- Question 60-- In social network like Facebook or Twitter, people send friend requests and accept others' requests as well.-- Table request_accepted
--+--------------+-------------+------------+--| requester_id | accepter_id | accept_date|--|--------------|-------------|------------|--|1|2|2016-06-03|--|1|3|2016-06-08|--|2|3|2016-06-08|--|3|4|2016-06-09|--+--------------+-------------+------------+-- This table holds the data of friend acceptance,while requester_id and accepter_id both are the id of a person.-- 根据以下规则编写查询以查找拥有最多朋友和拥有最多朋友数的人:
-- It is guaranteed there is only 1 people having the most friends.-- The friend request could only been accepted once, which mean there is no multiple records with the same requester_id and accepter_id value.-- For the sample data above, the result is:-- Result table:--+------+------+--| id | num |--|------|------|--|3|3|--+------+------+-- The person with id '3' is a friend of people '1','2' and '4', so he has 3 friends in total, which is the most number than any others.
-- Solution
select requester_id as id, b.total as num
from(
select requester_id,sum(one) as total
from((select requester_id,count(distinct accepter_id) as one
from request_accepted
group by requester_id)
union all
(select accepter_id,count(distinct requester_id) as two
from request_accepted
group by accepter_id)) a
group by requester_id
order by total desc) b
limit 1
18 Game Play Analysis 3.sql
-- Question 62-- Table: Activity
--+--------------+---------+--| Column Name | Type |--+--------------+---------+--| player_id |int|--| device_id |int|--| event_date | date |--| games_played |int|--+--------------+---------+--(player_id, event_date) is the primary key of this table.-- This table shows the activity of players of some game.-- 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.-编写一个SQL查询,以报告每个玩家和日期,该玩家到目前为止玩了多少游戏。 即,该日期之前玩家所玩的游戏总数。 检查示例是否清晰。
-- The query result format is in the following example:-- Activity table:--+-----------+-----------+------------+--------------+--| player_id | device_id | event_date | games_played |--+-----------+-----------+------------+--------------+--|1|2|2016-03-01|5|--|1|2|2016-05-02|6|--|1|3|2017-06-25|1|--|3|1|2016-03-02|0|--|3|4|2018-07-03|5|--+-----------+-----------+------------+--------------+-- Result table:--+-----------+------------+---------------------+--| player_id | event_date | games_played_so_far |--+-----------+------------+---------------------+--|1|2016-03-01|5|--|1|2016-05-02|11|--|1|2017-06-25|12|--|3|2016-03-02|0|--|3|2018-07-03|5|--+-----------+------------+---------------------+-- 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.-- For the player with id 3,0+5=5 games played by 2018-07-03.-- Note that for each player we only care about the days when the player logged in.
-- Solution
select player_id, event_date,sum(games_played)over(partition by player_id order by event_date rows between unbounded preceding and current row) as games_played_so_far
from activity
order by 1,2
19 Game Play Analysis 4.sql
-- Question 91-- Table: Activity
--+--------------+---------+--| Column Name | Type |--+--------------+---------+--| player_id |int|--| device_id |int|--| event_date | date |--| games_played |int|--+--------------+---------+--(player_id, event_date) is the primary key of this table.-- This table shows the activity of players of some game.-- 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.-编写一个SQL查询报告再次登录的玩家比例
-在他们首次登录后的第二天,四舍五入到小数点后两位。
-换句话说,您需要计算至少连续两次登录的玩家数量
-从他们的第一个登录日期开始的天数,然后用该数字除以玩家总数。
-- The query result format is in the following example:-- Activity table:--+-----------+-----------+------------+--------------+--| player_id | device_id | event_date | games_played |--+-----------+-----------+------------+--------------+--|1|2|2016-03-01|5|--|1|2|2016-03-02|6|--|2|3|2017-06-25|1|--|3|1|2016-03-02|0|--|3|4|2018-07-03|5|--+-----------+-----------+------------+--------------+-- Result table:--+-----------+--| fraction |--+-----------+--|0.33|--+-----------+-- 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
-- Solution
With t as
(select player_id,min(event_date)over(partition by player_id) as min_event_date,case when event_date-min(event_date)over(partition by player_id)=1 then 1else0
end as s
from Activity)
select round(sum(t.s)/count(distinct t.player_id),2) as fraction
from t
20 Get highest answer rate question.sql
-- Question 86-- Get the highest answer rate question from a table survey_log with these columns: id, action, question_id, answer_id, q_num, timestamp.-- id 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.-编写sql查询以识别答案率最高的问题。
-- Example:-- Input:--+------+-----------+--------------+------------+-----------+------------+--| id | action | question_id | answer_id | q_num | timestamp |--+------+-----------+--------------+------------+-----------+------------+--|5| show |285| null |1|123|--|5| answer |285|124124|1|124|--|5| show |369| null |2|125|--|5| skip |369| null |2|126|--+------+-----------+--------------+------------+-----------+------------+-- Output:--+-------------+--| survey_log |--+-------------+--|285|--+-------------+-- Explanation:-- question 285 has answer rate 1/1,while question 369 has 0/1 answer rate, so output 285.-- Note: The highest answer rate meaning is: answer number's ratio in show number in the same question.
-- Solution
with t1 as(
select a.question_id,coalesce(b.answer/a.show_1,0) as rate
from
(select question_id,coalesce(count(*),0) as show_1
from survey_log
where action !='answer'
group by question_id) a
left join
(select question_id,coalesce(count(*),0) as answer
from survey_log
where action ='answer'
group by question_id) b
on a.question_id = b.question_id)
select a.question_id as survey_log
from
( select t1.question_id,rank()over(order by rate desc) as rk
from t1) a
where a.rk =1
21 Get the second most recent activity.sql
-- Question 109-- Table: UserActivity
--+---------------+---------+--| Column Name | Type |--+---------------+---------+--| username | varchar |--| activity | varchar |--| startDate | Date |--| endDate | Date |--+---------------+---------+-- This table does not contain primary key.-- This table contain information about the activity performed of each user in a period of time.-- A person with username performed a activity from startDate to endDate.-编写SQL查询以显示每个用户的第二最新活动。
-- If the user only has one activity,return that one.-- A user can't perform more than one activity at the same time. Return the result table in any order.-- The query result format is in the following example:-- UserActivity table:--+------------+--------------+-------------+-------------+--| username | activity | startDate | endDate |--+------------+--------------+-------------+-------------+--| Alice | Travel |2020-02-12|2020-02-20|--| Alice | Dancing |2020-02-21|2020-02-23|--| Alice | Travel |2020-02-24|2020-02-28|--| Bob | Travel |2020-02-11|2020-02-18|--+------------+--------------+-------------+-------------+-- Result table:--+------------+--------------+-------------+-------------+--| username | activity | startDate | endDate |--+------------+--------------+-------------+-------------+--| Alice | Dancing |2020-02-21|2020-02-23|--| Bob | Travel |2020-02-11|2020-02-18|--+------------+--------------+-------------+-------------+-- The most recent activity of Alice is Travel from 2020-02-24 to 2020-02-28, before that she was dancing from 2020-02-21 to 2020-02-23.-- Bob only has one record, we just take that one.
-- Solution
select username, activity, startdate, enddate
from
(select *,rank()over(partition by username order by startdate desc) as rk,count(username)over(partition by username) as cnt
from useractivity) a
where a.rk =2 or cnt =1
22 Highest grade for each student.sql
-- Question 63-- Table: Enrollments
--+---------------+---------+--| Column Name | Type |--+---------------+---------+--| student_id |int|--| course_id |int|--| grade |int|--+---------------+---------+--(student_id, course_id) is the primary key of this table.-编写SQL查询以找到每个学生的最高成绩及其相应的课程。 如果是平局,您应该找到带有最小course_id的科目。 输出必须通过升序student_id进行排序。
-- The query result format is in the following example:-- Enrollments table:--+------------+-------------------+--| student_id | course_id | grade |--+------------+-----------+-------+--|2|2|95|--|2|3|95|--|1|1|90|--|1|2|99|--|3|1|80|--|3|2|75|--|3|3|82|--+------------+-----------+-------+-- Result table:--+------------+-------------------+--| student_id | course_id | grade |--+------------+-----------+-------+--|1|2|99|--|2|2|95|--|3|3|82|--+------------+-----------+-------+
-- Solution
select student_id, course_id, grade
from(
select student_id, course_id, grade,rank()over(partition by student_id order by grade desc, course_id) as rk
from enrollments) a
where a.rk =1
23 Immediate Food Delivery 2.sql
-- Question 82-- Table: Delivery
--+-----------------------------+---------+--| Column Name | Type |--+-----------------------------+---------+--| delivery_id |int|--| customer_id |int|--| order_date | date |--| customer_pref_delivery_date | date |--+-----------------------------+---------+-- delivery_id is the primary key of this table.-- The table holds information about food delivery to customers that make orders at some date and specify a preferred delivery date (on the same order date or after it).-如果客户的首选交货日期与订单日期相同,则该订单称为即时订单,否则称为计划订单。
-客户的第一笔订单是客户下达最早订单日期的订单。 可以确保客户只下一个订单。
-编写SQL查询以查找所有客户的第一笔订单中即时订单的百分比,四舍五入到小数点后两位。
-- The query result format is in the following example:-- Delivery table:--+-------------+-------------+------------+-----------------------------+--| delivery_id | customer_id | order_date | customer_pref_delivery_date |--+-------------+-------------+------------+-----------------------------+--|1|1|2019-08-01|2019-08-02|--|2|2|2019-08-02|2019-08-02|--|3|1|2019-08-11|2019-08-12|--|4|3|2019-08-24|2019-08-24|--|5|3|2019-08-21|2019-08-22|--|6|2|2019-08-11|2019-08-13|--|7|4|2019-08-09|2019-08-09|--+-------------+-------------+------------+-----------------------------+-- Result table:--+----------------------+--| immediate_percentage |--+----------------------+--|50.00|--+----------------------+-- The customer id 1 has a first order with delivery id 1 and it is scheduled.-- The customer id 2 has a first order with delivery id 2 and it is immediate.-- The customer id 3 has a first order with delivery id 5 and it is scheduled.-- The customer id 4 has a first order with delivery id 7 and it is immediate.-- Hence, half the customers have immediate first orders.
-- Solution
select
round(avg(case when order_date = customer_pref_delivery_date then 1else0 end)*100,2) as
immediate_percentage
from
(select *,rank()over(partition by customer_id order by order_date) as rk
from delivery) a
where a.rk=1
24 Investments in 2016.sql
-- Question 96-编写查询以打印满足以下条件的所有保单持有人2016年所有投资总价值之和(TIV_2016),小数点后两位。
-与一个或多个其他保单持有人具有相同的TIV_2015值。
-与其他投保人不在同一个城市中(即:(纬度,经度)属性对必须唯一)。
-- Input Format:-- The insurance table is described as follows:--| Column Name | Type |--|-------------|---------------|--| PID |INTEGER(11)|--| TIV_2015 |NUMERIC(15,2)|--| TIV_2016 |NUMERIC(15,2)|--| LAT |NUMERIC(5,2)|--| LON |NUMERIC(5,2)|-- 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.-- Sample Input
--| PID | TIV_2015 | TIV_2016 | LAT | LON |--|-----|----------|----------|-----|-----|--|1|10|5|10|10|--|2|20|20|20|20|--|3|10|30|20|20|--|4|10|40|40|40|-- Sample Output
--| TIV_2016 |--|----------|--|45.00|-- Explanation
-- The first record in the table, like the last record, meets both of the two criteria.-- The TIV_2015 value '10' is as the same as the third and forth record, and its location unique.-- The second record does not meet any of the two criteria. Its TIV_2015 is not like any other policyholders.-- And its location is the same with the third record, which makes the third record fail, too.-- So, the result is the sum of TIV_2016 of the first and last record, which is 45.
-- Solution
select sum(TIV_2016) TIV_2016
from
(select *,count(*) over (partition by TIV_2015) as c1,count(*) over (partition by LAT, LON) as c2
from insurance ) t
where c1 >=1 and c2 =1;
25 Last person to fit in the elevator.sql
-- Question 68-- Table: Queue
--+-------------+---------+--| Column Name | Type |--+-------------+---------+--| person_id |int|--| person_name | varchar |--| weight |int|--| turn |int|--+-------------+---------+-- person_id is the primary key column forthis table.-- This table has the information about all people waiting for an elevator.-- The person_id and turn columns will contain all numbers from 1 to n, where n is the number of rows in the table.-- The maximum weight the elevator can hold is 1000.-编写SQL查询以查找适合电梯的最后一位不超过重量限制的人员的person_name。 确保队列中第一位的人可以坐在电梯中。
-- The query result format is in the following example:-- Queue table
--+-----------+-------------------+--------+------+--| person_id | person_name | weight | turn |--+-----------+-------------------+--------+------+--|5| George Washington |250|1|--|3| John Adams |350|2|--|6| Thomas Jefferson |400|3|--|2| Will Johnliams |200|4|--|4| Thomas Jefferson |175|5|--|1| James Elephant |500|6|--+-----------+-------------------+--------+------+-- Result table
--+-------------------+--| person_name |--+-------------------+--| Thomas Jefferson |--+-------------------+-- Queue table is ordered by turn in the example for simplicity.-- 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.-- Thomas Jefferson(id 6) is the last person to fit in the elevator because he has the last turn in these three people.
With t1 as
(
select *,sum(weight)over(order by turn rows between unbounded preceding and current row ) as cum_weight
from queue
order by turn)
select t1.person_name
from t1
where turn =(select max(turn) from t1 where t1.cum_weight<=1000)