-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand_log.sql
5880 lines (4870 loc) · 131 KB
/
command_log.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
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
CREATE VIEW "sh_align" AS
SELECT superhero_name, full_name, alignment_id
FROM superhero;
.mode insert <superpeople>
.out superpeople.sql
select * from "sh_align";
SELECT "superhero_name", "full_name", COUNT("id") AS "count"
FROM "superhero"
GROUP BY "superhero_name"
HAVING COUNT("id") > 1
ORDER BY "count" DESC;
SELECT "alias", "full_name", COUNT("alias") AS "count"
FROM "temp1"
GROUP BY "alias"
HAVING COUNT("id") > 1
ORDER BY "count" DESC;
-- Select repeated "superhero_name"
SELECT * FROM "superhero"
WHERE "superhero_name" IN (
SELECT "superhero_name" FROM (
SELECT "superhero_name", "full_name", COUNT("id") AS "count"
FROM "superhero"
GROUP BY "superhero_name"
HAVING COUNT("id") > 1
ORDER BY "count" DESC
)
);
------
SELECT "superhero_name" FROM "superhero"
WHERE "morality_rate" IS NULL;
SELECT "alias" FROM "temp1"
WHERE "morality_rate" IS NULL;
SELECT
s."superhero_name",
p."publisher_name"
FROM "superhero" s
JOIN "publisher" p
ON s."publisher_id" = p."id"
WHERE s."superhero_name" IN (
SELECT "alias" FROM "temp1"
WHERE "morality_rate" IS NULL
);
SELECT
ROW_NUMBER() OVER (ORDER BY "superhero_name") AS "temp_id", -- adds a column "temp_id" to enumerate the output rows
"superhero_name"
FROM
"superhero"
WHERE
"morality_rate" IS NULL;
SELECT "morality_rate" FROM "temp1"
WHERE "alias" IN (
SELECT "superhero_name" FROM "superhero"
WHERE "morality_rate" IS NULL
);
-- Query with subquerie and with an added column to enumerate the output rows.
WITH superhero_with_id AS (
SELECT
ROW_NUMBER() OVER (ORDER BY "superhero_name") AS "temp_id",
"superhero_name"
FROM
"superhero"
WHERE
"morality_rate" IS NULL
)
SELECT
t.*,
s."temp_id"
FROM
"temp1" t
JOIN
superhero_with_id s
ON
t."alias" = s."superhero_name";
-- Query with subquery and with an added column to enumerate the output rows.
WITH superhero_with_id AS (
SELECT
ROW_NUMBER() OVER (ORDER BY "id") AS "temp_id",
"id"
FROM
"superhero"
WHERE
"full_name" IS NULL
)
SELECT
s."id",
s."superhero_name",
s."full_name",
"temp_id"
FROM "superhero" s
JOIN "superhero_with_id" i
ON s."id" = i."id"
WHERE s."full_name" IS NULL;
SELECT "id", "superhero_name". "full_name" FROM "superhero" WHERE "full_name" IS NULL;
SELECT * FROM "superhero" WHERE "superhero_name" = 'Absorbing Man';
UPDATE "superhero"
SET "morality_rate" = (
SELECT "morality_rate"
FROM "temp1"
WHERE "temp1"."alias" = "superhero"."superhero_name"
)
WHERE "morality_rate" IS NULL
AND "alias" IN (
SELECT "superhero_name"
FROM "superhero"
WHERE "morality_rate" IS NULL
);
UPDATE "superhero"
SET "morality_rate" = (
SELECT "morality_rate"
FROM "temp1"
WHERE "temp1"."alias" = "superhero"."superhero_name"
)
WHERE "morality_rate" IS NULL;
UPDATE "superhero"
SET "morality_rate" = 8
WHERE "id" = 358;
UPDATE "superhero"
SET "morality_rate" = 6
WHERE "id" = 378;
----
UPDATE "superhero"
SET "full_name" = 'Carol Susan Jane Danvers'
WHERE "id" = 161;
UPDATE "superhero"
SET "full_name" = "William Joseph 'Billy' Batson"
WHERE "id" = 160;
----
CREATE VIEW "has_null" AS
SELECT * FROM "superhero"
WHERE id IS NULL
OR superhero_name IS NULL
OR full_name IS NULL
OR gender_id IS NULL
OR eye_colour_id IS NULL
OR hair_colour_id IS NULL
OR skin_colour_id IS NULL
OR race_id IS NULL
OR publisher_id IS NULL
OR alignment_id IS NULL
OR height_cm IS NULL
OR weight_kg IS NULL
OR morality_rate IS NULL;
SELECT "id","superhero_name","full_name" FROM "superhero"
WHERE
"superhero_name" IS NULL
OR "superhero_name" = ''
OR "superhero_name" = '-'
OR "full_name" IS NULL
OR "full_name" = ''
OR "full_name" = '-';
SELECT
"superhero_name",
"full_name"
FROM
"superhero"
WHERE
"superhero_name" LIKE '%"%'
OR "full_name" LIKE '%"%'
-- Update "superhero" from "superhero_updates"
UPDATE "superhero"
SET "full_name" = (
SELECT "full_name"
FROM "superhero_updates"
WHERE "superhero_updates"."id" = "superhero"."id"
);
WHERE "id" IN (
SELECT "id" FROM "superhero_updates"
);
CREATE VIEW "sh_names" AS
SELECT "id", "superhero_name", "full_name"
FROM "superhero";
sqlite3 c:/sqlite/chinook.db <<EOF
.headers on
.mode csv
.output sh_names.csv
SELECT * FROM sh_names;
.quit
EOF
SELECT MAX("id") as "max_id"
FROM "superhero";
-- To look for "missing" ids (< than the greatest id but absent)
WITH RECURSIVE all_ids AS (
SELECT MIN("id") AS id
FROM "superhero"
UNION ALL
SELECT id + 1
FROM all_ids
WHERE id + 1 <= (SELECT MAX("id") FROM "superhero")
)
SELECT id
FROM all_ids
WHERE id NOT IN (SELECT "id" FROM "superhero");
ALTER TABLE "superhero"
ADD COLUMN "fee" INTEGER;
-- Update preventing changein unmatch to NULL:
---- Whithout CASE keyword:
UPDATE "superhero"
SET "fee" = (
SELECT "fee" FROM "fee_updates"
WHERE "fee_updates"."id" = "superhero"."id"
)
WHERE "id" IN (
SELECT "id" FROM "fee_updates"
)-- This last WHERE statment is not extrictly necesary here but is a good practice to prevent unwanted changes in other cases.
;
-- Update preventing changein unmatch to NULL:
---- Whith CASE keyword:
UPDATE "table_name"
SET "columnx_name" = CASE
WHEN "columny_name" = value THEN value
WHEN "columny_name" = value THEN value
WHEN "columny_name" = value THEN value
WHEN "columny_name" = value THEN value
ELSE "columnx_name" -- To leave unchanged if no match
END;
-- Check
SELECT "id", "superhero_name", "full_name", "morality_rating", "fee"
FROM "superhero";
SELECT "id", "superhero_name", "full_name"
FROM "superhero"
ORDER BY "full_name" ASC
LIMIT 5;
UPDATE "superhero"
SET "full_name" = 'William Joseph "Billy" Batson'
WHERE "id" = 160;
CREATE TABLE "names_complete" (
"id" INTEGER PRIMARY KEY,
"superhero_name" TEXT,
"full_name" TEXT
);
.import --csv --skip 1 names_complete.csv names_complete
UPDATE "superhero"
SET "full_name" = (
SELECT "full_name"
FROM "names_complete"
WHERE "names_complete"."id" = "superhero"."id"
)
WHERE "id" IN (
SELECT "id" FROM "names_complete"
);
UPDATE "superhero"
SET "fee" = 1000
WHERE "id" IN (293, 342, 402);
CREATE TABLE superentity (
"id" INTEGER PRIMARY KEY,
"superhero_name" TEXT NOT NULL,
"full_name" TEXT,
"gender_id" INTEGER,
"race_id" INTEGER,
"publisher_id" INTEGER,
"morality_rating" INTEGER CHECK ("morality_rating" BETWEEN 1 AND 10),
"fee" INTEGER,
FOREIGN KEY ("gender_id") REFERENCES "gender"("id"),
FOREIGN KEY ("publisher_id") REFERENCES "publisher"("id"),
FOREIGN KEY ("race_id") REFERENCES "race"("id")
);
INSERT INTO "superentity"
SELECT
"id",
"superhero_name",
"full_name",
"gender_id",
"race_id",
"publisher_id",
"morality_rating",
"fee"
FROM "superhero";
ALTER TABLE "superentity"
CREATE TABLE entity_attribute (
entity_id INTEGER DEFAULT NULL,
attribute_id INTEGER DEFAULT NULL,
attribute_value INTEGER DEFAULT NULL,
CONSTRAINT fk_hat_at FOREIGN KEY (attribute_id) REFERENCES attribute (id),
CONSTRAINT fk_hat_hero FOREIGN KEY (hero_id) REFERENCES superhero (id)
DROP TABLE "fee_updates";
DROP TABLE "temp1";
DROP TABLE "superhero_updates";
DROP VIEW "sh_names";
ALTER TABLE "attribute_1"
RENAME TO "attribute";
ALTER TABLE "gender_1"
RENAME TO "gender";
ALTER TABLE "publisher_1"
RENAME TO "publisher";
ALTER TABLE "race_1"
RENAME TO "race";
ALTER TABLE "superentity_1"
RENAME TO "superentity";
ALTER TABLE "superpower_1"
RENAME TO "superpower";
CREATE VIEW "entity_names" AS
SELECT "id", "known_as"
FROM "superentity";
SELECT "id","superhero_name","full_name" FROM "superhero"
WHERE
"superhero_name" IS NULL
OR "superhero_name" = ''
OR "superhero_name" = '-'
OR "full_name" IS NULL
OR "full_name" = ''
OR "full_name" = '-';
SELECT COUNT(*) AS "NULL, '', '-'"
FROM "superhero"
WHERE
"superhero_name" IS NULL
OR "superhero_name" = ''
OR "superhero_name" = '-'
OR "full_name" IS NULL
OR "full_name" = ''
OR "full_name" = '-';
sqlite3 superagency.db <<EOF
.output /workspaces/119766791/cs50_sql/project/superagency/actual/backup.sql
.dump
.exit
EOF
WITH RECURSIVE all_ids AS (
SELECT MIN("id") AS id
FROM "superentity"
UNION ALL
SELECT id + 1
FROM all_ids
WHERE id + 1 <= (SELECT MAX("id") FROM "superentity")
)
SELECT id AS "missing_ids"
FROM all_ids
WHERE id NOT IN (SELECT "id" FROM "superentity");
SELECT * FROM "attribute";
SELECT * FROM "gender";
SELECT * FROM "publisher";
SELECT * FROM "race";
SELECT * FROM "superpower";
SELECT * FROM "entity_attribute";
SELECT * FROM "entity_power";
SELECT * FROM "superentity";
SELECT * FROM "team";
SELECT * FROM "client";
SELECT * FROM "entity_team";
SELECT * FROM "order";
SELECT * FROM "entity_order";
.dump "attribute"
.dump "gender"
.dump "publisher"
.dump "race"
.dump "superpower"
.dump "entity_attribute"
.dump "entity_power"
.dump "superentity"
SELECT "id","known_as", "full_name" FROM "superentity";
.import --csv --skip 1 entity_team.csv entity_team
SELECT
s."id",
s."known_as",
t."name"
FROM
"superentity" s
JOIN
"entity_team" et
ON et."entity_id" = s."id"
JOIN
"team" t
ON t."id" = et."team_id";
-- Assign unique id values based on the rowid
UPDATE "team"
SET "id" = rowid;
/* Create a new table with the same schema as other table (but empty).
1=0 is always false, so the SELECT statement will return no rows.
As a result, the new table (temp_team) will be created with the same schema, but it will be empty.*/
CREATE TABLE IF NOT EXISTS "superentity_temp" AS
SELECT * FROM "superentity" WHERE 1=0;
-- Insert but fully reject failed inserts
INSERT OR IGNORE INTO team ("name")
SELECT columns FROM "entity_temp_team";
-- Compare against multiple columns.
SELECT *
FROM "entity_team_temp" et
WHERE NOT EXISTS ( --or WHERE EXISTS
SELECT 1
FROM "superentity" se
WHERE (se."known_as" = et."entity_name" OR se."full_name" = et."entity_name")
);
--
BEGIN TRANSACTION;
-- To decide if keep (commit) or undo (rollback) the impact of a SQL code execution
-- Step 1: Start a transaction
BEGIN TRANSACTION;
-- Step 2: Run one or more query/ies (e.g., update the hourly_price)
UPDATE superentities SET hourly_price = hourly_price * 1.1 WHERE id = 1;
-- Step 3: Check the impact by reading the output log or/and executing other testing queries, e.g:
SELECT * FROM superentities WHERE id = 1;
-- Step 4:
ROLLBACK; -- to undo changes
COMMIT; -- to aply changes.
CREATE VIEW team_match_temp AS
SELECT
et.*,
CASE
WHEN EXISTS (
SELECT 1
FROM "superentity" se
WHERE se."known_as" = et."entity_name" OR se."full_name" = et."entity_name"
) THEN 1
ELSE 0
END AS "match"
FROM "entity_team_temp" et;
SELECT "entity_name", "team_name"
FROM "team_match_temp"
WHERE
"match" = 0
/*AND "team_name" IN (
'Avengers',
'Guardians of the Galaxy',
'Inhumans',
...
'team_name_n",
)*/
;
SELECT "entity_name", "team_name"
FROM "team_match_temp"
WHERE
"entity_name" IN(
SELECT "known_as"
FROM "superentity"
WHERE "id" > 756
)
;
SELECT
MAX("morality_rating") AS "max_morality_rating",
MIN("morality_rating") AS "min_morality_rating",
MAX("fee"),
MIN("fee")
FROM "superentity";
SELECT *
FROM "entity_team_temp" et
WHERE EXISTS (
SELECT 1
FROM "superentity" se
WHERE (se."known_as" = et."entity_name" OR se."full_name" = et."entity_name")
);
-- Insert missing characters into the "superentity" table starting from ID 757
INSERT INTO "superentity" ("id", "known_as", "full_name", "gender_id", "race_id", "publisher_id", "morality_rating", "fee")
VALUES
(757, "Captain Boomerang", "Digger Harkness", 1, 24, 4, 3, 600),
(758, "Puck", "Eugene Judd", 1, 3, 13, 7, 800),
(759, "Nocturne", "Talia Josephine Wagner", 2, 42, 13, 8, 750),
(760, "Misty Knight", "Mercedes Kelly Knight", 2, 24, 13, 8, 725),
(761, "Colleen Wing", "Colleen Wing", 2, 24, 13, 8, 725),
(762, "Cosmic Boy", "Rokk Krinn", 1, 2, 4, 9, 850),
(763, "Kitty Pryde", "Katherine Pryde", 2, 42, 13, 9, 850),
(764, "Night Thrasher", "Dwayne Taylor", 1, 24, 13, 7, 750),
(765, "Abigail Brand", "Abigail Brand", 2, 12, 13, 8, 700),
(766, "Manifold", "Eden Fesi", 1, 42, 13, 7, 750),
(767, "Steve Rogers", "Steven Grant Rogers", 1, 24, 13, 10, 1000),
(768, "Katana", "Tatsu Yamashiro", 2, 24, 4, 8, 750),
(769, "Baron Zemo", "Helmut Zemo", 1, 24, 13, 3, 850);
INSERT INTO "superentity" ("id", "known_as", "full_name", "gender_id", "race_id", "publisher_id", "morality_rating", "fee")
VALUES
(770, "Sunfire", "Shiro Yoshida", 1, 24, 13, 7, 750),
(771, "Fantomex", "Charlie Cluster-7", 1, 24, 13, 6, 800),
(772, "Rachel Summers", "Rachel Anne Summers", 2, 42, 13, 9, 850),
(773, "Marrow", "Sarah", 2, 42, 13, 6, 750),
(774, "Stepford Cuckoos", "Phoebe, Celeste, Esme, Sophie, Irma", 2, 42, 13, 7, 900),
(775, "Pixie", "Megan Gwynn", 2, 42, 13, 7, 700),
(776, "Magik", "Illyana Nikolievna Rasputina", 2, 42, 13, 9, 850),
(777, "Omega Sentinel", "Karima Shapandar", 2, 24, 13, 8, 800),
(778, "Avalanche", "Dominikos Petrakis", 1, 42, 13, 5, 700),
(779, "Destiny", "Irene Adler", 2, 42, 13, 6, 750);
BEGIN TRANSACTION;
INSERT INTO "team" ("name")
SELECT "team_name"
FROM "team_match_temp"
/* WHERE "team_name" NOT IN (
SELECT "team_name"
FROM "team_match_temp"
WHERE "match" = 0
GROUP BY "team_name"
)*/
GROUP BY "team_name";
SELECT "team_name" FROM (
SELECT st."id", st."known_as", st."full_name", ett."team_name"
FROM "superentity_temp" st
JOIN "entity_team_temp" ett ON st."known_as" = ett."entity_name"
)
GROUP BY "team_name";
SELECT
st."id" AS "entity_id",
st."known_as",
st."full_name",
ett."team_name",
tpt."publisher_name",
p."id" AS "publisher_id"
FROM "superentity_temp" st
JOIN "entity_team_temp" ett ON st."known_as" = ett."entity_name"
JOIN "team_publisher_temp" tpt ON ett."team_name" = tpt."team_name"
JOIN "publisher" p ON tpt."publisher_name" = p."name";
▼▼▼▼
CREATE VIEW "superentity_team_temp" AS
SELECT
s."id" AS "entity_id",
s."known_as",
s."full_name",
-- ett."entity_name",
ett."team_name",
t."id" AS "team_id"
FROM "superentity" s
JOIN "entity_team_temp" ett ON s."known_as" = ett."entity_name"
JOIN "team" t ON ett."team_name" = t."name"
;
SELECT "entity_id", "team_id"
FROM "superentity_team_temp";
.import --csv --skip 1 superentity_team.csv entity_team
SELECT * FROM "superentity_team"
WHERE "known_as" LIKE 'Spider%';
DELETE FROM "team"
WHERE
"entity_id" = 889
AND "team_id" = 3;
DELETE FROM "team"
WHERE
"entity_id" = 644
AND "team_id" = 39;
-- Delete with 2 or more columns as conditional
-- Using "IN"
DELETE FROM "team"
WHERE
("entity_id", "team_id") IN (
(889, 3),
(644, 39)
);
-- Using "OR"
DELETE FROM "team"
WHERE
("entity_id" = 889 AND "team_id" = 3)
OR ("entity_id" = 644 AND "team_id" = 39);
▲▲▲▲
SELECT
st."id" AS "entity_id",
--st."known_as",
--st."full_name",
--ett."team_name",
--tpt."publisher_name",
p."id" AS "publisher_id"
FROM "superentity_temp" st
JOIN "entity_team_temp" ett ON st."known_as" = ett."entity_name"
JOIN "team_publisher_temp" tpt ON ett."team_name" = tpt."team_name"
JOIN "publisher" p ON tpt."publisher_name" = p."name";
SELECT "id", "publisher_id"
FROM "superentity_temp";
-- update specific column based on values on other column
UPDATE "superentity_temp"
SET "publisher_id" = CASE "id"
WHEN 757 THEN 13
WHEN 758 THEN 13
WHEN 759 THEN 13
WHEN 760 THEN 13
WHEN 760 THEN 13
WHEN 761 THEN 13
WHEN 762 THEN 13
WHEN 763 THEN 13
WHEN 763 THEN 13
WHEN 764 THEN 13
WHEN 765 THEN 13
WHEN 766 THEN 13
WHEN 767 THEN 13
WHEN 768 THEN 13
WHEN 769 THEN 13
WHEN 770 THEN 13
WHEN 771 THEN 13
WHEN 772 THEN 13
WHEN 773 THEN 13
WHEN 774 THEN 13
WHEN 775 THEN 13
WHEN 776 THEN 13
WHEN 777 THEN 13
WHEN 778 THEN 13
WHEN 779 THEN 13
WHEN 780 THEN 13
WHEN 781 THEN 13
WHEN 782 THEN 13
WHEN 783 THEN 13
WHEN 784 THEN 13
WHEN 785 THEN 13
WHEN 786 THEN 13
WHEN 786 THEN 13
WHEN 787 THEN 4
WHEN 788 THEN 13
WHEN 789 THEN 13
WHEN 790 THEN 13
WHEN 791 THEN 13
WHEN 792 THEN 13
WHEN 793 THEN 13
WHEN 794 THEN 13
WHEN 795 THEN 13
WHEN 796 THEN 4
WHEN 797 THEN 4
WHEN 798 THEN 4
WHEN 799 THEN 4
WHEN 800 THEN 4
WHEN 801 THEN 4
WHEN 802 THEN 4
WHEN 803 THEN 3
WHEN 804 THEN 3
WHEN 805 THEN 3
WHEN 806 THEN 3
WHEN 807 THEN 3
WHEN 808 THEN 13
WHEN 808 THEN 13
WHEN 809 THEN 13
WHEN 810 THEN 13
WHEN 811 THEN 13
WHEN 812 THEN 13
WHEN 813 THEN 13
WHEN 814 THEN 13
WHEN 815 THEN 13
WHEN 816 THEN 13
WHEN 817 THEN 13
WHEN 818 THEN 13
WHEN 819 THEN 13
WHEN 820 THEN 13
WHEN 821 THEN 13
WHEN 822 THEN 15
WHEN 823 THEN 15
WHEN 824 THEN 15
WHEN 825 THEN 4
WHEN 826 THEN 10
WHEN 827 THEN 10
WHEN 828 THEN 10
WHEN 829 THEN 13
WHEN 830 THEN 13
WHEN 831 THEN 13
WHEN 832 THEN 13
WHEN 833 THEN 13
WHEN 834 THEN 13
WHEN 835 THEN 13
WHEN 835 THEN 13
WHEN 836 THEN 13
WHEN 837 THEN 13
WHEN 838 THEN 13
WHEN 839 THEN 13
WHEN 840 THEN 13
WHEN 841 THEN 13
WHEN 842 THEN 13
WHEN 843 THEN 10
WHEN 843 THEN 10
WHEN 844 THEN 10
WHEN 844 THEN 10
WHEN 845 THEN 10
WHEN 846 THEN 10
WHEN 847 THEN 10
WHEN 848 THEN 13
WHEN 849 THEN 10
WHEN 849 THEN 10
WHEN 850 THEN 10
WHEN 850 THEN 10
WHEN 851 THEN 10
WHEN 852 THEN 10
WHEN 853 THEN 10
WHEN 854 THEN 10
WHEN 855 THEN 10
WHEN 856 THEN 10
WHEN 857 THEN 10
WHEN 858 THEN 10
WHEN 864 THEN 4
WHEN 865 THEN 4
WHEN 866 THEN 13
WHEN 867 THEN 13
WHEN 868 THEN 13
WHEN 869 THEN 13
WHEN 870 THEN 13
WHEN 871 THEN 26
WHEN 872 THEN 26
WHEN 873 THEN 26
WHEN 874 THEN 26
WHEN 875 THEN 26
WHEN 876 THEN 24
WHEN 877 THEN 24
WHEN 878 THEN 24
WHEN 879 THEN 24
WHEN 880 THEN 24
WHEN 881 THEN 13
WHEN 882 THEN 13
WHEN 883 THEN 13
WHEN 884 THEN 13
WHEN 885 THEN 13
WHEN 886 THEN 13
WHEN 887 THEN 13
WHEN 888 THEN 13
WHEN 889 THEN 13
WHEN 889 THEN 13
WHEN 890 THEN 13
WHEN 891 THEN 13
WHEN 892 THEN 13
WHEN 893 THEN 13
WHEN 894 THEN 13
WHEN 895 THEN 13
WHEN 896 THEN 13
WHEN 897 THEN 13
WHEN 897 THEN 13
END;
SELECT
st."id",
st."known_as",
st."full_name",
st."publisher_id",
p."name" AS "publisher_name"
FROM "superentity_temp" st
JOIN "publisher" p ON st."publisher_id" = p."id";
.mode csv
.headers on
SELECT * FROM "attribute";
SELECT
s."id",
s."known_as",
s."full_name",
p."name" AS "publisher_name"
FROM "superentity" s
JOIN "publisher" p
ON s."publisher_id" = p."id"
WHERE s."id" > 756;
SELECT "id", "known_as", "full_name"
FROM "superentity"
WHERE
"known_as" LIKE 'Iron man'
--OR "full_name" LIKE ''
;
644,Spider-Man
73,Batman
667,Superman
342,Hulk
356,"Iron Man"
SELECT
s."id" AS "entity_id",
s."known_as",
a."id" AS "attribute_id",
a."name" AS "attribute_name",
ea."attribute_value"
FROM "superentity" s
JOIN "entity_attribute" ea
ON s."id" = ea."entity_id"
JOIN "attribute" a
ON ea."attribute_id" = a."id"
WHERE s."known_as" IN (
"Spider-Man",
"Batman",
"Superman",
"Hulk",
"Iron Man",
"Thor",
"Joker",
"Magneto",
"Thanos"
)
ORDER BY s."id" ASC;
SELECT "id", "known_as"
FROM "superentity"
WHERE "id" > 756;
.import --csv --skip 1 ent_att.csv "entity_attribute"
CREATE TABLE IF NOT EXISTS "entity_attribute_1" (
"entity_id" INTEGER,
"attribute_id" INTEGER,
"attribute_value" INTEGER NOT NULL,
PRIMARY KEY ("entity_id", "attribute_id"),
CONSTRAINT fk_eat_at1 FOREIGN KEY (attribute_id) REFERENCES "attribute" (id),
CONSTRAINT fk_eat_ent1 FOREIGN KEY ("entity_id") REFERENCES "superentity" (id)
);
INSERT INTO "entity_attribute_1"
SELECT * FROM "entity_attribute";
DROP TABLE "entity_attribute";
CREATE TABLE IF NOT EXISTS "entity_attribute" (
"entity_id" INTEGER,
"attribute_id" INTEGER,
"attribute_value" INTEGER NOT NULL,
PRIMARY KEY ("entity_id", "attribute_id"),
CONSTRAINT fk_eat_at FOREIGN KEY (attribute_id) REFERENCES "attribute" (id),
CONSTRAINT fk_eat_ent FOREIGN KEY ("entity_id") REFERENCES "superentity" (id)
);
INSERT INTO "entity_attribute"
SELECT * FROM "entity_attribute_1";
.mode csv
.headers on
SELECT * FROM "superpower";
CREATE TABLE IF NOT EXISTS "entity_attribute" (
"entity_id" INTEGER,
"attribute_id" INTEGER,
"attribute_value" INTEGER NOT NULL,
PRIMARY KEY ("entity_id", "attribute_id"),
CONSTRAINT fk_eat_at FOREIGN KEY (attribute_id) REFERENCES "attribute" (id),
CONSTRAINT fk_eat_ent FOREIGN KEY ("entity_id") REFERENCES "superentity" (id)
);
SELECT
e."id",
e."known_as",
COUNT(ep."entity_id") AS "number_of_powers"
FROM "superentity" e
JOIN "entity_power" ep
ON ep."entity_id" = e."id"
GROUP BY ep."entity_id"
LIMIT 50;
SELECT MAX("number_of_powers"), MIN("number_of_powers")
FROM (
SELECT
e."id",
e."known_as",
COUNT(ep."entity_id") AS "number_of_powers"
FROM "superentity" e
JOIN "entity_power" ep
ON ep."entity_id" = e."id"
GROUP BY ep."entity_id"
);
SELECT "known_as", "full_name", COUNT(*)
FROM "superentity"
GROUP BY "known_as", "full_name"
HAVING COUNT(*) > 1;
SELECT *
FROM "superentity"
WHERE ("known_as", "full_name") IN (
SELECT "known_as", "full_name"
FROM "superentity"
GROUP BY "known_as", "full_name"
HAVING COUNT(*) > 1
)
ORDER BY "known_as";
SELECT *
FROM "superentity"