Aggregate Functions: Column References
Aggregate Functions: Column References
Aggregate Functions: Column References
Aggregate Functions
Column References
The GROUP BY and ORDER BY clauses can reference the selected columns by
number in which they appear in the SELECT statement. The example query
will count the number of movies per rating, and will:
SELECT SUM(salary)
FROM salary_disbursement;
SELECT MAX(amount)
FROM transactions;
Note: A column name of the table can also be used instead of * . Unlike
COUNT(*) , this variation COUNT(column) will not count NULL values in that
column.
SELECT COUNT(*)
FROM employees
WHERE experience < 5;
GROUP BY Clause
The GROUP BY clause will group records in a result set by identical values in
one or more columns. It is often used in combination with aggregate functions
to query information of similar records. The GROUP BY clause can come after
FROM or WHERE but must come before any ORDER BY or LIMIT clause.
The given query will count the number of movies per rating.
SELECT rating,
https://www.codecademy.com/learn/learn-sql/modules/learn-sql-aggregate-functions/cheatsheet 2/5
3/30/2020 Learn SQL: Aggregate Functions Reference Guide | Codecademy
COUNT(*)
FROM movies
GROUP BY rating;
SELECT MIN(amount)
FROM transactions;
SELECT AVG(salary)
FROM employees
WHERE experience < 5;
HAVING Clause
The HAVING clause is used to further filter the result set groups provided by
the GROUP BY clause. HAVING is often used with aggregate functions to filter
the result set groups based on an aggregate property. The given query will
https://www.codecademy.com/learn/learn-sql/modules/learn-sql-aggregate-functions/cheatsheet 3/5
3/30/2020 Learn SQL: Aggregate Functions Reference Guide | Codecademy
select only the records (rows) from only years where more than 5 movies
were released per year.
SELECT year,
COUNT(*)
FROM movies
GROUP BY year
HAVING COUNT(*) > 5;
● COUNT()
● SUM()
● MAX()
● MIN()
● AVG()
ROUND() Function
The ROUND() function will round a number value to a specified number of
places. It takes two arguments: a number, and a number of decimal places. It
can be combined with other aggregate functions, as shown in the given
query. This query will calculate the average rating of movies from 2015,
rounding to 2 decimal places.
SELECT year,
ROUND(AVG(rating), 2)
FROM movies
WHERE year = 2015;
https://www.codecademy.com/learn/learn-sql/modules/learn-sql-aggregate-functions/cheatsheet 4/5
3/30/2020 Learn SQL: Aggregate Functions Reference Guide | Codecademy
Related Courses
Course
Learn SQL
Learn to communicate with
databases using SQL, the standard
https://www.codecademy.com/learn/learn-sql/modules/learn-sql-aggregate-functions/cheatsheet 5/5