Menu

SQL MIN and MAX Functions – Unleashing Your Data’s Extremes

Written by Jagdeesh | 3 min read

Let’s dive into the world of SQL (Structured Query Language) to explore two incredibly valuable aggregate functions: MIN() and MAX().

These functions allow you to discover the smallest and largest values in a particular column of your data set, respectively. Understanding and properly leveraging these functions can be a game-changer when dealing with massive datasets and performing comprehensive data analyses.

SQL: A Quick Recap

SQL, for those unfamiliar, is a standardized programming language that is used to manage and manipulate databases. It allows you to query, insert, update, and modify data in a relational database management system (RDBMS), like MySQL, PostgreSQL, Oracle, or SQLite.

Understanding MIN() and MAX() Functions

Now, onto the main topic. MIN() and MAX() are two built-in functions in SQL that allow you to quickly find the smallest and largest values in a column of a table. They are particularly useful when you need to identify the extremes in your data.

Here is the basic syntax of both functions

sql
SELECT MIN(column_name) FROM table_name;
SELECT MAX(column_name) FROM table_name;

Note: You can replace column_name and table_name with the column and the table from your database that you wish to query.

A Sample Dataset

To illustrate these functions in action, let’s take a look at a sample data set. Assume we have a table called “Products”, with the following data:

sql
CREATE TABLE Products (
    ProductID INT,
    ProductName VARCHAR(255),
    Price INT
);

INSERT INTO Products (ProductID, ProductName, Price)
VALUES 
    (1, 'Chair', 45),
    (2, 'Table', 80),
    (3, 'Desk', 120),
    (4, 'Lamp', 30),
    (5, 'Bookshelf', 100);

Output:

output
ProductID   ProductName   Price
---------   -----------   -----
1           Chair         45
2           Table         80
3           Desk          120
4           Lamp          30
5           Bookshelf     100

1) Using the MIN() Function

Let’s say we want to find the product with the lowest price. We would use the MIN() function, applied to the “Price” column:

sql
SELECT MIN(Price) AS LowestPrice FROM Products;

The AS keyword is used to rename the output column as “LowestPrice”. The output of this query would be

output
LowestPrice
-----------
30

2) Finding the maximum price

Similarly, if we want to find the product with the highest price, we would use the MAX() function

sql
SELECT MAX(Price) AS HighestPrice FROM Products;

The output of this query would be

output
HighestPrice
------------
120

3) Going Beyond

We can even combine the MIN() and MAX() functions with other SQL commands.

For example, if we want to find the names of the cheapest and most expensive products, we can combine the MIN() and MAX() functions with the WHERE clause

sql
SELECT ProductName, Price FROM Products WHERE Price = (SELECT MIN(Price) FROM Products);

Output:

output
ProductName    Price
----------    ------
Lamp           30
sql
SELECT ProductName, Price FROM Products WHERE Price = (SELECT MAX(Price) FROM Products);

Output:

output
ProductName    Price
----------    ------
Desk           120

Conclusion

The MIN() and MAX() functions are powerful tools for quickly identifying the smallest and largest values in a data set. They are simple yet effective, and a necessary addition to your SQL toolkit.

Free Course
Master Core Python — Your First Step into AI/ML

Build a strong Python foundation with hands-on exercises designed for aspiring Data Scientists and AI/ML Engineers.

Start Free Course
Trusted by 50,000+ learners
Jagdeesh
Written by
Related Course
Master SQL — Hands-On
Join 5,000+ students at edu.darkorange-mallard-189514.hostingersite.com
Explore Course
Scroll to Top
Scroll to Top
Course Preview

Machine Learning A-Z™: Hands-On Python & R In Data Science

Free Sample Videos:

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science