In this short blog post I am going to discuss about the functions in SQL with examples. Let's start with the MIN and MAX.
- MIN(): Returns the smallest value from the selected COLUMN.
Syntax:
SELECT MIN(column_name)
FROM table_name
WHERE condition;
Example:
SELECT MIN(LifeExpectancy) AS LifeExpectancy FROM country;
Returns:
Lowest Life Expectancy
37.2
- MAX(): Returns the largest value from the selected COLUMN.
Syntax:
SELECT MAX(column_name)
FROM table_name
WHERE condition;
Example:
SELECT MAX(LifeExpectancy) AS LifeExpectancy FROM country;
Returns:
Highest Life Expectancy
83.5
- COUNT(): Returns the number of rows for the applied condition.
Syntax:
SELECT COUNT(column_name)
FROM table_name
WHERE condition;
Example:
SELECT COUNT(ID) AS ID FROM city;
Returns:
Total Countries
4079
- AVG(): Returns the average for a column. The column has to be numeric or it'll show an error.
Syntax:
SELECT AVG(column_name)
FROM table_name
WHERE condition;
Example:
SELECT AVG(Population) AS Population FROM country;
Returns:
Average Population
25434098.1172
- SUM(): Returns the sum for a column. The column has to be numeric or it'll show an error.
Syntax:
SELECT SUM(column_name)
FROM table_name
WHERE condition;
Example:
SELECT SUM(Population) AS Population FROM country;
Returns:
Total Population
6078749450
database functions