Intermediate SQL Flashcards
What are the aggregation functions used in SQL?
COUNT counts how many rows are in a particular column.
SUM adds together all the values in a particular column.
MIN and MAX return the lowest and highest values in a particular column, respectively.
AVG calculates the average of a group of selected values.
What does COUNT command do?
COUNT is a SQL aggregate function for counting the number of rows in a particular column.
Typing COUNT(1) has the same effect as COUNT(*). Which one you use is a matter of personal preference. True/False?
True
What does below code do?
SELECT COUNT(high)
FROM tutorial.aapl_historical_stock_price
The code will provide a count of all rows in which the high column is NOT NULL.
What does below code do?
SELECT COUNT(date) AS count_of_date
FROM tutorial.aapl_historical_stock_price
The column header for count(date) is changed from “count” to “count_of_date”.
Does COUNT command, count null values?
No
What does SUM do?
SUM is a SQL aggregate function. that totals the values in a given column.
An important thing to remember: aggregators only aggregate vertically. If you want to perform a calculation across rows, you would do this with simple arithmetic. True/False?
True
How does SUM treat nulls?
SUM treats nulls as 0.
What do MIN / MAX commands do?
MIN and MAX are SQL aggregation functions that return the lowest and highest values in a particular column.
MIN/MAX can be used on non-numerical columns. True/False, if True, how do they do it?
True.
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.”
What does AVG command do?
AVG is a SQL aggregate function that calculates the average of a selected group of values.
What does GROUP BY do?
GROUP BY allows you to separate data into groups, which can be aggregated independently of one another.
When is it recommended to use numbers instead of column names for group by?
It’s generally recommended to do this only when you’re grouping many columns, or if something else is causing the text in the GROUP BY clause to be excessively long
We use HAVING clause with aggregation, because WHERE doesn’t allow you to filter on aggregate columns. True/False?
True
What is SQL’s way of handling if/then logic?
CASE
The CASE statement is followed by at least one pair of WHEN and THEN statements. True/False?
True
How do we end a CASE statement?
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. True/False?
True
What does below code do?
SELECT CASE WHEN year = ‘FR’ THEN ‘FR’
WHEN year = ‘SO’ THEN ‘SO’
WHEN year = ‘JR’ THEN ‘JR’
WHEN year = ‘SR’ THEN ‘SR’
ELSE ‘No Year Data’ END AS year_group,
COUNT(1) AS count
FROM benn.college_football_players
GROUP BY 1
It counts the number of players in each year_group
What does below code do?
SELECT state,
COUNT(CASE WHEN year = ‘FR’ THEN 1 ELSE NULL END) AS fr_count,
COUNT(CASE WHEN year = ‘SO’ THEN 1 ELSE NULL END) AS so_count,
COUNT(CASE WHEN year = ‘JR’ THEN 1 ELSE NULL END) AS jr_count,
COUNT(CASE WHEN year = ‘SR’ THEN 1 ELSE NULL END) AS sr_count,
COUNT(1) AS total_players
FROM benn.college_football_players
GROUP BY state
ORDER BY total_players DESC
displays the number of players in each state, with FR, SO, JR, and SR players in separate columns and another column for the total number of players. Order results such that states with the most players come first.
What does below code do?
SELECT CASE WHEN state IN (‘CA’, ‘OR’, ‘WA’) THEN ‘West Coast’
WHEN state = ‘TX’ THEN ‘Texas’
ELSE ‘Other’ END AS arbitrary_regional_designation,
COUNT(1) AS players
FROM benn.college_football_players
WHERE weight >= 300
GROUP BY 1
Counts the number of 300lb+ players for each of the following regions: West Coast (CA, OR, WA), Texas, and Other (everywhere else).