Learn SQL_ Aggregate Functions Cheatsheet _ Codecademy
Learn SQL_ Aggregate Functions Cheatsheet _ Codecademy
Aggregate Functions
Column References
The GROUP BY and ORDER BY clauses can
reference the selected columns by number in SELECT COUNT(*) AS
which they appear in the SELECT statement. 'total_movies',
The example query will count the number of rating
movies per rating, and will:
FROM movies
● GROUP BY column 2 ( rating )
GROUP BY 2
ORDER BY 1;
● ORDER BY column 1 ( total_movies )
HAVING Clause
The HAVING clause is used to further lter the
result set groups provided by the GROUP BY SELECT year,
clause. HAVING is often used with aggregate COUNT(*)
functions to lter the result set groups based on FROM movies
an aggregate property. The given query will
GROUP BY year
select only the records (rows) from only years
HAVING COUNT(*) > 5;
where more than 5 movies were released per
year.
ROUND() Function
The ROUND() function will round a number
value to a speci ed number of places. It takes SELECT year,
two arguments: a number, and a number of ROUND(AVG(rating), 2)
decimal places. It can be combined with other FROM movies
aggregate functions, as shown in the given
WHERE year = 2015;
query. This query will calculate the average
rating of movies from 2015, rounding to 2
decimal places.
Aggregate Functions
Aggregate functions perform a calculation on a
set of values and return a single value:
● COUNT()
● SUM()
● MAX()
● MIN()
● AVG()