-
Notifications
You must be signed in to change notification settings - Fork 0
/
Query-Answers sql bolt.sql
255 lines (184 loc) · 9.36 KB
/
Query-Answers sql bolt.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
-- Exercise1 - SELECT queries 101---------------------------------------------------------------------------
-- Find the title of each film
SELECT Title FROM Movies;
-- Find the director of each film
SELECT Director FROM Movies;
-- Find the title and director of each film
SELECT Title, Director FROM Movies;
-- Find the title and year of each film
SELECT Title, Director FROM Movies;
-- Find all the information about each film
SELECT * FROM Movies;
-- Exercise2 - Queries with constraints (Pt. 1)--------------------------------------------------------------
-- Find the movie with a row id of 6
SELECT * FROM Movies WHERE Id = 6;
-- Find the movies released in the years between 2000 and 2010
SELECT * FROM Movies WHERE Year BETWEEN 2000 AND 2010;
-- Find the movies not released in the years between 2000 and 2010
SELECT * FROM Movies WHERE Year NOT BETWEEN 2000 AND 2010;
-- Find the first 5 Pixar movies and their release year
SELECT * FROM Movies WHERE Id BETWEEN 1 AND 5;
-- Exercise3 - Queries with constraints (Pt. 2)-------------------------------------------------------------
-- Find all the Toy Story movies
SELECT * FROM Movies WHERE Title LIKE "%Toy Story%";
-- Find all the movies directed by John Lasseter
SELECT * FROM Movies WHERE Director = "John Lasseter";
-- Find all the movies (and director) not directed by John Lasseter
SELECT * FROM Movies WHERE Director != "John Lasseter";
-- Find all the WALL-* movies
SELECT * FROM Movies WHERE Title LIKE "%WALL%";
-- Exercise4 - Filtering and sorting Query results-------------------------------------------------------------
-- List all directors of Pixar movies (alphabetically), without duplicates
SELECT DISTINCT Director FROM Movies
ORDER BY Director;
-- List the last four Pixar movies released (ordered from most recent to least)
SELECT * FROM Movies
ORDER BY Year DESC LIMIT 4;
-- List the first five Pixar movies sorted alphabetically
SELECT * FROM Movies
ORDER BY Title ASC LIMIT 5;
-- List the next five Pixar movies sorted alphabetically
SELECT * FROM Movies
ORDER BY Title ASC
LIMIT 5
OFFSET 5;
-- Exercise5 - Review Simple SELECT Queries-----------------------------------------------------------------
-- List all the Canadian cities and their populations
SELECT *
FROM North_american_cities
WHERE Country LIKE "Canada";
-- Order all the cities in the United States by their latitude from north to south
SELECT *
FROM North_american_cities
WHERE Country = "United States"
ORDER BY Latitude DESC;
-- List all the cities west of Chicago, ordered from west to east
SELECT *
FROM North_american_cities
WHERE Longitude < -87.69
ORDER BY Longitude ASC;
-- List the two largest cities in Mexico (by population)
SELECT *
FROM North_american_cities
WHERE Country LIKE "Mexico"
ORDER BY Population DESC
LIMIT 2;
-- List the third and fourth largest cities (by population) in the United States and their population
SELECT *
FROM North_american_cities
WHERE Country LIKE "United States"
ORDER BY Population DESC
LIMIT 2
OFFSET 2;
-- Exercise6 - Multi-table queries with JOINs------------------------------------------------------------
-- Find the domestic and international sales for each movie
SELECT Title, International_sales, Domestic_sales
FROM Movies JOIN Boxoffice
ON Id=Movie_id;
-- Show the sales numbers for each movie that did better internationally rather than domestically
SELECT Title, International_sales, Domestic_sales
FROM Movies JOIN Boxoffice ON Id=Movie_id
WHERE International_sales > Domestic_sales;
-- List all the movies by their ratings in descending order
SELECT Title, Rating FROM Movies JOIN Boxoffice
ON Id=Movie_id ORDER BY Rating DESC;
-- Exercise7 - OUTER JOIN-------------------------------------------------------------------------------------
-- Find the list of all buildings that have employees
SELECT DISTINCT Building FROM Employees
LEFT JOIN Buildings ON Building=Building_name
WHERE Years_employe;
-- Find the list of all buildings and their capacity
SELECT * FROM Buildings;
-- List all buildings and the distinct employee roles in each building (including empty buildings)
SELECT DISTINCT Building_name, Role FROM Buildings
LEFT JOIN employees ON building_name = building;
-- Exercise8 - A short note on NULLs---------------------------------------------------------------------------
-- Find the name and role of all employees who have not been assigned to a building
SELECT * FROM Employees
LEFT JOIN Buildings
ON Building_name = Building
WHERE Building IS NULL;
-- Find the names of the buildings that hold no employees
SELECT * FROM Buildings
LEFT JOIN Employees
ON Building_name = Building
WHERE Building IS NULL;
-- Exercise9 - Queries with expressions----------------------------------------------------------------------
-- List all movies and their combined sales in millions of dollars
SELECT Title, (Domestic_sales + International_sales)/1000000 AS Total_Sales_Millions
FROM Movies
LEFT JOIN Boxoffice ON Id=Movie_Id;
-- List all movies and their ratings in percent
SELECT Title, Rating*10 as Percent
FROM Movies
LEFT JOIN Boxoffice ON Id=Movie_Id;
-- List all movies that were released on even number years
SELECT Title, Year FROM Movies
LEFT JOIN Boxoffice ON Id=Movie_Id
WHERE Year % 2 = 0;
-- Exercise10 - Queries with aggregates (Pt. 1)------------------------------------------------------------------
-- Find the longest time that an employee has been at the studio
SELECT MAX(Years_employed) FROM Employees;
-- For each role, find the average number of years employed by employees in that role
SELECT Role, AVG(Years_Employed) FROM Employees
GROUP BY Role;
-- Find the total number of employee years worked in each building
SELECT Building, SUM(Years_Employed) FROM Employees
GROUP BY Building;
-- Exercise11 - Queries with aggregates (Pt. 2)------------------------------------------------------------------
-- Find the number of Artists in the studio (without a HAVING clause)
SELECT Role, COUNT(*) AS Number_of_Artists FROM Employees
WHERE Role = "Artist";
-- Find the number of Employees of each role in the studio
SELECT Role, COUNT(*) FROM Employees
GROUP BY Role;
-- Find the total number of years employed by all Engineers
SELECT Role, SUM(Years_Employed) FROM Employees
GROUP BY Role
HAVING Role = "Engineer";
-- Exercise12 - Order of execution of a Query--------------------------------------------------------------------
-- Find the number of movies each director has directed
SELECT *, COUNT(Title) FROM Movies
GROUP BY Director;
-- Find the total domestic and international sales that can be attributed to each director
SELECT Director, sum(Domestic_sales + International_Sales) as Total_Sales FROM Movies
LEFT JOIN Boxoffice ON Id = Movie_ID
GROUP BY Director;
-- Exercise13 - Inserting rows----------------------------------------------------------------------------------
-- Add the studio's new production, Toy Story 4 to the list of movies (you can use any director)
INSERT INTO Movies VALUES (4, "Toy Story 4", "John Lasseter", 2017, 123);
-- Toy Story 4 has been released to critical acclaim! It had a rating of 8.7, and made 340 million domestically and 270 million internationally. Add the record to the BoxOffice table.
INSERT INTO Boxoffice VALUES (4, 8.7, 340000000, 270000000);
-- Exercise14 - Updating rows----------------------------------------------------------------------------------
-- The director for A Bug's Life is incorrect, it was actually directed by John Lasseter
UPDATE Movies SET Director = "John Lasseter" WHERE Id = 2;
-- The year that Toy Story 2 was released is incorrect, it was actually released in 1999
UPDATE Movies SET Year = "1999" WHERE Id = 3;
-- Both the title and directory for Toy Story 8 is incorrect! The title should be "Toy Story 3" and it was directed by Lee Unkrich
UPDATE Movies SET Title = "Toy Story 3", Director = "Lee Unkrich"
WHERE Id = 11;
-- Exercise15 - Deleting rows-----------------------------------------------------------------------------------
-- This database is getting too big, lets remove all movies that were released before 2005.
DELETE FROM Movies WHERE Year < 2005;
-- Andrew Stanton has also left the studio, so please remove all movies directed by him.
DELETE FROM Movies WHERE Director = "Andrew Stanton";
-- Exercise16 - Creating Tables------------------------------------------------------------------------------
-- Create a new table named Database with the following columns:
-- 1. Name A string (text) describing the name of the database
-- 2. Version A number (floating point) of the latest version of this database
-- 3. Download_count An integer count of the number of times this database was downloaded
-- This table has no constraints.
CREATE TABLE `Database` (
Name TEXT,
Version FLOAT,
Download_Count INTEGER);
-- Exercise17 - Altering Tables--------------------------------------------------------------------------------
-- Add a column named Aspect_ratio with a FLOAT data type to store the aspect-ratio each movie was released in.
ALTER TABLE Movies ADD COLUMN Aspect_ratio FLOAT DEFAULT 3;
-- Add another column named Language with a TEXT data type to store the language that the movie was released in. Ensure that the default for this language is English.
ALTER TABLE Movies ADD COLUMN Language TEXT DEFAULT "English";
-- Exercise18 - Dropping Tables---------------------------------------------------------------------------------
-- removing the Movies table
DROP TABLE Movies;
-- And drop the BoxOffice table as well
DROP TABLE BoxOffice;