π 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 BYfor segmented analysis