-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.sql
66 lines (63 loc) · 1014 Bytes
/
solution.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
-- Section1
SELECT
c.name,
c.phone
FROM
customers c
INNER JOIN orders o ON c.id = o.customer_id
INNER JOIN restaurant_foods rf ON rf.id = o.restaurant_food_id
GROUP BY
c.name,
c.phone
ORDER BY
count(o.id) DESC,
count(rf.restaurant_id) DESC
LIMIT
1;
-- Section2
SELECT
f.id,
f.name
FROM
orders o
INNER JOIN restaurant_foods rf ON rf.id = o.restaurant_food_id
INNER JOIN foods f ON rf.food_id = f.id
GROUP BY
f.id,
f.name
ORDER BY
avg(o.rate) DESC,
f.id
LIMIT
10;
-- Section3
SELECT
r.id,
r.name
FROM
orders o
INNER JOIN restaurant_foods rf ON rf.id = o.restaurant_food_id
INNER JOIN restaurants r ON rf.restaurant_id = r.id
GROUP BY
r.id,
r.name
ORDER BY
avg(o.rate) DESC,
r.id
LIMIT
10;
-- Section4
SELECT
c.name,
c.phone
FROM
customers c
INNER JOIN orders o ON c.id = o.customer_id
INNER JOIN restaurant_foods rf ON rf.id = o.restaurant_food_id
GROUP BY
c.name,
c.phone
HAVING
count(DISTINCT rf.restaurant_id) >= 5
ORDER BY
c.name;