SQL MIN and MAX Tutorial
Example Table
We will use the following table named products
for our examples:
CREATE TABLE products (
product_id INT PRIMARY KEY,
product_name VARCHAR(50),
price DECIMAL(10, 2)
);
INSERT INTO products (product_id, product_name, price) VALUES
(1, 'Laptop', 999.99),
(2, 'Smartphone', 499.99),
(3, 'Tablet', 299.99),
(4, 'Monitor', 199.99),
(5, 'Keyboard', 49.99);
Products Table
product_id | product_name | price |
---|---|---|
1 | Laptop | 999.99 |
2 | Smartphone | 499.99 |
3 | Tablet | 299.99 |
4 | Monitor | 199.99 |
5 | Keyboard | 49.99 |
Using SQL MIN
SQL MIN
is used to find the minimum value in a column.
Example: Find the Minimum Price
To find the minimum price of products, use the following query:
SELECT MIN(price) AS min_price
FROM products;
Result:
min_price | 49.99 |
---|
Using SQL MAX
SQL MAX
is used to find the maximum value in a column.
Example: Find the Maximum Price
To find the maximum price of products, use the following query:
SELECT MAX(price) AS max_price
FROM products;
Result:
max_price | 999.99 |
---|