SQL2 Flashcards
What will this produce
SELECT *
FROM tutorial.us_housing_units
WHERE month_name > ‘January’
Months Feb - December
What will this produce?
SELECT *
FROM tutorial.us_housing_units
WHERE month_name > ‘J’
All months with names longer then j including January
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 adds the west and south housing as south_plus_west
Select year, month, west, south, west+south AS south_plus_west
FROM tutorial.us_housing_units
This only adds across rows, you need an aggregate function across multiple rows
select the average number of homes for south and west combined for each month and year. Order by month and year
SELECT year, month, avg(west+south) average_sw_housing
FROM tutorial.us_housing_units
GROUP BY 1,2
ORDER BY 1,2
Write a query that calculates the sum of all four regions in a separate column.
SELECT year, month, (west+south+northeast+midwest) as total_units
FROM tutorial.us_housing_units
get the south and west averages per ROW
SELECT year, month, west, south, (west + south)/2 AS south_west_avg
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, 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+northeast+midwest))*100 as west_percent,
(south/(west+south+northeast+midwest))*100 as south_percent,
(northeast/(west+south+northeast+midwest))*100 as northeast_percent,
(midwest/(west+south+northeast+midwest))*100 as midwest_percent
FROM tutorial.us_housing_units
WHERE year >=2000
ORDER BY 1,2
SQL
LIKE allows you to match similar values, instead of exact values.
allows you to match similar values, instead of exact values.
IN
allows you to specify a list of values you’d like to include.
Between
allows you to select only rows within a certain range.
IS NULL
allows you to select rows that contain no data in a given column.
AND
allows you to select only rows that satisfy two conditions.
OR
allows you to select rows that satisfy either of two conditions.
NOT
allows you to select rows that do not match a certain condition.
Select Rows where the “group” starts with Snoop
will this return a group that starts with snoop?
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE “group” LIKE ‘Snoop%’
Note: “group” appears in quotations above because GROUP is actually the name of a function in SQL. The double quotes (as opposed to single: ‘) are a way of indicating that you are referring to the column name “group”, not the SQL function. In general, putting double quotes around a word or phrase will indicate that you are referring to that column name.