- SQL Basics
- SQL Introduction
- SQL Syntax
- SQL Sample Database
- SQL SELECT
- SQL WHERE
- SQL ORDER BY
- SQL DISTINCT
- SQL LIMIT
- SQL FETCH
- SQL AND
- SQL OR
- SQL BETWEEN
- SQL IN
- SQL LIKE
- SQL IS NULL
- SQL Comparison Operators
- SQL Logical Operators
- SQL Alias
- SQL CASE
- Joins and Subqueries
- SQL INNER JOIN
- SQL LEFT JOIN
- SQL RIGHT JOIN
- SQL FULL OUTER JOIN
- SQL SELF JOIN
- SQL CROSS JOIN
- SQL Subquery
- SQL Correlated Subquery
- SQL UNION
- SQL INTERSECT
- SQL EXCEPT
- Aggregate Functions
- SQL AVG
- SQL COUNT
- SQL MAX
- SQL MIN
- SQL SUM
- SQL GROUP BY
- SQL HAVING
- SQL ROLLUP
- SQL CUBE
- SQL GROUPING SETS
- Database Management
- SQL CREATE DATABASE
- SQL ALTER DATABASE
- SQL DROP DATABASE
- SQL BACKUP DATABASE
- SQL SHOW DATABASES
- SQL SELECT DATABASE
- Table Management
- SQL CREATE TABLE
- SQL ALTER TABLE
- SQL ADD COLUMN
- SQL DROP COLUMN
- SQL DROP TABLE
- SQL TRUNCATE TABLE
- SQL SHOW TABLES
- SQL RENAME TABLE
- SQL Constraints
- SQL Primary Key
- SQL Foreign Key
- SQL UNIQUE Constraint
- SQL CHECK Constraint
- SQL NOT NULL Constraint
SQL MIN
The MIN() function is one of the most frequently used aggregate functions in SQL. Its primary purpose is to scan a specific column and return the smallest (minimum) value found within that dataset. Whether you are looking for the cheapest product in an inventory, the earliest hire date in an employee directory, or the lowest temperature recorded in a weather database, MIN() is the tool for the job.
MIN() is an aggregate function, it is highly efficient when used on indexed columns. If you frequently need to find the minimum or maximum of a large dataset, ensure that column is indexed to avoid full table scans.
Syntax of SQL MIN
To use the MIN() function, you include it in your SELECT statement. It is a best practice to use an alias (using the AS keyword) so the resulting column has a meaningful name in your application's code.
SELECT MIN(column_name) AS alias_name
FROM table_name
WHERE condition;
- MIN(column_name): The aggregate function that targets the column you want to evaluate.
- AS alias_name: Renames the resulting column for better readability (e.g.,
lowest_price). - FROM table_name: Specifies the source table.
- WHERE condition: An optional clause to filter the rows before the minimum value is calculated.
AS keyword to provide a descriptive name for your result. Without it, many database systems will return a column name like "MIN(price)" or "computed_0," which is harder to work with in your backend code.
Key Points
- Data Types:
MIN()isn't just for numbers. It works on Numeric types (lowest number), Date/Time types (earliest date), and String types (first value alphabetically). - NULL Values: The function automatically ignores
NULLvalues. It only compares actual data. - Single Row Output: Unless you are using a
GROUP BYclause,MIN()will always return exactly one row.
NULL values, the MIN() function will return NULL. Always ensure your application logic can handle a potential null result.
Example of SQL MIN
Let's look at a practical example using a product inventory. This table tracks different items and their current market prices.
Table: Products
To find the most affordable item in our list, we query the price column:
SELECT MIN(price) AS min_price FROM products;
Output:
Explanation:
- The database engine scans the
pricecolumn. It compares 2.5, 1.2, 3.0, 2.8, and 1.5. Since1.2is the smallest numerical value, it is returned as the result.
SELECT product_name, MIN(price) FROM products;. In most SQL dialects (like PostgreSQL or SQL Server), this will cause an error because product_name is not part of an aggregate function or a GROUP BY clause.
SQL MIN with Conditions
In the real world, you often need to find the minimum value within a specific subset of data. For instance, you might want to find the lowest price among "premium" products (those costing more than $2.00).
SELECT MIN(price) AS min_price_above_2
FROM products
WHERE price > 2.0;
Output:
Explanation:
- The
WHEREclause filters out Banana (1.2) and Orange (1.5). Among the remaining items (Apple, Cherry, Mango), the lowest price is2.5.
SQL MIN with Grouping
The MIN() function becomes incredibly powerful when paired with GROUP BY. This allows you to find the minimum value for every category in your table at once. Imagine our products had a category column:
SELECT category, MIN(price) AS min_price
FROM products
GROUP BY category;
Output:
Explanation:
- The database groups all rows by their
categoryand then calculates theMIN(price)for each specific group independently.
SQL MIN with DISTINCT
You can use the DISTINCT keyword inside the MIN() function, although it is rarely necessary. DISTINCT tells SQL to only look at unique values before finding the minimum.
SELECT MIN(DISTINCT price) AS min_distinct_price FROM products;
Output:
Explanation:
- Logic-wise, the minimum value of a set is the same whether you include duplicates or not. For example,
MIN(1, 1, 2, 3)is 1, andMIN(DISTINCT 1, 1, 2, 3)is also 1. Therefore,DISTINCTis seldom used withMIN().
Summary
The MIN() function is an essential tool for data analysis in SQL. It is used to find the "floor" of your data the smallest number, the earliest date, or the first string alphabetically. To get the most out of it, remember to use aliases for clarity, leverage GROUP BY for categorical reports, and be mindful of how your database handles NULL values during aggregation.