Cheatsheets / Learn SQL
Aggregate Functions
Column References
‘The hour ay and onbex ay clauses can reference the
selected columns by number in which they appear in the
seiecr statement. The example query will count the
umber of movies per rating, and wil:
nour by column 2 ( rating )
onoee ey column 4 ( total_sovies }
SUM() Aggregate Function
‘The sum) aggregate function takes the name of a
‘column as an argument and returns the sum of all the
value in that column,
MAX() Aggregate Function
‘The wx() aggregate function takes the name of a
column as an argument and returns the largest value in a
‘column, The given query will return the largest value from
the srount column,
COUNT() Aggregate Function
“The couwr() aggregate function returns the total number
of rows that match the specified criteria. For instance, to
find the total number of employees who have less than 5
years of experience, the given query can be used.
Note: A column name of the table can also be used
instead of + . Unlike couvr¢+) , thi variation
coumr(coiune) willnot count suit values in that
column.
GROUP BY Clause
‘The tour ay 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 Grou» sy clause can
come after Fo or but must come before any
conver BY or art clause.
‘The given query will count the number of movies per
rating.
SELECT COUNT(*) AS ‘total_movies'
FROM
GROUP BY 2
RDER BY 1
SELECT SUM( )
FROM
SELECT MAX( )
FROM
EC
COUNT (*)
FROM
GROUP BYMIN() Aggregate Function
The mint) aggregate function returns the smallest value
Ina column. For instance, to find the smallest value of the
anount_ column from the table named transactions , the
sven query can be used.
AVG() Aggregate Function
‘The avc() aggregate function returns the average value
ina column. For instance, to find the average salary for
the employees who have less than 6 years of experience,
the given query can be used,
HAVING Clause
The saviNe clause Is uses to further fiter the result set
groups provided by the crove ay clause. ‘avin is often
used with aggregate functions to fter the result set
groups based on an aggregate property, The given query
will select only the records (rows) from only years where
more than 5 movies were released per year.
The savins clause must always come after a croup oy
clause but must come before any onoeR sy or LintT
clause.
Aggregate Functions
‘Aggregate functions perform a calculation on a set of
values and return a single value:
coun)
sm)
xe)
ran
ave)
ROUND() Function
‘The rowo() 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
ven query. This query will calculate the average rating of
movies from 2085, rounding to 2 decimal places.
SELECT MIN( )
FROM
SELECT AVG( )
FROM
WHERE 5
SELECT year.
COUNT(*)
FROM
GROUP BY year
HAVING COUNT(*) > 5
SELECT year
ROUND (AVG( ), 2)
ROM
WHERE year = 2015.