-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqldc.sql
211 lines (154 loc) · 5.51 KB
/
sqldc.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
/* select title, release year from films where films aer french and spanish and released in the 90s */
SELECT title, release_year
FROM films
WHERE (release_year >= 1990 AND release_year < 2000)
AND (language = 'French' OR language = 'Spanish')
/* where gross > 2 mill*/
SELECT title, release_year
FROM films
WHERE (release_year >= 1990 AND release_year < 2000)
AND (language = 'French' OR language = 'Spanish')
AND (gross > 2000000)
/*BETWEEN is INCLUSIVE*/
/* Select title and release year of films that are spanish or french with budgets of more than 100 mill*/
SELECT title, release_year
FROM films
WHERE (release_year BETWEEN 1990 AND 2000)
AND (budget > 100000000) AND (language = 'Spanish' or language = 'French')
/* IN for specifying multiple values */
/* Get title and language of films that are in English, Spanish and French */
SELECT title, language
FROM films
WHERE language IN ('English', 'Spanish', 'French')
/* Select title and certification from films where rating is either NC-17 or R */
SELECT title, certification
FROM films
WHERE certification IN ('NC-17', 'R')
/* IS NULL, IS NOT NULL */
SELECT name
FROM people
WHERE deathdate IS NULL
/* % will match zero, one or many characters in text */
/* _ will match a single character */
/* NOT LIKE will match records taht doesn't match pattern specified */
/* names begin with B */
SELECT name
FROM people
WHERE name LIKE 'B%'
/* names have r as second letter */
SELECT name
FROM people
WHERE name LIKE '_r%'
/* names don't start with an A */
SELECT name
FROM people
WHERE name NOT LIKE 'A%'
/* AGGREGATE FUNCTIONS*/
/* AVG(x) gives avg of that column, x */
/* MAX is highest */
/* SUM gives sum of numeric values in column */
/*Get average duration of films*/
SELECT AVG(duration)
FROM films
/* Find avg gross of films that start with A */
SELECT AVG(gross)
FROM films
WHERE title LIKE 'A%'
/* Get title and net profit (ie gross - budget) for all films */
SELECT title,
gross - budget AS net_profit
FROM films
/* Get title and duration for all films, changing minutes to hours*/
SELECT title, (duration / 60.0) AS duration_hours
FROM films
/* aliasing */
SELECT ((COUNT (deathdate) * 100.0) / COUNT(*)) AS
percentage_dead
FROM people
/* Get the number of years between the newest film and oldest film. Alias as difference*/
SELECT (MAX(release_year) - MIN(release_year)) AS difference
FROM films
/* number of decades */
SELECT ((MAX(release_year) - MIN(release_year)) / 10) AS number_of_decades
FROM films
/* Sort names alphabetically */
SELECT name
FROM people
ORDER BY name ASC
/* Get title of films released in 2000 or 2012, in order they were released*/
SELECT title
FROM films
WHERE (release_year = 2000) or (release_year = 2012)
ORDER BY release_year
/* All details for films except those released in 2015, ordered by duration */
SELECT *
FROM films
WHERE NOT (release_year) = 2015
ORDER BY duration
/* Get title and gross earnings for movies which begin with letter M and sort alphabetically */
SELECT title, gross
FROM films
WHERE title LIKE 'M%'
ORDER BY title
/* Get imdb score and film id for every film sorted from highest to lowest */
SELECT imdb_score, film_id
FROM reviews
ORDER BY imdb_score DESC
/* sort by multiple will sort by first, then second, etc */
/* Get birth date and name of people in people table, order by birthdate and alphabetically by name*/
SELECT birthdate, name
FROM people
ORDER BY birthdate, people ASC
/* Group by */
/* Get the release year and count of films released in each year*/
SELECT release_year, COUNT(title)
FROM films
GROUP BY release_year
/* Get the imdb score and count of film reviews grouped by imdb score in the reviews table */
SELECT imdb_score, COUNT(num_votes)
FROM reviews
GROUP BY imdb_score
/* Get the language and total gross amount films in each language */
SELECT language, SUM(gross)
from films
GROUP BY language
/* Get the country and total budget spent making movies in each country */
SELECT country, SUM(budget)
from films
GROUP BY country
/* Get the release year, country, and highest budget spent making a film for reach year, each country
. Sort your results by the release year and country */
SELECT release_year, country, MAX(budget)
from films
GROUP BY release_year, country
ORDER BY release_year, country
/* Having */
/* Aggregate functions can't be used in WHERE clauses. Use Having */
/* How many different years were more than 200 movies released? */
SELECT release_year
FROM films
GROUP by release_year
HAVING COUNT(title) > 200;
/* Write a query that returns the average budget and avg gross earnings for films in each year
after 1990 if the avg budget is greater than 60 mill). Sort by gross earnings highest to lowest
*/
SELECT release_year, AVG(budget) avg_budget, AVG(gross) avg_gross
FROM films
GROUP BY release_year
HAVING (release_year > 1990) AND AVG(budget) > 60000000
ORDER BY AVG(gross) DESC
/* Select country, avg budget, average gross from films table
Group by country where count title > 10 order by country limit 5 */
-- select country, average budget, average gross
-- select country, average budget, average gross
SELECT country, AVG(budget) avg_budget, AVG(gross) avg_gross
-- from the films table
FROM films
-- group by country
GROUP BY country
-- where the country has a title count greater than 10
HAVING COUNT(title) > 10
-- order by country
ORDER BY country
-- limit to only show 5 results
LIMIT 5