SQL Intermediate Flashcards
(66 cards)
COUNT
Returns the count of only non-null rows
Write a query to count the number of non-null rows in the low column.

SELECT COUNT(low) AS low FROM tutorial.aapl_historical_stock_price
Write a query that determines counts of every single column.

SELECT COUNT(year) AS year,
COUNT(month) AS month,
COUNT(open) AS open,
COUNT(high) AS high,
COUNT(low) AS low,
COUNT(close) AS close,
COUNT(volume) AS volume
FROM tutorial.aapl_historical_stock_price
Write a query to calculate the average opening price

SELECT (SUM(open)/COUNT(open)) as avg_open
FROM tutorial.aapl_historical_stock_price
MIN and MAX
Depending on the column type, MIN will return the lowest number, earliest date, or non-numerical value as close alphabetically to “A” as possible. As you might suspect, MAX does the opposite—it returns the highest number, the latest date, or the non-numerical value closest alphabetically to “Z.”
select the minimum and maximum volumes of stock

SELECT MIN(volume) AS min_volume, MAX(volume) AS max_volume FROM tutorial.aapl_historical_stock_price
What was Apple’s lowest stock price (at the time of this data collection)?

SELECT MIN(low) FROM tutorial.aapl\_historical\_stock\_price
What was the highest single-day increase in Apple’s share value?

SELECT MAX(close-open) FROM tutorial.aapl\_historical\_stock\_price
AVG
calculates the average of a selected group of values. It’s very useful, but has some limitations. First, it can only be used on numerical columns. Second, it ignores nulls completely.
SELECT AVG(high) FROM tutorial.aapl_historical_stock_price WHERE high IS NOT NULL
VS
SELECT AVG(high) FROM tutorial.aapl_historical_stock_price
No difference as AVG ignores nulls
Write a query that calculates the average daily trade volume for Apple stock.

SELECT AVG(volume) FROM tutorial.aapl\_historical\_stock\_price
GROUP BY
GROUP BY allows you to separate data into groups, which can be aggregated independently of one another.
Calculate the total number of shares traded each month for each year. Order your results chronologically.

SELECT year, month, SUM(volume) as total_num_shares
FROM tutorial.aapl_historical_stock_price
GROUP BY 1, 2
ORDER BY 1, 2
Using GROUP BY with ORDER BY
The order of column names in your GROUP BY clause doesn’t matter—the results will be the same regardless. If you want to control how the aggregations are grouped together, use ORDER BY.
SELECT year, month, COUNT(*) AS count FROM tutorial.aapl_historical_stock_price GROUP BY year, month ORDER BY month, year
VS
SELECT year, month, COUNT(*) AS count FROM tutorial.aapl_historical_stock_price GROUP BY year, month ORDER BY year, month

2000 1 20
2001 1 21
2002 1 21
2003 1 21
2004 1 20
vs
2000 1 20
2000 2 20
2000 3 23
2000 4 19
Write a query to calculate the average daily price change in Apple stock, grouped by year.

SELECT year,
AVG(close-open) AS avg_change
FROM tutorial.aapl_historical_stock_price
GROUP BY 1
ORDER BY 1
Write a query that calculates the lowest and highest prices that Apple stock achieved each month.
SELECT year, month,
MAX(high) AS highest,
MIN(low) AS lowest
FROM tutorial.aapl_historical_stock_price
GROUP BY 1, 2
ORDER BY 1, 2
find every month during which AAPL stock where the max was over $400/share.

SELECT year, month, MAX(high) AS month_high FROM tutorial.aapl_historical_stock_price GROUP BY year, month HAVING MAX(high) > 400 ORDER BY year, month
ORDER of SQL operations
SELECT
FROM
WHERE
GROUP BY
HAVING
ORDER BY
CASE
The CASE statement is SQL’s way of handling if/then logic. The CASE statement is followed by at least one pair of WHEN and THEN statements—SQL’s equivalent of IF/THEN in Excel. Because of this pairing, you might be tempted to call this SQL CASE WHEN, but CASE is the accepted term.
Every CASE statement must end with the END statement. The ELSE statement is optional, and provides a way to capture values not specified in the WHEN/THEN statements.
write a query that adds a new column called is_a_senior that returns yes if the player is a senior and no if not

SELECT player_name, year,
CASE
WHEN year = ‘SR’ THEN ‘yes’
ELSE
‘no’
END AS is_a_senior
FROM benn.college_football_players
Write a query that includes a column that is flagged “yes” when a player is from California, and sort the results with those players first.

SELECT player_name,
CASE
WHEN state = ‘CA’ THEN ‘yes’
ELSE ‘no’
END AS from_ca
FROM benn.college_football_players
ORDER BY 2 DESC
sort players into three weight classes:
over 250
201 - 250
176 - 200
175 and under

SELECT player_name, weight,
CASE WHEN weight > 250 THEN ‘over 250’
WHEN weight > 200 AND weight <= 250 THEN ‘201-250’
WHEN weight > 175 AND weight <= 200 THEN ‘176-200’
ELSE ‘175 or under’ END AS weight_group
FROM benn.college_football_players
Write a query that includes players’ names and a column that classifies them into four categories based on height. Keep in mind that the answer we provide is only one of many possible answers, since you could divide players’ heights in many ways.

SELECT player_name, height,
CASE
WHEN height > 74 THEN ‘over 74’
WHEN height > 72 AND height <= 74 THEN ‘73-74’
WHEN height > 70 AND height <= 72 THEN ‘71-72’
ELSE ‘under 70’
END AS height_group
FROM benn.college_football_players


















