-
Notifications
You must be signed in to change notification settings - Fork 1
/
basic_sql.sql
197 lines (162 loc) · 6.07 KB
/
basic_sql.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
-- SQL SELECT
-- Write a query to select all of the columns in the tutorial.us_housing_units table without using *.
SELECT year,
month,
month_name,
west,
midwest,
south,
northeast
FROM tutorial.us_housing_units;
-- Write a query to select all of the columns in tutorial.us_housing_units and
-- rename them so that their first letters are capitalized.
SELECT year AS "Year",
month AS "Month",
month_name AS "Month Name",
west AS "West",
midwest AS "Midwest",
south AS "South",
northeast AS "Northeast"
FROM tutorial.us_housing_units
-- SQL LIMIT
-- Write a query that uses the LIMIT command to restrict the result set to only 15 rows.
SELECT *
FROM tutorial.us_housing_units
LIMIT 15
-- SQL Comparison Operators
-- Did the West Region ever produce more than 50,000 housing units in one month?
SELECT *
FROM tutorial.us_housing_units
WHERE west > 50
-- Did the South Region ever produce 20,000 or fewer housing units in one month?
SELECT *
FROM tutorial.us_housing_units
WHERE south <= 20
-- Write a query that only shows rows for which the month name is February.
SELECT *
FROM tutorial.us_housing_units
WHERE month_name = 'February'
-- Write a query that only shows rows for which the month_name starts with the letter "N"
-- or an earlier letter in the alphabet.
SELECT *
FROM tutorial.us_housing_units
WHERE month_name < 'o'
-- Write a query that calculates the sum of all four regions in a separate column.
SELECT year,
month,
west,
south,
midwest,
northeast,
west + south + midwest + northeast AS usa_total
FROM tutorial.us_housing_units
-- Write a query that returns all rows for which more units were produced in the West region
-- than in the Midwest and Northeast combined.
SELECT year,
month,
west,
south,
midwest,
northeast
FROM tutorial.us_housing_units
WHERE west > (midwest + northeast)
-- Write a query that calculates the percentage of all houses completed in the United States represented by each region.
-- Only return results from the year 2000 and later.
SELECT year,
month,
west/(west + south + midwest + northeast)*100 AS west_pct,
south/(west + south + midwest + northeast)*100 AS south_pct,
midwest/(west + south + midwest + northeast)*100 AS midwest_pct,
northeast/(west + south + midwest + northeast)*100 AS northeast_pct
FROM tutorial.us_housing_units
WHERE year >= 2000
-- SQL LIKE
-- Write a query that returns all rows for which Ludacris was a member of the group.
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE "group" ilike '%ludacris%'
-- Write a query that returns all rows for which the first artist listed in the group
-- has a name that begins with "DJ".
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE "group" LIKE 'DJ%'
-- SQL IN
-- Write a query that shows all of the entries for Elvis and M.C. Hammer.
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE "group" IN ('M.C. Hammer', 'Hammer', 'Elvis Presley')
-- SQL BETWEEN
-- Write a query that shows all top 100 songs from January 1, 1985 through December 31, 1990.
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE year BETWEEN 1985 AND 1990
-- SQL IS NULL
-- Write a query that shows all of the rows for which song_name is null.
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE song_name IS NULL
-- SQL AND
-- Write a query that surfaces all rows for top-10 hits for which Ludacris is part of the Group.
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE year_rank <= 10
AND "group" ILIKE '%ludacris%'
-- Write a query that surfaces the top-ranked records in 1990, 2000, and 2010.
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE year_rank = 1
AND year IN (1990, 2000, 2010)
-- Write a query that lists all songs from the 1960s with "love" in the title
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE year BETWEEN 1960 AND 1969
AND song_name ilike '%love%'
-- SQL OR
-- Write a query that returns all rows for top-10 songs that featured either Katy Perry or Bon Jovi.
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE year_rank <= 10
AND ("group" ILIKE '%katy perry%' OR "group" ILIKE '%bon jovi%')
-- Write a query that returns all songs with titles that contain the word "California"
-- in either the 1970s or 1990s.
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE song_name LIKE '%California%'
AND (year BETWEEN 1970 AND 1979 OR year BETWEEN 1990 AND 1999)
-- Write a query that lists all top-100 recordings that feature Dr. Dre before 2001 or after 2009.
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE "group" ILIKE '%dr. dre%'
AND (year < 2001 OR year > 2009)
-- SQL NOT
-- Write a query that returns all rows for songs that were on the charts in 2013
-- and do not contain the letter "a".
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE song_name NOT ILIKE '%a%'
AND year = 2013
-- SQL ORDER BY
-- Write a query that returns all rows from 2012, ordered by song title from Z to A.
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE year = 2012
ORDER BY song_name DESC
-- Write a query that returns all rows from 2010 ordered by rank,
-- with artists ordered alphabetically for each song.
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE year = 2010
ORDER BY year_rank, artist
-- Write a query that shows all rows for which T-Pain was a group member,
-- ordered by rank on the charts, from lowest to highest rank (from 100 to 1).
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE "group" ILIKE '%t-pain%'
ORDER BY year_rank DESC
-- Write a query that returns songs that ranked between 10 and 20 (inclusive) in 1993, 2003, or 2013.
-- Order the results by year and rank, and leave a comment on each line of the WHERE clause.
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE year IN (2013, 2003, 1993) --Select the relevant years
AND year_rank BETWEEN 10 AND 20 --Limit the rank to 10-20
ORDER BY year, year_rank