SQL MIN() Function |
SQL Sample Code - SQL MIN() Function |
| SELECT MIN(column_name) FROM table_name; |
See the SQL samples section below for an explanation of the MIN function.
What does the MIN() function do? |
The SQL MIN() function simply retrieves the smallest value that exists in a specified column.
Note: The MIN() function can only be used on database columns with numeric based data-types. See our data-types section for more information.
Sample 1 – SQL MIN() Function |
In this SQL sample we will use the database table ‘tblCompany’.
| Company_ID | CompanyName | Address | Town | Sales |
| 1 | SQL Sample | 1 Sample St | Hamburg | 10000 |
| 2 | SQL Code Land | 2 Code Rd | Hamburg | 20000 |
| 3 | Sample Code World | 66 SQL St | Curry | 18000 |
| 4 | SQL Reference Ltd | 34 Reference St | Pisa | 17000 |
In this SQL sample we simply want to know the smallest Sales figure in the tblCompany table.
Use the SELECT statement below:
| SELECT MIN(Sales) AS Sales_Min FROM tblCompany; |
The result will look like this:
| Sales_Min |
| 10000 |
Note: the AS operator is used to name the result column as ‘Sales_Min’. See the Alias section of this website to learn more about renaming database columns and tables.
Sample 2 – SQL MIN() Function using GROUP BY. |
In this example we will use the database table ‘tblCompany’.
| Company_ID | CompanyName | Address | Town | Sales |
| 1 | SQL Sample | 1 Sample St | Hamburg | 10000 |
| 2 | SQL Code Land | 2 Code Rd | Hamburg | 20000 |
| 3 | Sample Code World | 66 SQL St | Curry | 18000 |
| 4 | SQL Reference Ltd | 34 Reference St | Pisa | 17000 |
In this SQL sample we want to know the smallest Sales records for each town in the tblCompany table.
Use the SELECT statement below:
| SELECT Town, MIN(Sales) FROM tblCompany GROUP BY Town; |
The result will look like this:
| Town | COUNT(Sales) |
| Hamburg | 10000 |
| Curry | 18000 |
| Pisa | 17000 |
Related SQL Sample Code:
|
|