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


-- Adding 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 the specified number of decimal places
SELECT column2_name, ROUND(column2_name, 2)
FROM table_name; 
                                    

FLOOR function


-- Rounding a given value down to the nearest integer
SELECT column2_name, FLOOR(column2_name)
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;
                                    

The HAVING clause is used to filter groups based on aggregate values after grouping, while the WHERE clause is used to filter individual rows before any grouping takes place. The WHERE clause works with non-aggregated data, whereas HAVING is specifically used with aggregated values such as SUM(), COUNT(), or AVG().