-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSet2.sql
More file actions
1687 lines (1318 loc) · 45 KB
/
Set2.sql
File metadata and controls
1687 lines (1318 loc) · 45 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
# Q51.
-- create table if not exists World
-- (
-- name VARCHAR(50),
-- continent varchar(50),
-- area int,
-- population bigint,
-- gdp bigint,
-- constraint pk PRIMARY KEY (name)
-- );
-- insert into World VALUES ('Afghanistan','Asia',652230,25500100,20343000000),
-- ('Albania','Europe',28748,2831741,12960000000),
-- ('Algeria','Africa',2381741,37100000,188681000000),
-- ('Andorra','Europe',468,78115,3712000000),
-- ('Angola','Africa',1246700,20609294,100990000000);
-- select * from World;
-- select name, population, area from World
-- where area >= 3000000 or population >=25000000
# Q52.
-- create table if not exists Customer
-- (
-- id int,
-- name varchar(50),
-- referee_id int,
-- constraint pk PRIMARY KEY (id)
-- );
-- insert into Customer VALUES (1,'Will',null),(2,'Jane',null),(3,'Alex',2),(4,'Bill',null),(5,'Zack',1),(6,'Mark',2);
-- select * from Customer;
-- SELECT name from Customer
-- where referee_id != 2
-- OR referee_id IS NULL;
# Q53.
-- create table if not exists customers(
-- id int,
-- name VARCHAR(250),
-- PRIMARY KEY (id)
-- );
-- create TABLE if not exists orders(
-- id int,
-- customerid int,
-- PRIMARY KEY (id),
-- constraint fk FOREIGN KEY (customerid) REFERENCES customers(id)
-- );
-- insert into customers VALUES (1,'Joe'),(2,'Henry'),(3,'Sam'),(4,'Max');
-- select * from customers;
-- insert into orders VALUES (1,3),(2,1);
-- select * from orders;
-- #Approach-1
-- select name from customers
-- where id not in (select customerid from orders );
-- #Approach-2
-- select name from customers c left join orders o
-- on c.id = o.customerid where o.customerid is null
# Q54.
-- create table if not exists Employee
-- (
-- employee_id int,
-- team_id int,
-- constraint pk PRIMARY KEY (employee_id)
-- );
-- insert into Employee VALUES (1,8),(2,8),(3,8),(4,7),(5,9),(6,9);
-- select * from Employee;
-- SELECT employee_id, count(team_id) over(PARTITION BY team_id ) as team_size from Employee
-- order by employee_id ;
# Q55.
-- create table if not exists Person
-- (
-- id int,
-- name VARCHAR(50),
-- phone_number VARCHAR(50),
-- constraint pk PRIMARY KEY (id)
-- );
-- insert into Person VALUES (3,'Jonathan','051-1234567'),(12,'Elvis','051-7654321'),(1,'Moncef','212-1234567'),(2,'Maroua','212-6523651'),(7,'Meir','972-1234567'),(9,'Rachel','972-0011100');
-- select * from Person;
-- create table if not exists Country
-- (
-- name VARCHAR(50),
-- country_code VARCHAR(50),
-- constraint pk PRIMARY KEY (country_code)
-- );
-- insert into Country VALUES ('Peru',51),('Israel',972),('Morocco',212),('Germany',49),('Ethiopia',251);
-- select * from Country;
-- create table if not exists Calls
-- (
-- caller_id int,
-- callee_id int,
-- duration int
-- );
-- insert into Calls VALUES (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);
-- select * from Calls;
-- with country_phone as (SELECT p.*, c.name as country_name FROM person p JOIN
-- (SELECT name,
-- CASE
-- WHEN LENGTH(country_code) < 3 then CONCAT("0", country_code)
-- else country_code
-- end as new_code
-- FROM country) as c
-- ON
-- left(p.phone_number, 3) = c.new_code
-- )
-- SELECT country_name, sum(total_dur)/sum(total_count) as final FROM
-- (SELECT cp.country_name, (2 * cal.duration) as total_dur, (2 * count(cp.country_name)) as total_count FROM calls as cal
-- JOIN
-- country_phone as cp
-- ON
-- cal.caller_id = cp.id
-- GROUP BY cp.country_name, duration) as tmp
-- GROUP BY country_name ORDER BY final DESC LIMIT 1
# Q56.
-- create table if not exists Activity
-- (
-- player_id int,
-- device_id int,
-- event_date date,
-- games_played int,
-- constraint pk PRIMARY KEY (player_id, event_date)
-- );
-- insert into Activity VALUES (1,2,'2016-03-01',5),(1,2,'2016-05-02',6),(2,3,'2017-06-25',1),(3,1,'2016-03-02',0),(3,4,'2018-07-03',5);
-- select * from Activity;
-- SELECT player_id,device_id FROM (SELECT player_id,device_id,
-- ROW_NUMBER() OVER(PARTITION BY player_id order by event_date) as rnk from Activity) TMP
-- WHERE TMP.rnk = 1
# Q57.
-- create table if not exists Orders
-- (
-- order_number int,
-- customer_number int,
-- constraint pk PRIMARY KEY (order_number)
-- );
-- insert into Orders VALUES (1,1),(2,2),(3,3),(4,3);
-- select * from Orders;
-- select customer_number
-- FROM Orders
-- group by customer_number
-- order by count(*) DESC
-- LIMIT 1;
# 58.
-- create table if not exists Cinema
-- (
-- seat_id int AUTO_INCREMENT,
-- free bool,
-- constraint pk PRIMARY KEY (seat_id)
-- );
-- insert into Cinema VALUES (1,1),(2,0),(3,1),(4,1),(5,1);
-- select * from Cinema;
--
-- select DISTINCT c1.seat_id from Cinema c1
-- INNER JOIN Cinema c2
-- on abs (c1.seat_id - c2.seat_id) = 1
-- and
-- c1.free = 1 and c2.free = 1
-- ORDER BY 1;
# Q59.
-- create table if not exists SalesPerson
-- (
-- sales_id int,
-- name VARCHAR(50),
-- salary int,
-- commission_rate int,
-- hire_date date,
-- constraint pk PRIMARY KEY (sales_id)
-- );
-- INSERT into SalesPerson VALUES (1,'John',100000,6,'2006-04-01'),(2,'Amy',12000,5,'2010-05-01'),(3,'Mark',65000,12,'2008-12-25'),
-- (4,'Pam',25000,25,'2005-01-01'),(5,'Alex',5000,10,'2007-02-03');
-- select * from SalesPerson;
-- create table if not exists Company
-- (
-- com_id int,
-- name VARCHAR(50),
-- city VARCHAR(50),
-- constraint pk PRIMARY KEY (com_id)
-- );
-- insert into Company VALUES (1,'RED','Boston'),(2,'ORANGE','New York'),(3,'YELLOW','Boston'),(4,'GREEN','Austin');
-- select * from Company;
-- create table if not exists Orders
-- (
-- order_id int,
-- order_date DATE,
-- com_id int,
-- sales_id int,
-- amount int,
-- constraint pk PRIMARY KEY (order_id),
-- constraint fk1 FOREIGN KEY (com_id) REFERENCES Company(com_id),
-- constraint fk2 FOREIGN KEY (sales_id) REFERENCES SalesPerson(sales_id)
-- );
-- insert into Orders VALUES (1,'2014-01-01',3,4,10000),(2,'2014-02-01',4,5,5000),(3,'2014-03-01',1,1,50000),(4,'2014-04-01',1,4,25000);
-- select * from Orders;
-- SELECT name
-- FROM Salesperson
-- WHERE sales_id
-- NOT IN (
-- SELECT s.sales_id FROM Orders o
-- INNER JOIN Salesperson s ON o.sales_id = s.sales_id
-- INNER JOIN Company c ON o.com_id = c.com_id
-- WHERE c.name = 'RED'
-- );
# Q60.
-- create table if not exists Triangle
-- (
-- x int,
-- y int,
-- z int,
-- constraint pk PRIMARY KEY (x,y,z)
-- );
-- insert into Triangle VALUES (13,15,30),(10,20,15);
-- select * from Triangle;
-- Select x, y, z,
-- (case
-- when x+y < z or x+z < y or y+z < x then "No"
-- when x=y and y=z then "Yes"
-- when x=y or x=z or y=z then "Yes"
-- when x<>y and y<>z then "Yes" end ) triangle
-- from Triangle
# Q61.
-- create table if not exists Point
-- (
-- x int,
-- constraint pk PRIMARY KEY (x)
-- );
-- insert into Point VALUES (-1),(0),(2);
-- select * from Point;
-- select min(abs(p2.x-p1.x)) as shortest
-- from Point p1, Point p2
-- where p1.x != p2.x;
# Q62.
-- create table if not exists ActorDirector
-- (
-- actor_id int,
-- director_id int,
-- timestamp int,
-- constraint pk PRIMARY KEY (timestamp)
-- );
-- insert into ActorDirector VALUES (1,1,0),(1,1,1),(1,1,2),(1,2,3),(1,2,4),(2,1,5),(2,1,6);
-- select * from ActorDirector;
-- SELECT actor_id, director_id
-- FROM ActorDirector
-- GROUP BY actor_id,director_id
-- HAVING COUNT(actor_id) >=3
# Q63.
-- create table if not exists Product
-- (
-- product_id int,
-- product_name varchar(50),
-- constraint pk PRIMARY KEY (product_id)
-- );
-- insert into Product VALUES (100,'Nokia'),(200,'Apple'),(300,'Samsung');
-- select * from Product;
-- create table if not exists Sales
-- (
-- sale_id int,
-- product_id int,
-- year int,
-- quantity int,
-- price int,
-- constraint pk PRIMARY KEY (sale_id, year),
-- constraint fk FOREIGN KEY (product_id) REFERENCES Product(product_id)
-- );
-- insert into Sales VALUES (1,100,2008,10,5000),(2,100,2009,12,5000),(7,200,2011,15,9000);
-- select * from Sales;
-- SELECT P.product_name,S.year, S.price
-- FROM Product P INNER JOIN Sales S
-- ON P.product_id = S.product_id
# Q64.
-- create table if not exists Employee
-- (
-- employee_id int,
-- name varchar(50),
-- experience_years int,
-- constraint pk PRIMARY KEY (employee_id)
-- );
-- insert into Employee VALUES (1,'Khaled',3),(2,'Ali',2),(3,'John',1),(4,'Doe',2);
-- select * from Employee;
-- create table if not exists Project
-- (
-- project_id int,
-- employee_id int,
-- constraint pk PRIMARY KEY (project_id, employee_id),
-- constraint fkp FOREIGN KEY (employee_id) REFERENCES Employee(employee_id)
-- );
-- insert into Project VALUES (1,1),(1,2),(1,3),(2,1),(2,4);
-- select * from Project;
-- SELECT project_id, ROUND(AVG(experience_years),2) AS avg_years FROM Employee E LEFT JOIN Project P
-- ON E.employee_id = P.employee_id
-- GROUP BY project_id
# Q65.
-- CREATE TABLE IF NOT EXISTS product(
-- product_id int,
-- product_name VARCHAR(50),
-- unit_price int,
-- PRIMARY KEY (product_id)
-- );
-- INSERT INTO product VALUES
-- (1,'S8',1000),
-- (2,'G4',800),
-- (3,'iPhone',1400);
-- SELECT * FROM product;
-- create table if not exists sales
-- (
-- seller_id int,
-- product_id int,
-- buyer_id int,
-- sale_date date,
-- quantity int,
-- price int,
-- constraint fk FOREIGN KEY (product_id) REFERENCES Product(product_id)
-- );
-- insert into sales VALUES (1,1,1,'2019-01-21',2,2000),(1,2,2,'2019-02-17',1,800),(2,2,3,'2019-06-02',1,800),(3,3,4,'2019-05-13',2,2800);
-- select * from sales;
-- SELECT DISTINCT t.seller_id from (select seller_id, sum(price) over(partition by seller_id) as total_Sales from sales s inner join product p
-- on s.product_id = p.product_id) t
-- where t.total_Sales = 2800;
# Q66.
-- select buyer_id from sales s inner join product p
-- on s.product_id = p.product_id
-- where product_name = 'S8'
-- and buyer_id not in
-- (
-- select buyer_id from sales s
-- join product p on p.product_id = s.product_id
-- where p.product_name = 'iPhone'
-- );
# Q67.
-- create table if not exists Customer
-- (
-- customer_id int,
-- name VARCHAR(50),
-- visited_on date,
-- amount int,
-- constraint pk PRIMARY KEY (customer_id, visited_on)
-- );
-- INSERT into Customer VALUES (1,'Jhon','2019-01-01',100),(2,'Daniel','2019-01-02',110),(3,'Jade','2019-01-03',120),(4,'Khaled','2019-01-04',130),(5,'Winston','2019-01-05',110),(6,'Elvis','2019-01-06',140),(7,'Anna','2019-01-07',150),(8,'Maria','2019-01-08',80),(9,'Jaze','2019-01-09',110),(1,'Jhon','2019-01-10',130),(3,'Jade','2019-01-10',150);
-- select * from Customer;
-- with cte as (
-- SELECT visited_on,
-- sum(amount) AS total_amount FROM
-- Customer
-- GROUP BY visited_on
-- ORDER BY visited_on
-- ) ,
-- cte2 as (
-- SELECT visited_on,
-- SUM(total_amount) over(rows BETWEEN 6 preceding and current row) as amount,
-- round(avg(total_amount) over(rows BETWEEN 6 preceding and current row),2) as average_amount,
-- dense_rank() over(order by visited_on) as rnk
-- FROM cte
-- )
-- Select visited_on, amount, average_amount from cte2
-- where rnk > 6;
# Q68.
-- create table if not exists Scores
-- (
-- player_name VARCHAR(50),
-- gender varchar(50),
-- day date,
-- score_points int,
-- constraint pk PRIMARY KEY (gender, day)
-- );
-- insert into Scores VALUES ('Aron','F','2020-01-01',17),('Alice','F','2020-01-07',23),('Bajrang','M','2020-01-07',7),('Khali','M','2019-12-25',11),('Slaman','M','2019-12-30',13),('Joe','M','2019-12-31',3),('Jose','M','2019-12-18',2),('Priya','F','2019-12-31',23),('Priyanka','F','2019-12-30',17);
-- select * from Scores
-- ORDER BY day ;
-- SELECT gender,day, sum(score_points) over(PARTITION BY gender order by day ) as total from Scores
# Q69.
-- create table if not exists Logs
-- (
-- log_id int,
-- constraint pk PRIMARY KEY (log_id)
-- );
-- insert into Logs VALUES (1),(2),(3),(7),(8),(10);
-- select * from Logs;
-- select min(log_id) as start_id, max(log_id) as end_id
-- from (select l.log_id, (l.log_id - l.row_num) as diff
-- from (select log_id, row_number() over() as row_num from Logs) l
-- ) l2
-- group by diff;
# Q70.
-- create table if not exists Students
-- (
-- student_id int,
-- student_name VARCHAR(50),
-- constraint pk PRIMARY KEY (student_id)
-- );
-- insert into Students VALUES (1,'Alice'),(2,'Bob'),(13,'John'),(6,'Alex');
-- select * from Students;
-- create table if not exists Subjects
-- (
-- subject_name VARCHAR(50),
-- constraint pk PRIMARY KEY (subject_name)
-- );
-- insert into Subjects VALUES ('Math'),('Physics'),('Programming');
-- select * from Subjects;
-- create table if not exists Examinations
-- (
-- student_id int,
-- subject_name VARCHAR(50)
-- );
-- INSERT into Examinations VALUES (1,'Math'),(1,'Physics'),(1,'Programming'),(2,'Programming'),(1,'Physics'),(1,'Math'),(13,'Math'),(13,'Programming'),(13,'Physics'),(2,'Math'),(1,'Math');
-- select * from Examinations;
-- select a.student_id, b.subject_name, count(b.subject_name) as attended_exams
-- from Students as a
-- join Subjects as b
-- left join Examinations as c
-- on a.student_id = c.student_id and b.subject_name = c.subject_name
-- group by a.student_id, b.subject_name;
# Q71.
-- create table if not exists Employees
-- (
-- employee_id int,
-- employee_name VARCHAR(50),
-- manager_id int,
-- constraint pk PRIMARY KEY (employee_id)
-- );
-- insert into Employees VALUES (1,'Boss',1),(3,'Alice',3),(2,'Bob',1),(4,'Daniel',2),(7,'Luis',4),(8,'Jhon',3),(9,'Angela',8),(77,'Robert',1);
-- select * from Employees;
-- select e3.employee_id from Employees e1, Employees e2, Employees e3
-- where e1.manager_id = 1 and e2.manager_id = e1.employee_id and e3.manager_id = e2.employee_id and e3.employee_id != 1;
# Q72.
-- create table if not exists Transactions
-- (
-- id int,
-- country VARCHAR(50),
-- state enum('approved', 'declined'),
-- amount int,
-- trans_date date,
-- constraint pk PRIMARY KEY (id)
-- );
-- insert into Transactions VALUES (121,'US','approved',1000,'2018-12-18'),(122,'US','declined',2000,'2018-12-19'),
-- (123,'US','approved',2000,'2019-01-01'),(124,'DE','approved',2000,'2019-01-07');
-- select * from Transactions;
-- select country,
-- DATE_FORMAT(trans_date, '%Y-%m') AS month,
-- sum(amount) as trans_total_amount,
-- sum(if(state = 'approved', amount, 0)) as approved_total_amount,
-- count(if(state = 'approved', state, NULL)) as approved_count,
-- count(id) as trans_count
-- from Transactions
-- GROUP BY month, country;
# Q73.
-- create table if not exists Actions
-- (
-- user_id int,
-- post_id int,
-- action_date date,
-- action enum('view', 'like', 'reaction', 'comment', 'report', 'share'),
-- extra VARCHAR(50)
-- );
-- insert into Actions VALUES (1,1,'2019-07-01','view',null),(1,1,'2019-07-01','like',null),(1,1,'2019-07-01','share',null),(2,2,'2019-07-04','view',null),(2,2,'2019-07-04','report','spam'),(3,4,'2019-07-04','view',null),(3,4,'2019-07-04','report','spam'),(4,3,'2019-07-02','view',null),(4,3,'2019-07-02','report','spam');
-- select * from Actions;
-- create table if not exists Removals
-- (
-- post_id int,
-- remove_date date,
-- constraint pk PRIMARY KEY (post_id)
-- );
-- insert into Removals VALUES (2,'2019-07-20'),(3,'2019-07-18');
-- select * from Removals;
-- SELECT * FROM Actions a INNER JOIN Removals r
-- ON a.post_id = r.post_id;
-- SELECT ROUND(AVG(percentage),2) AS average_daily_percent
-- FROM (
-- SELECT action_date,
-- (COUNT(DISTINCT b.post_id)/COUNT(DISTINCT a.post_id))*100 AS percentage
-- FROM Actions AS a
-- LEFT JOIN Removals AS b
-- ON a.post_id = b.post_id
-- WHERE a.action = 'report'
-- AND a.extra = 'spam'
-- GROUP BY a.action_date
-- ) AS tmp;
# Q74.
-- create table if not exists Activity
-- (
-- player_id int,
-- device_id int,
-- event_date date,
-- games_played int,
-- constraint pk PRIMARY KEY (player_id, event_date)
-- );
-- insert into Activity VALUES (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);
-- select * from Activity;
-- select player_id, abs(min(event_date) - max(event_date)) as diff
-- from Activity
-- GROUP BY player_id;
-- select player_id, min(event_date),max(event_date)
-- from Activity
-- GROUP BY player_id;
-- with temp as (
-- select player_id, abs(min(event_date) - max(event_date)) as diff
-- from Activity
-- GROUP BY player_id
-- )
-- SELECT
-- round((count(distinct c.player_id)/ (select count(distinct player_id) from Activity)),2) as fraction
-- from temp c
-- inner join Activity a
-- on c.player_id = a.player_id
-- where c.diff = 1;
# Q75.
-- create table if not exists Activity
-- (
-- player_id int,
-- device_id int,
-- event_date date,
-- games_played int,
-- constraint pk PRIMARY KEY (player_id, event_date)
-- );
-- insert into Activity VALUES (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);
-- select * from Activity;
-- select player_id, abs(min(event_date) - max(event_date)) as diff
-- from Activity
-- GROUP BY player_id;
-- select player_id, min(event_date),max(event_date)
-- from Activity
-- GROUP BY player_id;
-- with temp as (
-- select player_id, abs(min(event_date) - max(event_date)) as diff
-- from Activity
-- GROUP BY player_id
-- )
-- SELECT
-- round((count(distinct c.player_id)/ (select count(distinct player_id) from Activity)),2) as fraction
-- from temp c
-- inner join Activity a
-- on c.player_id = a.player_id
-- where c.diff = 1;
# Q76.
-- create table if not exists Salaries
-- (
-- company_id int,
-- employee_id int,
-- employee_name VARCHAR(50),
-- salary int,
-- constraint pk PRIMARY KEY (company_id, employee_id)
-- );
-- insert into Salaries VALUES (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,'Nyan Cat',3300),(3,15,'Morning Cat',7777);
-- SELECT * FROM Salaries;
-- with cte as (
-- SELECT *,max(salary) over(partition by company_id ) as max_salary FROM Salaries
-- )
-- SELECT company_id, employee_id, employee_name,
-- (CASE WHEN max_salary < 1000 THEN salary
-- WHEN max_salary >= 1000 AND max_salary<=10000 THEN round((salary - (.24)*salary))
-- ELSE round((salary - (.49)*salary)) END) salary
-- FROM cte
# Q77.
-- create table if not exists Variables
-- (
-- name varchar(50),
-- value int,
-- constraint pk PRIMARY KEY (name)
-- );
-- insert into Variables VALUES ('x',66),('y',77);
-- select * from Variables;
-- create table if not exists Expressions
-- (
-- left_operand varchar(50),
-- operator enum ('<', '>', '='),
-- right_operand VARCHAR(50),
-- constraint pk PRIMARY KEY (left_operand, operator, right_operand)
-- );
-- insert into Expressions VALUES ('x','>','y'),('x','<','y') ,('x','=','y'),('y','>','x'),('y','<','x'),('x','=','x');
-- select * from Expressions;
-- WITH cte as (
-- select * from Expressions
-- )
-- SELECT *,
-- CASE
-- when operator = "<" and (left_operand < right_operand) = 1 then "true"
-- when operator = ">" and (left_operand > right_operand) = 1 then "true"
-- when operator = "=" and (left_operand = right_operand) = 1 then "true"
-- else "false"
-- end as value
-- FROM CTE;
# Q78.
-- create table if not exists Person
-- (
-- id int,
-- name VARCHAR(50),
-- phone_number VARCHAR(50),
-- constraint pk PRIMARY KEY (id)
-- );
-- insert into Person VALUES (3,'Jonathan','051-1234567'),(12,'Elvis','051-7654321'),(1,'Moncef','212-1234567'),(2,'Maroua','212-6523651'),(7,'Meir','972-1234567'),(9,'Rachel','972-0011100');
-- select * from Person;
-- create table if not exists Country
-- (
-- name VARCHAR(50),
-- country_code VARCHAR(50),
-- constraint pk PRIMARY KEY (country_code)
-- );
-- insert into Country values ('Peru',51),('Israel',972),('Morocco',212),('Germany',49),('Ethiopia',251);
-- select * from Country;
-- create table if not exists Calls
-- (
-- caller_id int,
-- callee_id int,
-- duration int
-- );
-- insert into Calls VALUES (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);
-- select * from Calls;
-- with country_phone as (SELECT p.*, c.name as country_name FROM person p JOIN
-- (SELECT name,
-- CASE
-- WHEN LENGTH(country_code) < 3 then CONCAT("0", country_code)
-- else country_code
-- end as new_code
-- FROM country) as c
-- ON
-- SUBSTRING(p.phone_number,1,3) = c.new_code
-- )
-- SELECT
-- p.country_name AS country,AVG(duration)
-- FROM
-- country_phone p
-- JOIN
-- Calls c
-- ON p.id IN (c.caller_id, c.callee_id)
-- GROUP BY
-- p.country_name
-- HAVING
-- AVG(duration) > (SELECT AVG(duration) FROM Calls);
# Q79.
-- create table if not exists Employee
-- (
-- employee_id int,
-- name VARCHAR(50),
-- months int,
-- salary int
-- );
-- insert into Employee VALUES (12228,'Rose',15,1968),(33645,'Angela',1,3443),(45692,'Frank',17,1608),
-- (56118,'Patrick',7,1345),(59725,'Lisa',11,2330),(74197,'Kimberly',16,4372),(78454,'Bonnie',8,1771),
-- (83565,'Michael',6,2017),(98607,'Todd',5,3396),(99989,'Joe',9,3573);
-- select * from Employee;
-- SELECT name from Employee order by name;
# Q80.
-- create table if not exists user_transactions
-- (
-- transaction_id int,
-- product_id int,
-- spend decimal (7,2),
-- transaction_date DATETIME
-- );
-- #YYYY-MM-DD hh:mm:ss -datetime
-- insert into user_transactions VALUES (1341,123424,1500.60,'2019-12-31 12:00:00'),(1423,123424,1000.20,'2020-12-31 12:00:00'),
-- (1623,123424,1246.44,'2021-12-31 12:00:00'),(1322,123424,2145.32,'2022-12-31 12:00:00');
-- select * from user_transactions;
-- select product_id, spend as curr_year_spend,
-- lag(spend,1) over(order by extract(YEAR FROM transaction_date)) AS prev_year_spend,
-- round(((spend - lag(spend,1) over(order by extract(YEAR FROM transaction_date)))/lag(spend,1) over(order by extract(YEAR FROM transaction_date))) * 100,2) as yoy_rate
-- from user_transactions;
# Q81.
-- create table if not exists inventory
-- (
-- item_id int,
-- item_type VARCHAR(50),
-- item_category VARCHAR(50),
-- square_footage DECIMAL(4,2)
-- );
-- insert into inventory VALUES (1374,'prime_eligible','mini refrigerator',68.00),(4245,'not_prime','standing lamp',26.40),
-- (2452,'prime_eligible','television',85.00),(3255,'not_prime','side table',22.60),(1672,'prime_eligible','laptop',8.50);
-- select * from inventory;
-- WITH product_inventory_summary AS
-- (
-- SELECT
-- item_type,
-- SUM(square_footage) as square_footage_required,
-- COUNT(item_id) as unique_item_count,
-- 500000 as total_space,
-- FLOOR(500000/sum(square_footage))*sum(square_footage) as space_used,
-- FLOOR(500000/sum(square_footage))*COUNT(item_id) as item_count
-- FROM
-- inventory
-- GROUP BY
-- item_type
-- )
-- SELECT
-- t1.item_type,
-- CASE
-- WHEN t1.item_type = 'prime_eligible'
-- THEN t1.item_count
-- ELSE
-- FLOOR((500000-t2.space_used)/t1.square_footage_required)*t1.unique_item_count
-- END AS item_count
-- FROM
-- product_inventory_summary t1
-- JOIN product_inventory_summary t2 ON t1.item_type <> t2.item_type
-- ORDER BY t1.item_type DESC
# Q82.
-- CREATE TABLE user_actions
-- (
-- user_id INT,
-- event_id INT,
-- event_type VARCHAR(20),
-- event_date DATE
-- );
-- INSERT INTO user_actions VALUES(445, 7765 , 'sign-in', '2022-05-31');
-- INSERT INTO user_actions VALUES(742, 6458, 'sign-in', '2022-06-03');
-- INSERT INTO user_actions VALUES(445, 3634, 'like', '2022-06-05');
-- INSERT INTO user_actions VALUES(742, 1374, 'comment', '2022-06-05');
-- INSERT INTO user_actions VALUES(648, 3124, 'like', '2022-06-18');
-- select * from user_actions;
-- with temp as (SELECT *, count(user_id) over(PARTITION BY user_id) as t_count,
-- lag(event_date, 1) over() as new_col
-- from user_actions)
--
-- select distinct extract(month from t.event_date) as month,
-- count(t.user_id) as monthly_active_users
-- from temp t
-- where abs(extract(month from t.new_col) - extract(month from t.event_date)) = 1
-- group by extract(month from t.event_date)
# Q85.
-- CREATE TABLE server_utilization
-- (
-- server_id INT,
-- status_time TIMESTAMP,
-- session_status VARCHAR(10)
-- );
-- INSERT INTO server_utilization VALUES(1, '2022-08-02 10:00:00', 'start');
-- INSERT INTO server_utilization VALUES(1, '2022-08-04 10:00:00', 'stop');
-- INSERT INTO server_utilization VALUES(2, '2022-08-17 10:00:00', 'start');
-- INSERT INTO server_utilization VALUES(2, '2022-08-24 10:00:00', 'stop');
-- select * from server_utilization;
-- with temp as (select *, lag(status_time,1) OVER(partition by server_id ORDER BY status_time) as new_status_time from server_utilization)
-- SELECT sum(TIMESTAMPDIFF(day, t.new_status_time,t.status_time)) as total_uptime_days from temp t;
# Q86.
-- CREATE TABLE transactions
-- (
-- transaction_id INT,
-- merchant_id INT,
-- credit_card_id INT,
-- amount INT,
-- transaction_timestamp TIMESTAMP
-- );
-- INSERT INTO transactions VALUES(1, 101, 1, 100, '2022-09-25 12:00:00');
-- INSERT INTO transactions VALUES(2, 101, 1, 100, '2022-09-25 12:08:00');
-- INSERT INTO transactions VALUES(3, 101, 1, 100, '2022-09-25 12:28:00');
-- INSERT INTO transactions VALUES(4, 102, 2, 300, '2022-09-25 12:00:00');
-- INSERT INTO transactions VALUES(5, 102, 2, 400, '2022-09-25 14:00:00');
-- SELECT * from transactions;
-- -- Approach 1
-- select *, lag(transaction_timestamp,1) over(PARTITION BY credit_card_id ) as new_col,
-- row_number() over(PARTITION BY credit_card_id order by amount) AS serial_number,
-- rank() over(PARTITION BY credit_card_id order by amount) AS serial_rank,
-- dense_rank() over(PARTITION BY credit_card_id order by amount) AS dense_number
-- from transactions;
-- with temp as (
-- select *, lag(transaction_timestamp,1) over(PARTITION BY credit_card_id ) as new_col
-- from transactions
-- ),
-- temp1 as (
-- select *,TIMESTAMPDIFF(minute,t.new_col, t.transaction_timestamp) as minute_diff from temp t
-- )
-- Select count(a.credit_card_id) as payment_count from temp1 a where a.minute_diff <= 10;
-- Approach 2
-- WITH trx_with_repeadted AS
-- (
-- SELECT
-- credit_card_id,
-- amount,
-- transaction_timestamp,
-- count(*) OVER(
-- PARTITION BY credit_card_id,amount
-- ORDER BY transaction_timestamp