-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample.sql
More file actions
692 lines (644 loc) · 9.77 KB
/
sample.sql
File metadata and controls
692 lines (644 loc) · 9.77 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
SELECT
ID,
数量
FROM
test_table
SELECT
*
FROM
test_table
WHERE
ID > 2
SELECT
*
from
test_table
WHere
ID < 10
ORDER by
単価 DESC,
売上金額
SELECT
商品名,
sum(売上金額),
count(*),
count(商品名)
FROM
test_table
GROUP by
商品名
ORDER by
sum(売上金額)
SELECT
*
FROM
test_table
WHERE
商品名 != 'ニット'
GROUP by
商品名
HAVING
sum(売上金額) > 10000
ORDER by
売上金額
SELECT
*
from
test_table
WHERE
売上金額 Between 10000
and 200000
SELECT
*
from
test_table
WHERE
商品名 Like '%A%'
SELECT
Distinct JOB
from
EMP
INSERT INTO
test_table (name, price, tax)
VALUES
('test', 1000, 0.1)
SELECT
A.氏名,
A.数学,
B.国語
from
test01 as A
INNER JOIN test02 as B on A.氏名 = B.氏名
SELECT
*
from
test_table
WHERE
not 商品名 = 'ニット';
SELECT
*
from
test_table
WHere
商品名 is null;
SELECT
*
from
test_table
WHere
商品名 is not null;
SELECT
*
from
test_table
where
商品名 = 'ニット'
and 売上金額 = 3000
SELECT
*
from
test_table
where
商品名 = 'ニット'
or 売上金額 = 3000
SELECT
*
from
test_table
ORDER by
売上金額 DESC
limit
10;
SELECT
商品名 as a
from
test_table as test
SELECT
*
from
test_tablea
where
> (
SELECT
売上金額
from
test_table
where
単価 > 1000
);
SELECT
*
FROM
players
join countries on players.country_id = countries.id
left join teams on players.previous_team_id = teams.id;
SELECT
countries.name as '国名',
avg(goals) as '平均得点'
FROM
players
join countries on countries.id = players.country_id
group by
countries.name;
select
name,
price,
price - cost
from
items
select
avg(price - cost)
from
items;
select
name,
price - cost
from
items
order by
price - cost desc
limit
5;
select
name,
price
from
items
where
price > (
select
price
from
items
where
name = 'グレーパーカー'
);
select
name,
price - cost
from
items
where
price <= 7000
and price - cost > (
select
price - cost
from
items
where
name = 'グレーパーカー'
);
select
item_id,
count(item_id)
from
sales_records
group by
item_id;
select
item_id,
count(item_id)
from
sales_records
group by
item_id
order by
count(item_id) desc
limit
5;
select
i.id,
i.name,
count(sale.item_id)
from
items as i
join sales_records as sale on i.id = sale.item_id
group by
sale.item_id
order by
count(sale.item_id) desc
limit
5;
select
sum(i.price) as '総売上',
sum(i.price - i.cost) as '総利益'
from
items as i
join sales_records as sale on i.id = sale.item_id
select
purchased_at,
count(purchased_at) as '販売個数'
from
sales_records
group by
purchased_at
order by
purchased_at;
select
purchased_at,
sum(price) as '売上額'
from
items as i
join sales_records as sale on i.id = sale.item_id
group by
purchased_at
order by
purchased_at
select
u.id,
u.name,
count(sale.user_id) as '購入数'
from
users as u
join sales_records as sale on u.id = sale.user_id
group by
u.id
having
count(u.id) >= 10;
select
distinct u.id,
u.name
from
users as u
join sales_records as sale on u.id = sale.user_id
where
sale.item_id = (
select
id
from
items
where
name = 'サンダル'
);
select
i.gender,
sum(i.price) as '売上額'
from
sales_records as sale
join items as i on sale.item_id = i.id
group by
i.gender;
select
i.id,
i.name,
sum(price) as '売上額'
from
sales_records as sale
join items as i on sale.item_id = i.id
group by
i.id
order by
sum(price) desc
limit
5;
select
i.id,
i.name,
sum(price)
from
sales_records as sale
join items as i on sale.item_id = i.id
group by
i.id
having
sum(price) > (
select
sum(price)
from
sales_records as sale
join items as i on sale.item_id = i.id
group by
i.id
having
i.name = 'グレーパーカー'
);
Insert into
students(name, course)
values
('Kate', 'Java');
update
students
set
name = 'jordan',
course = 'HTML'
WHERE
id = 6;
delete from
students
where
id = 7;
select
release_year,
count(*) as books_num
from
books
group by
release_year
having
count(*) >= 2
order by
books_num desc;
select
*
from
(
select
shohin_bunrui,
count(*) as cnt_shohin
from
shohin
group by
shohin_bunrui
) as shohin_bunrui_num
where
cnt_shohin >= 4;
create view ViewRenshu5_1 (shohim_mei, hanbai_tanka, torokubi) as
select
shohin_mei,
hanbai_tanka,
torokubi
from
shohin
where
hanbai_tanka >= 1000
and torokubi = '2009-09-20';
create view AvgTankaByBunrui as
select
shohin_id,
shohin_mei,
shohin_bunrui,
hanbai_tanka,
(
select
avg(hanbai_tanka)
from
shohin as s2
where
s2.shohin_bunrui = s1.shohin_bunrui
group by
s2.shohin_bunrui
) as avg_hanbai_tanka
from
shohin as s1
select
count(
case
when hanbai_tanka <= 1000 then hanbai_tanka
end
) as low_price,
count(
case
when hanbai_tanka between 1001
and 3000 then hanbai_tanka
end
) as mid_price,
count(
case
when hanbai_tanka >= 3001 then hanbai_tanka
end
) as high_price
from
shohin;
select
id,
name,
(
select
case
when salary < 50000 then '低'
when salary between 50000
and 99999 then '中'
when salary >= 100000 then '高'
end
) as level
from
employees;
select
id,
name,
(
case
when category in ('A', 'B') then 'グループ1'
when category = 'C' then 'グループ2'
else 'その他'
end
) as category
from
products;
select
gender,
count(*) as num
from
authors
group by
gender
order by
num desc;
select
id,
postion,
name,
height,
weight
from
players
where
height > 195
union
all
select
id,
postion,
name,
height,
weight
from
players
where
weight > 95
order by
id;
select
p.id,
p.price,
s.saleNum
from
product as p
inner join sale as s on p.id = s.id;
select
b.book_name,
a.author_name
from
books as b
inner join book_authors as ba on b.id = ba.book_id
inner join authors as a on ba.author_id = a.id;
select
shohim_mei,
shohin_bunrui,
hanbai_tanka,
rank() over (
partition by shohin_bunrui
order by
hanbai_tanka
) as ranking
from
shohin;
select
shohin_mei,
shohin_bunrui,
hanbai_tanka,
rank() over (
order by
hanbai_tanka
) as ranking
from
shohin;
select
shohin_mei,
shohin_bunrui,
hanbai_tanka,
rank() over (
order by
hanbai_tanka
) as ranking,
dense_rank() (
order by
hanbai_tanka
) as dense_rank,
row_number () (
order by
hanbai_tanka
) as row_num
from
shohin;
insert into
shohin
values
(
'0001',
'Tシャツ',
'衣服',
1000,
500,
'2009-09-20'
);
insert into
shohin
values
(
'0002',
'穴あけパンチ',
'事務用品',
500,
320,
'2009-09-11'
);
insert into
shohin
values
('0003', 'カッターシャツ', '衣服', 4000, 2800, null);
insert into
shohin
values
(
'0004',
'包丁',
'キッチン用品',
3000,
2800,
'2009-09-20'
);
insert into
shohin
values
(
'0005',
'圧力鍋',
'キッチン用品',
6800,
5000,
'2009-01-15'
);
insert into
shohin
values
(
'0006',
'フォーク',
'キッチン用品',
500,
null,
'2009-09-20'
);
insert into
shohin
values
(
'0007',
'おろしがね',
'キッチン用品',
880,
790,
'2008-04-28'
);
insert into
shohin
values
(
'0008',
'ボールペン',
'事務用品',
100,
null,
'2009-11-11'
);
select
shohin_id,
shohin_mei,
hanbai_tanka,
sum(hanbai_tanka) over (
order by
shohin_id
) as current_sum
from
shohin;
select
shohin_id,
shohin_mei,
hanbai_tanka,
min(hanbai_tanka) over (
order by
shohin_id
)
from
shohin;
select
shohin_id,
shohin_mei,
hanbai_tanka,
avg(hanbai_tanka) over(
order by
shohin_id rows 2 preceding
) as mov ing_avg
from
shohin;
select
shohin_id,
shohin_mei,
hanbai_tanka,
sum(hanbai_tanka) over (
order by
shohin_id rows between 1 precedi ng
and 1 following
) as sum
from
shohin;
select
avg(amount) over (
partition by store_id rows between 6 precedeing
and current row
order by
sale_date
)
from
sales;