0% found this document useful (0 votes)
67 views

Learn SQL - Aggregate Functions Cheatsheet - Codecademy

This document provides an overview of common SQL aggregate functions including COUNT, SUM, MAX, MIN, and AVG. It explains what each function does, provides basic usage examples, and also covers additional concepts like GROUP BY, HAVING, and ROUND functions that are often used with aggregates.

Uploaded by

kartik8586
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

Learn SQL - Aggregate Functions Cheatsheet - Codecademy

This document provides an overview of common SQL aggregate functions including COUNT, SUM, MAX, MIN, and AVG. It explains what each function does, provides basic usage examples, and also covers additional concepts like GROUP BY, HAVING, and ROUND functions that are often used with aggregates.

Uploaded by

kartik8586
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Cheatsheets / Learn SQL

Aggregate Functions
Aggregate Functions
Aggregate functions perform a calculation on a set of
values and return a single value:

● COUNT()
● SUM()
● MAX()
● MIN()
● AVG()

COUNT() Aggregate Function


The COUNT() aggregate function returns the total
number of rows that match the speci ed criteria. For SELECT COUNT(*)
instance, to nd the total number of employees who FROM employees
have less than 5 years of experience, the given query WHERE experience < 5;
can be used.
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.

SUM() Aggregate Function


The SUM() aggregate function takes the name of a
column as an argument and returns the sum of all the SELECT SUM(salary)
value in that column. FROM salary_disbursement;

AVG() Aggregate Function


The AVG() aggregate function returns the average
value in a column. For instance, to nd the average SELECT AVG(salary)
salary for the employees who have less than 5 FROM employees
years of experience, the given query can be used. WHERE experience < 5;

ROUND() Function
The ROUND() function will round a number value to
a speci ed number of places. It takes two arguments: a SELECT year,
number, and a number of decimal places. It can be ROUND(AVG(rating), 2)
combined with other aggregate functions, as shown in FROM movies
the given query. This query will calculate the average WHERE year = 2015;
rating of movies from 2015, rounding to 2 decimal
places.

/
GROUP BY Clause
The GROUP BY clause will group records in a result
set by identical values in one or more columns. It is SELECT rating,
often used in combination with aggregate functions to COUNT(*)
query information of similar records. The GROUP FROM movies
BY clause can come after FROM or WHERE but GROUP BY rating;
must come before any ORDER BY or LIMIT
clause.
The given query will count the number of movies per
rating.

Column References
The GROUP BY and ORDER BY clauses can
reference the selected columns by number in which SELECT COUNT(*) AS 'total_movies',
they appear in the SELECT statement. The example rating
query will count the number of movies per rating, and FROM movies
will: GROUP BY 2
ORDER BY 1;
● GROUP BY column 2 ( rating )
● 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,
COUNT(*)
clause. HAVING is often used with aggregate
functions to lter the result set groups based on an
FROM movies
aggregate property. The given query will select only the GROUP BY year
records (rows) from only years where more than 5 HAVING COUNT(*) > 5;
movies were released per year.

MAX() Aggregate Function


The MAX() aggregate function takes the name of a
column as an argument and returns the largest value in SELECT MAX(amount)
a column. The given query will return the largest value FROM transactions;
from the amount column.

MIN() Aggregate Function


The MIN() aggregate function returns the smallest
value in a column. For instance, to nd the smallest SELECT MIN(amount)
value of the amount column from the table named FROM transactions;
transactions , the given query can be used.

You might also like