-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathSQL.py
More file actions
executable file
·2949 lines (2692 loc) · 76 KB
/
SQL.py
File metadata and controls
executable file
·2949 lines (2692 loc) · 76 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# 262
"""
SELECT t.Request_at AS "Day",
CAST(SUM(CASE
WHEN t.Status != 'completed' THEN 1
ELSE 0
END) / COUNT(*) AS decimal(16,2)) AS "Cancellation Rate"
FROM Trips t
LEFT JOIN Users u1 on u1.Users_Id = t.Client_Id
LEFT JOIN Users u2 on u2.Users_Id = t.Driver_Id
WHERE u1.Banned = 'No' AND u2.Banned = 'No' AND t.Request_at BETWEEN "2013-10-01" AND "2013-10-03"
GROUP BY t.Request_at
;
"""
# 185
"""
SELECT d.Name AS "Department", e.Name AS "Employee", e.Salary
FROM Employee e
JOIN Department d on e.DepartmentId = d.Id
JOIN (
SELECT e.Name, COUNT(DISTINCT e2.Salary) AS "Rownum"
FROM Employee e
JOIN Employee e2 on e.DepartmentId = e2.DepartmentId
WHERE e.Salary <= e2.Salary AND e.DepartmentId = e2.DepartmentId
GROUP BY e.Id ) t on e.Name = t.Name
WHERE t.RowNum <=3
ORDER BY d.Name ASC, e.Salary DESC
;
"""
# 579
"""
SELECT e.Id AS "id", e.Month AS "month", SUM(e2.Salary) AS "Salary"
FROM Employee e
JOIN Employee e2 on e.Id = e2.Id
WHERE e.Month < (SELECT MAX(e3.Month)
FROM Employee e3
WHERE e.Id = e3.Id) AND e.Month >= e2.Month
AND e.Month - 3 < e2.Month
GROUP BY e.Id, e.Month
ORDER BY e.Id ASC, e.Month DESC
;
"""
# 579 Echo
# Putting the and conditions in 'ON Clause' will be much slower
"""
SELECT E.Id AS 'id', E.Month AS 'month', SUM(IFNULL(E1.Salary,0)) AS 'Salary'
FROM Employee E
lEFT JOIN Employee E1
ON E.Id = E1.Id and E1.Month+2 >= E.Month and E.Month >= E1.Month
WHERE (E.Id, E.Month) NOT IN
(SELECT Id, MAX(Month) AS 'last_month'
FROM Employee
GROUP BY Id)
GROUP BY E.Id, E.Month
ORDER BY E.Id ASC, E.Month DESC
"""
# 601
"""
SELECT s1.id, s1.visit_date, s1.people
FROM stadium s1
WHERE (SELECT COUNT(*)
FROM stadium s2
WHERE s1.id < s2.id + 1 AND s1.id > s2.id - 3 AND s2.people>=100) = 3
OR
(SELECT COUNT(*)
FROM stadium s3
WHERE s1.id < s3.id + 2 AND s1.id > s3.id - 2 AND s3.people>=100) = 3
OR
(SELECT COUNT(*)
FROM stadium s4
WHERE s1.id < s4.id + 3 AND s1.id > s4.id - 1 AND s4.people>=100) = 3
"""
# 601 Echo
"""
SELECT S.id,S.visit_date,S.people
FROM stadium S
LEFT JOIN (SELECT id,visit_date,people,IF(people>=100,1,0) AS 'check'
FROM stadium) AS A
ON S.id+2 = A.id
LEFT JOIN (SELECT id,visit_date,people,IF(people>=100,1,0) AS 'check'
FROM stadium) AS B
ON S.id+1 = B.id
lEFT JOIN (SELECT id,visit_date,people,IF(people>=100,1,0) AS 'check'
FROM stadium) AS C
ON S.id-1 = C.id
LEFT JOIN (SELECT id,visit_date,people,IF(people>=100,1,0) AS 'check'
FROM stadium) AS D
ON S.id-2 = D.id
WHERE ((S.people>=100) and A.check=1 and B.check=1)
OR ((S.people>=100) and B.check=1 and C.check=1)
OR ((S.people>=100) and C.check=1 and D.check=1)
ORDER BY id
"""
# 601 Answer from wangyihuan24 https://leetcode.com/wanyihuang24/
"""
SELECT DISTINCT S1.*
FROM stadium S1
JOIN stadium S2
JOIN stadium S3
ON ((S1.id = S2.id - 1 AND S1.id = S3.id -2)
OR (S3.id = S1.id - 1 AND S3.id = S2.id -2)
OR (S3.id = S2.id - 1 AND S3.id = S1.id -2))
WHERE S1.people >= 100
AND S2.people >= 100
AND S3.people >= 100
ORDER BY S1.id;
"""
# 615
"""
SELECT t1.pay_month, t1.department_id,
(CASE WHEN CAST(t1.avg_salary AS decimal(16,2)) > (SELECT CAST(AVG(s2.amount) AS decimal(16,2))
FROM salary s2
WHERE DATE_FORMAT(s2.pay_date,'%Y-%m') = t1.pay_month) THEN "higher"
WHEN CAST(t1.avg_salary AS decimal(16,2)) = (SELECT CAST(AVG(s2.amount) AS decimal(16,2))
FROM salary s2
WHERE DATE_FORMAT(s2.pay_date,'%Y-%m') = t1.pay_month) THEN "same"
ELSE "lower"
END) AS "comparison"
FROM (
SELECT DATE_FORMAT(s.pay_date,'%Y-%m') AS "pay_month",
e.department_id AS "department_id", AVG(s.amount) AS "avg_salary"
FROM salary s
JOIN employee e on s.employee_id = e.employee_id
GROUP BY e.department_id, DATE_FORMAT(s.pay_date,'%Y-%m') ) t1
"""
# 615 Echo
"""
SELECT date_format(S.pay_date,'%Y-%m') AS 'pay_month',
E.department_id,
( CASE WHEN ROUND(AVG(S.amount),2)> A.avg THEN 'higher'
WHEN ROUND(AVG(S.amount),2)= A.avg THEN 'same'
ELSE 'lower' END) AS 'comparison'
FROM salary S
lEFT JOIN (SELECT date_format(pay_date,'%Y-%m') AS 'month',
ROUND(AVG(amount),2) AS 'avg'
FROM Salary
GROUP BY date_format(pay_date,'%Y-%m')) AS A
ON date_format(S.pay_date,'%Y-%m') = A.month
LEFT JOIN employee E
ON S.employee_id = E.employee_id
GROUP BY date_format(S.pay_date,'%Y-%m'), E.department_id
"""
# 618
# use @ function to create row_number()
"""
SELECT Am_table.America, As_table.Asia, Eu_table.Europe
FROM (SELECT @am:=@am+1 AS idx_am, s1.name AS America
FROM (SELECT @am:=0) c, student s1
WHERE s1.continent = 'America'
ORDER BY America ASC) Am_table
LEFT JOIN (
SELECT @asia:=@asia+1 AS idx_as, s2.name AS Asia
FROM (SELECT @asia:=0) c, student s2
WHERE s2.continent = 'Asia'
ORDER BY Asia ASC) As_table
ON Am_table.idx_am = As_table.idx_as
LEFT JOIN (
SELECT @eu:=@eu+1 AS idx_eu, s3.name AS Europe
FROM (SELECT @eu:=0) c, student s3
WHERE s3.continent = 'Europe'
ORDER BY Europe ASC) Eu_table
ON Am_table.idx_am = Eu_table.idx_eu
"""
# 1127
"""
SELECT DISTINCT (sp.spend_date), sp3.platform,
IFNULL(clean_spending.total_amount,0) AS "total_amount",
IFNULL(clean_spending.total_users,0) AS "total_users"
FROM Spending sp
CROSS JOIN
(SELECT DISTINCT (sp2.platform)
FROM Spending sp2
UNION (SELECT 'both' FROM Spending sp3)) sp3
LEFT JOIN
(SELECT s.spend_date,
(CASE WHEN s2.count_platform = 2 THEN "both" ELSE s.platform END) AS "clean_platform",
SUM(s.amount) AS "total_amount",
COUNT(DISTINCT s.user_id) AS "total_users"
FROM Spending s
LEFT JOIN (SELECT user_id, spend_date, COUNT(DISTINCT platform) AS count_platform
FROM Spending
GROUP BY user_id, spend_date) s2
ON s.user_id = s2.user_id AND s.spend_date = s2.spend_date
GROUP BY s.spend_date, clean_platform ) clean_spending
ON sp.spend_date = clean_spending.spend_date AND sp3.platform = clean_spending.clean_platform
ORDER BY CASE sp3.platform WHEN "desktop" THEN 0
WHEN "mobile" THEN 1
ELSE 2
END
"""
# 1127 Echo
"""
SELECT D.spend_date,D.platform,IFNULL(S2.total_amount,0) AS 'total_amount',
IFNULL(S2.total_users,0) AS 'total_users'
FROM
(SELECT * FROM
(SELECT DISTINCT spend_date FROM Spending) AS A
JOIN
(SELECT 'desktop' AS 'platform'
UNION
SELECT 'mobile' AS 'platform'
UNION
SELECT 'both' AS 'platform') AS P
ON 1) AS D
lEFT JOIN
(SELECT S1.spend_date,S1.platform,
SUM(S1.t_amount) AS 'total_amount',
COUNT(S1.user_id) AS 'total_users'
FROM
(SELECT S.user_id, S.spend_date,
SUM(S.amount) AS 't_amount',
(CASE WHEN COUNT(S.platform)=2 THEN 'both'
ELSE S.platform END) AS 'platform'
FROM Spending S
GROUP BY user_id, spend_date) AS S1
GROUP BY S1.spend_date, S1.platform) AS S2
ON D.platform = S2.platform and D.spend_date = S2.spend_date
"""
# 571
"""
SELECT agg2.Clean_median AS "median"
FROM (SELECT agg.cum_fre,
CASE WHEN agg.cum_fre=0.5 THEN (agg.Number + agg.Sub_num)/2
ELSE agg.Number END AS Clean_median
FROM (SELECT n1.Number, MIN(n2.Number) AS "Sub_num",
@fre:=@fre+n1.Frequency/(SELECT SUM(n3.Frequency)
FROM Numbers n3) AS cum_fre
FROM (SELECT @fre:=0) c, Numbers n1
LEFT JOIN Numbers n2 on n1.Number < n2.Number
GROUP BY n1.Number) agg ) agg2
WHERE agg2.cum_fre >=0.5
ORDER BY agg2.Clean_median
LIMIT 1
"""
# 571 Echo
"""
SELECT AVG(Number) 'median'
FROM
(SELECT Number, Frequency, @cum:=@cum+Frequency AS 'cum'
FROM Numbers, (SELECT @cum:=0) tmp
ORDER BY Number) AS N_cum
WHERE
((SELECT ROUND(SUM(Frequency)/2,0) FROM Numbers)
between cum-Frequency+1 and cum)
OR ((SELECT ROUND(SUM(Frequency)/2+0.5,0) FROM Numbers)
between cum-Frequency+1 and cum)
"""
# 1159
"""
SELECT DISTINCT u2.user_id AS "seller_id", agg.2nd_item_fav_brand
FROM Users u2
LEFT JOIN (
SELECT o1.seller_id, CASE WHEN u.favorite_brand = i.item_brand THEN "yes"
ELSE "no" END AS "2nd_item_fav_brand"
FROM Orders o1
LEFT JOIN Users u on o1.seller_id = u.user_id
LEFT JOIN Items i on o1.item_id = i.item_id
WHERE (SELECT COUNT(order_id) FROM Orders o2
WHERE o1.seller_id = o2.seller_id
AND o1.order_date>o2.order_date) = 1
) agg
ON u2.user_id = agg.seller_id
ORDER BY u2.user_id
"""
# 1159 faster if getting rank first. answer from ying61 https://leetcode.com/ying61/
"""
select u.user_id seller_id, case when u.favorite_brand = i.item_brand then "yes" else "no" end 2nd_item_fav_brand
from Users u
left outer join
(select seller_id, order_date, item_id,
# assume Orders table is in chronological order
case when @prev = seller_id then @rank := @rank + 1 else @rank := 1 end Rank,
@prev := seller_id partition_key
FROM Orders
join (select @rank := 0, @prev := 0) tmp
order by seller_id, order_date asc) rt
on u.user_id = rt.seller_id and rt.Rank = 2
left outer join Items i
on rt.item_id = i.item_id
"""
# 1097
"""
SELECT a.event_date AS "install_dt",
COUNT(DISTINCT a.player_id) AS "installs",
CAST(AVG(CASE WHEN a3.games_played>0 THEN 1
ELSE 0 END) AS decimal(16,2)) AS "Day1_retention"
FROM ((SELECT a2.player_id, MIN(a2.event_date) AS "event_date"
FROM Activity a2
GROUP BY a2.player_id)) a
LEFT JOIN Activity a3
ON a3.player_id = a.player_id AND a3.event_date = DATE_ADD(a.event_date, INTERVAL 1 DAY)
GROUP BY a.event_date
"""
# 1097 Echo
"""
SELECT Install.install_dt, Install.installs,
ROUND(IFNULL(Re.retention/Install.installs,0),2) As 'Day1_retention'
FROM
(SELECT install_dt, COUNT(*) AS 'installs'
FROM Activity A
LEFT JOIN
(SELECT player_id,min(event_date) AS 'install_dt'
FROM Activity
GROUP BY player_id) AS install_table
ON A.player_id = install_table.player_id
WHERE A.event_date = install_table.install_dt
GROUP BY install_dt) AS Install
LEFT JOIN
(SELECT install_dt,
COUNT(*) AS 'retention'
FROM Activity A2
LEFT JOIN (SELECT player_id,min(event_date) AS 'install_dt'
FROM Activity
GROUP BY player_id) AS install_table
ON A2.player_id = install_table.player_id
WHERE A2.event_date = adddate(install_dt,interval 1 day)
GROUP BY install_dt) AS Re
ON Re.install_dt = Install.install_dt
"""
# 569
"""
SELECT clean.Id, clean.Company , clean.Salary
FROM (
SELECT e.Id, e.Company, e.Salary, (
SELECT SUM( CASE WHEN e2.Salary <= e.Salary AND e2.Id < e.Id THEN 1
WHEN e2.Salary < e.Salary THEN 1
ELSE 0 END)
FROM Employee e2
WHERE e2.Company = e.Company AND e2.Salary <= e.Salary ) AS "small",
(SELECT SUM( CASE WHEN e3.Salary >= e.Salary AND e3.Id > e.Id THEN 1
WHEN e3.Salary > e.Salary THEN 1
ELSE 0 END)
FROM Employee e3
WHERE e3.Company = e.Company AND e3.Salary >= e.Salary ) AS "large"
FROM Employee e ) clean
WHERE ABS(clean.small - clean.large) <= 1
ORDER BY clean.Company, clean.Salary
"""
# 569 Echo beats 99%
"""
SELECT Id, Company, Salary
FROM
(SELECT Id, Company, Salary,
(CASE WHEN @prev = Company THEN @rank:= @rank+1 ELSE @rank:=1 END) Rank,
@prev:= Company patition_key
FROM Employee
JOIN (SELECT @rank:=0, @prev:=0) tmp
ORDER BY Company, Salary) AS Rank_table
WHERE (Company,Rank) in
(SELECT * FROM
((SELECT Company,(COUNT(ID)+1)/2 AS 'Rank'
FROM Employee
GROUP BY Company
HAVING COUNT(Id)%2=1)
UNION
(SELECT Company,COUNT(ID)/2 AS 'Rank'
FROM Employee
GROUP BY Company
HAVING COUNT(Id)%2=0)
UNION
(SELECT Company,COUNT(ID)/2+1 AS 'Rank'
FROM Employee
GROUP BY Company
HAVING COUNT(Id)%2=0)) AS A)
ORDER BY Company, Salary
"""
# below is faster
"""
SELECT clean.Id, clean.Company, clean.Salary
FROM (
SELECT e.Id, e.Company, e.Salary, (
SELECT SUM( CASE WHEN e2.Salary <= e.Salary AND e2.Id < e.Id THEN 1
WHEN e2.Salary < e.Salary THEN 1
ELSE 0 END) + 1
FROM Employee e2
WHERE e2.Company = e.Company AND e2.Salary <= e.Salary ) AS "small",
e4.sum_count
FROM Employee e
LEFT JOIN (SELECT e3.Company, COUNT(*) AS "sum_count"
FROM Employee e3
GROUP BY e3.Company) e4
ON e.Company = e4.Company ) clean
WHERE ABS((clean.sum_count + 1)/2 - clean.small) <= 0.5
ORDER BY clean.Company, clean.Salary
"""
# 1194
"""
SELECT group_max.group_id AS "GROUP_ID", clean2.player_id AS "PLAYER_ID"
FROM (
SELECT clean.group_id, MAX(clean.total_score) AS "max_score"
FROM (
SELECT p.player_id, p.group_id,
SUM(IFNULL(m.first_score, 0)) AS "total_score"
FROM Players p
LEFT JOIN (SELECT m.first_player, m.first_score
FROM Matches m
UNION ALL (SELECT m2.second_player, m2.second_score
FROM Matches m2)) m
ON p.player_id = m.first_player
GROUP BY p.player_id, p.group_id) clean
GROUP BY clean.group_id) group_max
JOIN (
SELECT p.player_id, p.group_id,
SUM(IFNULL(m.first_score, 0)) AS "total_score"
FROM Players p
LEFT JOIN (SELECT m.first_player, m.first_score
FROM Matches m
UNION ALL(SELECT m2.second_player, m2.second_score
FROM Matches m2)) m
ON p.player_id = m.first_player
GROUP BY p.player_id, p.group_id) clean2
ON group_max.max_score = clean2.total_score AND group_max.group_id = clean2.group_id
WHERE clean2.player_id = (SELECT MIN(clean3.player_id)
FROM (
SELECT p.player_id, p.group_id,
SUM(IFNULL(m.first_score, 0)) AS "total_score"
FROM Players p
LEFT JOIN (SELECT m.first_player, m.first_score
FROM Matches m
UNION ALL(SELECT m2.second_player, m2.second_score
FROM Matches m2)) m
ON p.player_id = m.first_player
GROUP BY p.player_id, p.group_id) clean3
WHERE clean3.total_score = group_max.max_score AND clean3.group_id = clean2.group_id)
ORDER BY group_max.group_id
"""
# 1194 Echo
"""
SELECT group_id,player_id
FROM
(SELECT P.group_id, P.player_id, SUM(first_score) AS 'total_score'
FROM
(SELECT first_player, first_score FROM Matches
UNION ALL
SELECT second_player,second_score FROM Matches) AS M
LEFT JOIN Players P
ON P.player_id = M.first_player
GROUP BY P.player_id
ORDER BY group_id, total_score DESC, player_id) final
Group BY group_id
"""
# 614
"""
SELECT DISTINCT f.follower, f3.num
FROM follow f
JOIN (SELECT f2.followee, COUNT(DISTINCT f2.follower) AS "num"
FROM follow f2
GROUP BY f2.followee) f3
ON f.follower = f3.followee
ORDER BY f.follower
"""
# 177
"""
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
RETURN (
SELECT clean.Salary
FROM (SELECT e.Salary, (SELECT COUNT(DISTINCT e2.Salary) + 1
FROM Employee e2
WHERE e2.Salary < e.Salary) AS "nth"
FROM Employee e ) clean
WHERE clean.nth = N
LIMIT 1
);
END
"""
# faster
"""
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
DECLARE row_start INT;
SET row_start = N - 1;
RETURN (
SELECT DISTINCT e.Salary
FROM Employee e
ORDER BY e.Salary DESC
LIMIT 1 OFFSET row_start
);
END
"""
# 184
"""
SELECT d.Name AS "Department", e.Name AS "Employee", e.Salary
FROM Employee e
JOIN Department d ON e.DepartmentId = d.Id
WHERE (SELECT COUNT(DISTINCT e2.Id)
FROM Employee e2
WHERE e2.Salary > e.Salary AND e2.DepartmentId = e.DepartmentId ) = 0
"""
# faster
"""
SELECT d.Name AS "Department", e.Name AS "Employee", e.Salary
FROM Employee e
JOIN Department d ON e.DepartmentId = d.Id
WHERE (e.DepartmentId, e.Salary) IN
( SELECT e2.DepartmentId, MAX(e2.Salary)
FROM Employee e2
GROUP BY e2.DepartmentId
)
"""
# 1132
"""
SELECT ROUND(AVG(clean.daily) * 100, 2) AS "average_daily_percent"
FROM (
SELECT a.action_date, COUNT(DISTINCT CASE WHEN r.remove_date IS NOT NULL THEN a.post_id END)/COUNT(DISTINCT a.post_id) AS "daily"
FROM Actions a
LEFT JOIN Removals r
ON a.post_id = r.post_id
WHERE a.extra = "spam"
GROUP BY a.action_date ) clean
"""
# 1132 Echo
"""
SELECT ROUND(100*AVG(Ratio),2) AS 'average_daily_percent'
FROM
(SELECT A.action_date, COUNT(R.remove_date)/COUNT(*) AS Ratio
FROM
(SElECT DISTINCT post_id,action_date
FROM Actions
WHERE action='report' and extra='spam') AS A
LEFT JOIN Removals R
ON A.post_id = R.post_id
GROUP BY A.action_date) AS B
"""
# 180
"""
SELECT DISTINCT l.Num AS "ConsecutiveNums"
FROM Logs l
JOIN Logs l2
ON l.Id + 1 = l2.Id
JOIN Logs l3
ON l2.Id + 1 = l3.Id
WHERE l.Num = l2.Num AND l2.Num = l3.Num
ORDER BY l.NUm ASC
"""
# 578
"""
SELECT clean.question_id AS "survey_log"
FROM (
SELECT s.question_id,
SUM(CASE WHEN action = "answer" THEN 1 END)/
SUM(CASE WHEN action = "show" THEN 1 END) AS "answer_rate"
FROM survey_log s
GROUP BY s.question_id) clean
ORDER BY clean.answer_rate DESC
LIMIT 1
"""
# 574
"""
SELECT c.Name
FROM (
SELECT clean_vote.CandidateId
FROM (
SELECT v.CandidateId, COUNT(v.CandidateId) AS "num_vote"
FROM Vote v
GROUP BY v.CandidateId) clean_vote
ORDER BY clean_vote.num_vote DESC
LIMIT 1 ) clean2
JOIN Candidate c
ON clean2.CandidateId = c.id
"""
# 178
"""
SELECT s.Score,
(SELECT COUNT(DISTINCT s2.Score)
FROM Scores s2
WHERE s2.Score > s.Score) + 1 AS "Rank"
FROM Scores s
ORDER BY s.Score DESC
"""
# 1098
"""
SELECT b2.book_id, b2.name
FROM Books b2
LEFT JOIN (
SELECT o.book_id, b.name, SUM(quantity) AS "num_sold"
FROM Orders o
LEFT JOIN Books b
ON o.book_id = b.book_id
WHERE o.dispatch_date BETWEEN DATE_SUB('2019-06-23', INTERVAL 1 YEAR) AND '2019-06-23'
GROUP BY o.book_id ) sold
ON b2.book_id = sold.book_id
WHERE b2.available_from < DATE_SUB('2019-06-23', INTERVAL 1 MONTH)
AND IFNULL(sold.num_sold, 0) < 10
"""
#1098 Echo
"""
SELECT book_id,name
FROM Books
WHERE available_from < subdate('2019-06-23',interval 1 month)
and book_id not in
(SELECT book_id
FROM Orders
WHERE dispatch_date between subdate('2019-06-23',interval 1 year) and '2019-06-23'
GROUP BY book_id
HAVING SUM(quantity) >= 10)
"""
# 1107
"""
SELECT first_login.login_date, COUNT(DISTINCT first_login.user_id) AS "user_count"
FROM (
SELECT t.user_id, MIN(t.activity_date) AS "login_date"
FROM Traffic t
WHERE t.activity = "login"
GROUP BY t.user_id) first_login
WHERE first_login.login_date BETWEEN DATE_SUB("2019-06-30", INTERVAL 90 DAY)
AND "2019-06-30"
GROUP BY first_login.login_date
"""
# 550
"""
SELECT ROUND(COUNT(DISTINCT CASE WHEN a2.event_date IS NOT NULL THEN a.player_id END)/ COUNT(DISTINCT a.player_id),2) AS "fraction"
FROM Activity a
LEFT JOIN Activity a2
ON DATE_ADD(a.event_date, INTERVAL 1 DAY) = a2.event_date AND a.player_id = a2.player_id
JOIN (SELECT a3.player_id, MIN(a3.event_date) AS "start_date"
FROM Activity a3
GROUP BY a3.player_id
) start_date_table
ON a.event_date = start_date_table.start_date AND a.player_id = start_date_table.player_id
"""
# 550 Echo
"""
SELECT ROUND(
(SELECT COUNT(DISTINCT A1.player_id)
FROM Activity A1
WHERE (A1.player_id,A1.event_date)
in (SELECT A2.player_id, adddate(min(A2.event_date),interval 1 day)
FROM Activity A2
GROUP BY A2.player_id))
/
(SELECT COUNT(DISTINCT A3.player_id) FROM Activity A3)
,2) AS 'fraction'
"""
# 580
"""
SELECT d.dept_name, COUNT(DISTINCT s.student_id) AS "student_number"
FROM department d
LEFT JOIN student s
ON d.dept_id = s.dept_id
GROUP BY d.dept_name
ORDER BY student_number DESC, d.dept_name
"""
# 1070
"""
SELECT s2.product_id, s2.year AS "first_year", s2.quantity, s2.price
FROM Sales s2
JOIN (
SELECT s.product_id, MIN(s.year) AS "year"
FROM Sales s
GROUP BY s.product_id ) first_year
ON s2.product_id = first_year.product_id AND s2.year = first_year.year
"""
# 602
"""
SELECT union_table.requester_id AS "id", COUNT(*) AS "num"
FROM (
SELECT r.requester_id
FROM request_accepted r
UNION ALL (SELECT r2.accepter_id
FROM request_accepted r2)) union_table
GROUP BY union_table.requester_id
ORDER BY num DESC
LIMIT 1
"""
# 1149
"""
SELECT DISTINCT count_views.viewer_id AS "id"
FROM (
SELECT v.viewer_id, v.view_date, COUNT(DISTINCT v.article_id) AS "views"
FROM Views v
GROUP BY v.viewer_id, v.view_date) count_views
WHERE count_views.views >= 2
"""
# 585
"""
SELECT ROUND(SUM(distinct_table.TIV_2016),2) AS "TIV_2016"
FROM (
SELECT DISTINCT i.PID, i.TIV_2016, i3.LAT AS "LAT"
FROM insurance i
JOIN insurance i2 ON i.TIV_2015 = i2.TIV_2015 AND i.PID != i2.PID
LEFT JOIN insurance i3 ON i.LAT = i3.LAT AND i.LON = i3.LON AND i.PID != i3.PID
) distinct_table
WHERE distinct_table.LAT IS NULL
"""
# 585 takes longer time but more clear
"""
SELECT ROUND(SUM(condition_table.TIV_2016),2) AS "TIV_2016"
FROM (
SELECT i.PID, i.TIV_2016,
(SELECT COUNT(DISTINCT i2.PID)
FROM insurance i2
WHERE i2.TIV_2015=i.TIV_2015 AND i2.PID!=i.PID) AS "first_req",
(SELECT COUNT(DISTINCT i3.PID)
FROM insurance i3
WHERE i3.LAT=i.LAT AND i3.LON=i.LON AND i3.PID!=i.PID) AS "second_req"
FROM insurance i ) condition_table
WHERE condition_table.first_req !=0 AND condition_table.second_req = 0
"""
# 585 Echo
"""
SELECT ROUND(SUM(TIV_2016),2) AS 'TIV_2016'
FROM insurance
WHERE TIV_2015 in (SELECT DISTINCT TIV_2015
FROM insurance
GROUP BY TIV_2015
HAVING COUNT(*)>1)
AND (LAT,LON) in (SELECT DISTINCT LAT,LON
FROM insurance
GROUP BY LAT,LON
HAVING COUNT(*)=1)
"""
# 1205
"""
SELECT union_table.month, union_table.country,
SUM(CASE WHEN union_table.state = "approved" THEN 1 ELSE 0 END)
AS "approved_count",
SUM(CASE WHEN union_table.state = "approved" THEN union_table.amount ELSE 0 END)
AS "approved_amount",
SUM(CASE WHEN union_table.state = "Chargebacks" THEN 1 ELSE 0 END) AS "chargeback_count",
SUM(CASE WHEN union_table.state = "Chargebacks" THEN union_table.amount ELSE 0 END)
AS "chargeback_amount"
FROM (
SELECT c.trans_id, t.country, "Chargebacks" AS "state", t.amount, DATE_FORMAT(c.trans_date, "%Y-%m") AS "month"
FROM Chargebacks c
LEFT JOIN Transactions t on t.id = c.trans_id
UNION (SELECT t2.id, t2.country, t2.state, t2.amount,
DATE_FORMAT(t2.trans_date, "%Y-%m") AS "month"
FROM Transactions t2)) union_table
GROUP BY union_table.month, union_table.country
HAVING approved_count + approved_amount + chargeback_count + chargeback_amount > 0
"""
# 1205 Echo
# Similar to David's answer
"""
SELECT date_format(TT.trans_date,'%Y-%m') AS 'month',
country,SUM(state='approved') AS 'approved_count',
SUM(IF(state='approved',amount,0)) AS 'approved_amount',
SUM(state='charged') AS 'chargeback_count',
SUM(IF(state='charged',amount,0)) AS 'chargeback_amount'
FROM
(SELECT * FROM Transactions
UNION ALL
(SELECT trans_id,country,'charged' AS 'state',amount,C.trans_date
FROM Chargebacks C
LEFT JOIN Transactions T
ON C.trans_id = T.id)) AS TT #total
GROUP BY date_format(TT.trans_date,'%Y-%m'),TT.country
HAVING SUM(state='approved')>0 or SUM(state='charged')>0
"""
# 1158
"""
SELECT u.user_id AS "buyer_id", u.join_date, IFNULL(clean_orders.orders_in_2019, 0) AS "orders_in_2019"
FROM Users u
LEFT JOIN (SELECT o.buyer_id, COUNT(DISTINCT order_id) AS 'orders_in_2019'
FROM Orders o
WHERE YEAR(o.order_date) = 2019
GROUP BY o.buyer_id) clean_orders
ON u.user_id = clean_orders.buyer_id
"""
# 1174
"""
SELECT ROUND(COUNT(DISTINCT CASE WHEN first_table.first_date = d2.customer_pref_delivery_date THEN d2.customer_id END)/COUNT(DISTINCT d2.customer_id) * 100,2) AS "immediate_percentage"
FROM (
SELECT d.customer_id, MIN(d.order_date) AS "first_date"
FROM Delivery d
GROUP BY d.customer_id) first_table
LEFT JOIN Delivery d2
ON d2.customer_id = first_table.customer_id AND d2.order_date = first_table.first_date
"""
# 1174
# faster, using IN is faster than the previous one
"""
SELECT ROUND(COUNT(DISTINCT CASE WHEN d.order_date = d.customer_pref_delivery_date THEN d.customer_id END)/COUNT(DISTINCT d.customer_id) * 100,2) AS "immediate_percentage"
FROM Delivery d
WHERE (d.customer_id, d.order_date) IN (SELECT d2.customer_id, MIN(d2.order_date)
FROM delivery d2
GROUP BY d2.customer_id)
"""
#1174 Echo
"""
SELECT ROUND(100*SUM(D.order_date=D.customer_pref_delivery_date)/COUNT(*),2)
AS 'immediate_percentage'
FROM Delivery D
WHERE (customer_id,order_date) in
(SElECT customer_id,min(order_date)
FROM Delivery
GROUP BY customer_id)
"""
# 612
# no aggregate function allowed during features created by select
"""
SELECT ROUND((SELECT MIN(SQRT(POWER(p.x-p2.x,2) + POWER(p.y-p2.y,2)))
FROM point_2d p2
WHERE p.x != p2.x OR p.y!=p2.y),2) AS "shortest"
FROM point_2d p
ORDER BY shortest ASC
LIMIT 1
"""
# 612 Echo
"""
SELECT ROUND(MIN(SQRT(pow(p1.x-p2.x,2)+pow(p1.y-p2.y,2))),2) AS 'shortest'
FROM point_2d p1, point_2d p2
WHERE not (p1.x = p2.x and p1.y = p2.y)
"""
# 626
"""
SELECT s.id, IFNULL(CASE WHEN MOD(s.id,2) = 1 THEN s2.student
WHEN MOD(s.id,2) = 0 THEN s3.student
END, s.student) AS "student"
FROM seat s
LEFT JOIN seat s2 on s.id + 1 = s2.id
LEFT JOIN seat s3 on s.id - 1 = s3.id
ORDER BY s.id ASC
"""
# 626 Echo
"""
SELECT S1.id,
IFNULL((CASE
WHEN S1.id%2=1 THEN (SELECT student FROM seat S2 WHERE S2.id=S1.id+1)
WHEN S1.id%2=0 THEN (SELECT student FROM seat S3 WHERE S3.id=S1.id-1)
END),S1.student) AS 'student'
FROM seat S1
"""
# 1193
"""
SELECT DATE_FORMAT(t.trans_date, "%Y-%m") AS "month", t.country,
COUNT(DISTINCT t.id) AS "trans_count",
COUNT(DISTINCT CASE WHEN t.state = "approved" THEN t.id END) AS "approved_count",
SUM(t.amount) AS "trans_total_amount",
SUM(CASE WHEN t.state = "approved" THEN t.amount ELSE 0 END) AS "approved_total_amount"
FROM Transactions t
GROUP BY DATE_FORMAT(t.trans_date, "%Y-%m"), t.country
"""
# 1212
"""
SELECT t.team_id, t.team_name, IFNULL(SUM(clean_score.host_score),0) AS "num_points"
FROM Teams t
LEFT JOIN (
SELECT host_table.*
FROM (SELECT m.host_team, m.guest_team,
(CASE WHEN m.host_goals > m.guest_goals THEN 3
WHEN m.host_goals = m.guest_goals THEN 1
ELSE 0 END) AS "host_score"
FROM Matches m) host_table
UNION ALL
(SELECT m2.guest_team, m2.host_team,
(CASE WHEN m2.host_goals < m2.guest_goals THEN 3
WHEN m2.host_goals = m2.guest_goals THEN 1
ELSE 0 END) AS "guest_score"
FROM Matches m2)) clean_score
ON t.team_id = clean_score.host_team
GROUP BY t.team_id
ORDER BY num_points DESC, team_id ASC
"""
# 1164
"""
SELECT DISTINCT p3.product_id, IFNULL(price_table.price, 10) AS "price"
FROM Products p3
LEFT JOIN (
SELECT p.product_id, p.new_price AS "price"
FROM Products p
WHERE (p.product_id, p.change_date) IN
(SELECT p2.product_id, MAX(p2.change_date) AS "change_date"
FROM Products p2
WHERE p2.change_date <= '2019-08-16'
GROUP BY p2.product_id
)
) price_table
ON p3.product_id = price_table.product_id
ORDER BY price DESC
"""
# 1164 Echo
"""
SELECT P.product_id,IFNULL(A.price,10) AS 'price'
FROM (SELECT DISTINCT product_id FROM Products) AS P
LEFT JOIN
(SELECT product_id,new_price AS 'price'
FROM Products P
WHERE (product_id,change_date) IN
(SELECT product_id,MAX(change_date)
FROM Products
WHERE change_date <= '2019-08-16'
GROUP BY product_id)) AS A
ON P.product_id=A.product_id
"""
# OR
"""
SELECT product_id,new_price AS 'price'
FROM Products P
WHERE (product_id,change_date) IN
(SELECT product_id,MAX(change_date)
FROM Products
WHERE change_date <= '2019-08-16'
GROUP BY product_id)
UNION
SELECT product_id,10
FROM (SELECT DISTINCT product_id FROM Products) AS A
WHERE product_id not in
(SELECT product_id
FROM Products
WHERE change_date <= '2019-08-16')
"""
# 608
"""
SELECT count_table.id, (CASE WHEN count_table.p_id IS NULL THEN "Root"
WHEN count_table.parent_count = 0 THEN "Leaf"
ELSE "Inner" END) AS "Type"
FROM (
SELECT t.id, t.p_id, (SELECT COUNT(t2.p_id)
FROM tree t2
WHERE t2.p_id = t.id) AS 'parent_count'
FROM tree t ) count_table
"""
# 608
# using join is faster
"""
SELECT t2.id , (CASE WHEN t2.p_id IS NULL THEN "Root"
WHEN parent_table.parent_count IS NULL THEN "Leaf"
ELSE 'Inner' END) AS "Type"
FROM tree t2
LEFT JOIN (
SELECT t.p_id, COUNT(t.p_id) AS "parent_count"
FROM tree t
WHERE t.p_id IS NOT NULL
GROUP BY t.p_id) parent_table
ON t2.id = parent_table.p_id
"""
# 608 Echo