๐ What Youโll Learn
- What
MIN()
andMAX()
functions do - Syntax and structure
- Practical examples with numbers and dates
- Use cases with
GROUP BY
๐ What Are MIN and MAX?
MIN()
returns the lowest value in a columnMAX()
returns the highest value in a column
These are aggregate functions, meaning they work on a set of values and return a single result.
๐งฑ Basic Syntax
SELECT MIN(column_name) FROM table_name;
SELECT MAX(column_name) FROM table_name;
You can also use them alongside WHERE
, GROUP BY
, and other clauses.
๐งช Example: Using MIN and MAX
Find the lowest price in the products
table
SELECT MIN(price) AS lowest_price
FROM products;
Find the most expensive product
SELECT MAX(price) AS highest_price
FROM products;
๐งช Example: Dates
Get the first and last registered user by date
SELECT MIN(signup_date) AS first_signup,
MAX(signup_date) AS last_signup
FROM users;
๐ Example with GROUP BY
Get the highest salary per department
SELECT department, MAX(salary) AS top_salary
FROM employees
GROUP BY department;
๐ Recap
MIN()
gives the smallest value;MAX()
gives the largest- Work with numbers, dates, and text (alphabetically)
- Useful for reporting, summaries, and comparisons
- Combine with
GROUP BY
for segmented analysis