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

Calculating Aggregates - Aggregate Functions Cheatsheet - Codecademy

Calculating Aggregates_ Aggregate Functions Cheatsheet _ Codecademy

Uploaded by

ANASS NASSIMI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Calculating Aggregates - Aggregate Functions Cheatsheet - Codecademy

Calculating Aggregates_ Aggregate Functions Cheatsheet _ Codecademy

Uploaded by

ANASS NASSIMI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Cheatsheets / Calculating Aggregates

Aggregate Functions

Column References

The GROUP BY and ORDER BY clauses can SELECT COUNT(*) AS 'total_movies',


reference the selected columns by number in which
rating
they appear in the SELECT statement. The example
query will count the number of movies per rating, and FROM movies
will: GROUP BY 2
GROUP BY column 2 ( rating ) ORDER BY 1;
ORDER BY column 1
( total_movies )

SUM() Aggregate Function

The SUM() aggregate function takes the name of a SELECT SUM(salary)


column as an argument and returns the sum of all the
FROM salary_disbursement;
value in that column.

MAX() Aggregate Function

The MAX() aggregate function takes the name of a SELECT MAX(amount)


column as an argument and returns the largest value in
FROM transactions;
a column. The given query will return the largest value
from the amount column.

COUNT() Aggregate Function

The COUNT() aggregate function returns the total SELECT COUNT(*)


number of rows that match the specified criteria. For
FROM employees
instance, to find the total number of employees who
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.
GROUP BY Clause

The GROUP BY clause will group records in a result SELECT rating,


set by identical values in one or more columns. It is
COUNT(*)
often used in combination with aggregate functions to
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.

MIN() Aggregate Function

The MIN() aggregate function returns the smallest SELECT MIN(amount)


value in a column. For instance, to find the smallest
FROM transactions;
value of the amount column from the table named
transactions , the given query can be used.

AVG() Aggregate Function

The AVG() aggregate function returns the average SELECT AVG(salary)


value in a column. For instance, to find the average
FROM employees
salary for the employees who have less than 5
years of experience, the given query can be used. WHERE experience < 5;

HAVING Clause

The HAVING clause is used to further filter the result SELECT year,
set groups provided by the GROUP BY clause.
COUNT(*)
HAVING is often used with aggregate functions to
FROM movies
filter the result set groups based on an aggregate
property. The given query will select only the records GROUP BY year
(rows) from only years where more than 5 movies were HAVING COUNT(*) > 5;
released per year.
The HAVING clause must always come after a
GROUP BY clause but must come before any
ORDER BY or LIMIT clause.
Aggregate Functions

Aggregate functions perform a calculation on a set of


values and return a single value:
COUNT()
SUM()
MAX()
MIN()
AVG()

ROUND() Function

The ROUND() function will round a number value to SELECT year,


a specified number of places. It takes two arguments: a
ROUND(AVG(rating), 2)
number, and a number of decimal places. It can be
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.

Print Share

You might also like