-
Notifications
You must be signed in to change notification settings - Fork 10
/
very_easy.sql
81 lines (73 loc) · 2.41 KB
/
very_easy.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
-- https://leetcode.com/problems/employees-earning-more-than-their-managers/
select e.Name as Employee
from Employee as e
inner join Employee as m
on e.ManagerId = m.Id
where e.Salary > m.Salary;
-- https://leetcode.com/problems/not-boring-movies/
select id,movie,description,rating
from cinema
where id%2=1 and description <> 'boring'
order by rating desc;
-- https://leetcode.com/problems/invalid-tweets/
select tweet_id from Tweets where length(content) > 15;
-- https://leetcode.com/problems/find-total-time-spent-by-each-employee/
select
event_day as day,
emp_id,
sum(out_time-in_time) as total_time
from
Employees
group by
event_day,
emp_id
;
-- https://leetcode.com/problems/find-the-team-size/
-- select e1.employee_id, count(*) as team_size from Employee e1, Employee e2 where e1.team_id = e2.team_id group by e1.employee_id;
-- select employee_id, count(*) over(partition by team_id) as team_size from employee
select
a.employee_id as employee_id,
b.team_size as team_size
from
Employee as a
left join
(
select
team_id,
count(employee_id) as team_size
from
Employee
group by
team_id
) as b
on
a.team_id = b.team_id
;
-- https://leetcode.com/problems/average-selling-price/
select
p.product_id,
round(sum(p.price * u.units) / sum(u.units), 2) as average_price
from
Prices as p,
UnitsSold as u
where
p.product_id = u.product_id
and u.purchase_date between start_date and end_date
group by
p.product_id
;
-- https://leetcode.com/problems/product-sales-analysis-i/
-- select product_name, year, price from Sales left join Product using (product_id)
-- join on 后面的条件已经把结果过滤了一遍,而where是笛卡尔积后才根据限制条件进行过滤,所以join性能要比where好
select p.product_name, s.year, s.price
from Sales as s, Product as p
where s.product_id = p.product_id;
-- https://leetcode.com/problems/product-sales-analysis-ii/
select product_id, sum(quantity) as total_quantity from Sales group by product_id;
-- https://leetcode.com/problems/triangle-judgement/
-- SELECT x,y,z,(CASE WHEN x+y>z AND x+z>y AND z+y>x THEN 'Yes' ELSE 'No' END) as triangle FROM triangle;
select
x, y, z,
-- 两最小边之和大于最长边,则能构成三角形
if( x+y+z-greatest(x,y,z)>greatest(x,y,z), 'Yes', 'No' ) as triangle
from triangle;