-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday4.sqbpro
81 lines (67 loc) · 4.64 KB
/
day4.sqbpro
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
<?xml version="1.0" encoding="UTF-8"?><sqlb_project><db path="/Users/maximilianolopezsalgado/data_projects/sql-playground/databases/day3.db" readonly="0" foreign_keys="1" case_sensitive_like="0" temp_store="0" wal_autocheckpoint="1000" synchronous="2"/><attached/><window><main_tabs open="structure browser pragmas query" current="3"/></window><tab_structure><column_width id="0" width="300"/><column_width id="1" width="0"/><column_width id="2" width="100"/><column_width id="3" width="1398"/><column_width id="4" width="0"/><expanded_item id="0" parent="1"/><expanded_item id="1" parent="1"/><expanded_item id="2" parent="1"/><expanded_item id="3" parent="1"/></tab_structure><tab_browse><current_table name="4,5:mainBoxes"/><default_encoding codec=""/><browse_table_settings><table schema="main" name="Boxes" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk="_rowid_"><sort/><column_widths><column index="1" value="59"/><column index="2" value="79"/><column index="3" value="51"/><column index="4" value="96"/></column_widths><filter_values/><conditional_formats/><row_id_formats/><display_formats/><hidden_columns/><plot_y_axes/><global_filter/></table><table schema="main" name="Departments" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk="_rowid_"><sort/><column_widths><column index="1" value="31"/><column index="2" value="160"/><column index="3" value="71"/></column_widths><filter_values/><conditional_formats/><row_id_formats/><display_formats/><hidden_columns/><plot_y_axes/><global_filter/></table><table schema="main" name="Warehouses" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk="_rowid_"><sort/><column_widths><column index="1" value="25"/><column index="2" value="126"/><column index="3" value="74"/></column_widths><filter_values/><conditional_formats/><row_id_formats/><display_formats/><hidden_columns/><plot_y_axes/><global_filter/></table></browse_table_settings></tab_browse><tab_sql><sql name="SQL 1">CREATE TABLE IF NOT EXISTS Movies (
Code INTEGER,
Title VARCHAR(255) NOT NULL,
Rating VARCHAR(255),
PRIMARY KEY (Code)
);
CREATE TABLE IF NOT EXISTS MovieTheaters (
Code INTEGER,
Name VARCHAR(255) NOT NULL,
Movie INTEGER,
PRIMARY KEY (Code)
);
INSERT INTO Movies(Code,Title,Rating) VALUES(1,'Citizen Kane','PG');
INSERT INTO Movies(Code,Title,Rating) VALUES(2,'Singin'' in the Rain','G');
INSERT INTO Movies(Code,Title,Rating) VALUES(3,'The Wizard of Oz','G');
INSERT INTO Movies(Code,Title,Rating) VALUES(4,'The Quiet Man',NULL);
INSERT INTO Movies(Code,Title,Rating) VALUES(5,'North by Northwest',NULL);
INSERT INTO Movies(Code,Title,Rating) VALUES(6,'The Last Tango in Paris','NC-17');
INSERT INTO Movies(Code,Title,Rating) VALUES(7,'Some Like it Hot','PG-13');
INSERT INTO Movies(Code,Title,Rating) VALUES(8,'A Night at the Opera',NULL);
INSERT INTO MovieTheaters(Code,Name,Movie) VALUES(1,'Odeon',5);
INSERT INTO MovieTheaters(Code,Name,Movie) VALUES(2,'Imperial',1);
INSERT INTO MovieTheaters(Code,Name,Movie) VALUES(3,'Majestic',NULL);
INSERT INTO MovieTheaters(Code,Name,Movie) VALUES(4,'Royale',6);
INSERT INTO MovieTheaters(Code,Name,Movie) VALUES(5,'Paraiso',3);
INSERT INTO MovieTheaters(Code,Name,Movie) VALUES(6,'Nickelodeon',NULL);
-- 4.1 Select the title of all movies.
SELECT Title
FROM Movies;
-- 4.2 Show all the distinct ratings in the database.
SELECT DISTINCT Rating
FROM Movies;
-- 4.3 Show all unrated movies.
SELECT *
FROM Movies
WHERE Rating IS NULL;
-- 4.4 Select all movie theaters that are not currently showing a movie.
SELECT *
FROM MovieTheaters
WHERE Movie IS NULL;
-- 4.5 Select all data from all movie theaters and, additionally, the data from the movie that is being shown in the theater (if one is being shown).
SELECT mt.*, m.Title AS MovieTitle, m.Rating
FROM MovieTheaters mt
LEFT JOIN Movies m ON mt.Movie = m.Code;
-- 4.6 Select all data from all movies and, if that movie is being shown in a theater, show the data from the theater.
SELECT m.*, mt.Name AS Theater_name, mt.Code AS Theater_code
FROM Movies m
LEFT JOIN MovieTheaters mt ON mt.Movie = m.Code;
-- 4.7 Show the titles of movies not currently being shown in any theaters.
SELECT m.Title
FROM Movies m
LEFT JOIN MovieTheaters mt ON mt.Movie = m.Code
WHERE NOT m.Title = mt.Movie;
-- 4.8 Add the unrated movie "One, Two, Three".
INSERT INTO Movies (Code,Title,Rating) VALUES(9,'One, Two, Three','NULL');
-- 4.9 Set the rating of all unrated movies to "G".
UPDATE Movies
SET Rating = 'G'
WHERE Rating IS NULL;
-- 4.10 Remove movie theaters projecting movies rated "NC-17".
DELETE FROM MovieTheaters
WHERE Movie IN (
SELECT Title
FROM Movies
WHERE Rating = 'NC-17'
);
</sql><current_tab id="0"/></tab_sql></sqlb_project>