SQL - Aggregate Functions Flashcards
count the number of rows
COUNT()
SELECT COUNT(*)
FROM table_name;
the sum of the values in a column
SUM()
SELECT SUM(downloads)
FROM fake_apps;
the largest/smallest value
MAX(), MIN()
SELECT MAX(downloads)
FROM fake_apps;
the average of the values in a column
AVG()
SELECT AVG(downloads)
FROM fake_apps;
round the values in the column
ROUND()
SELECT ROUND(price, 0)
FROM fake_apps;
_______ is a clause used with aggregate functions to combine data from one or more columns
GROUP BY
SELECT year,
AVG(imdb_rating)
FROM movies
GROUP BY year
ORDER BY year;
_______ limit the results of a query based on an aggregate property.
HAVING
SELECT year,
genre,
COUNT(name)
FROM movies
GROUP BY 1, 2
HAVING COUNT(name) > 10;
Aggregate functions
Aggregate functions combine multiple rows together to form a single value of more meaningful information.
Find the error in this code:
SELECT COUNT(*)
FROM songs
HAVING plays > 100;
It should be WHERE instead of HAVING.
It should be WHERE plays > 100 because WHERE filter rows and HAVING filter groups.
Which function takes a column and returns the total sum of the numeric values in that column?
SUM()
What does the following query do?
SELECT price,
COUNT(*)
FROM menu
WHERE orders > 50
GROUP BY price;
It calculates the total number of menu items that have been ordered more than 50 times – for each price.
How would you calculate the minimum number of stops from the train table?
SELECT MIN(stops)
FROM train;
What does the ROUND function take as argument(s)?
The column name, and the number of decimal places to round the values in the column to.
The WHERE clause filters rows, whereas the HAVING clause filter groups. T/F
T
What does the COUNT() function take as argument(s)?
The name of a column or *.