Aggregate functions
LIMIT
clause
-- Specifying a limit on the number of records that will be displayed
SELECT column_name
FROM table_name
LIMIT 5;
COUNT
function
-- Counting records in a table
SELECT COUNT(*)
FROM table_name;
SUM
function
-- Summing up values
SELECT SUM(column_name)
FROM table_name;
AVG
function
-- Calculating the average value
SELECT AVG(column_name)
FROM table_name;
MAX
function
-- Finding the highest value
SELECT MAX(column_name)
FROM table_name;
MIN
function
-- Finding the lowest value
SELECT MIN(column_name)
FROM table_name;
ROUND
function
-- Rounding a given value to a certain number of decimal places
SELECT column2_name, ROUND(column2_name, 2)
FROM table_name;
GROUP BY
clause
-- Grouping records by the values of a specified column
SELECT column_name, COUNT(*)
FROM table_name
GROUP BY column_name;
Column references
Referring to columns by their order in the SELECT
clause.
1 - column1_name
2 - column2_name
SELECT column1_name, column2_name
FROM table_name
GROUP BY 2
ORDER BY 1;
HAVING
clause
-- Adding a condition to the GROUP BY clause
SELECT column_name, COUNT(*)
FROM table_name
GROUP BY column_name
HAVING COUNT(*) < 3;