MIN and MAX functions in MySQL - Time & Space Complexity
When using MIN and MAX functions in SQL, it's important to know how the time to find these values changes as the data grows.
We want to understand how the work done increases when the number of rows gets bigger.
Analyze the time complexity of the following code snippet.
SELECT MIN(price) AS LowestPrice, MAX(price) AS HighestPrice
FROM products;
This query finds the smallest and largest price values from the products table.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning each row in the products table once to compare prices.
- How many times: Once per row, so as many times as there are rows.
As the number of rows grows, the database checks each price once to find the minimum and maximum.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 comparisons |
| 100 | 100 comparisons |
| 1000 | 1000 comparisons |
Pattern observation: The work grows directly with the number of rows; double the rows means double the checks.
Time Complexity: O(n)
This means the time to find MIN and MAX grows in a straight line with the number of rows.
[X] Wrong: "MIN and MAX run instantly no matter how big the table is because they just pick one value."
[OK] Correct: The database must look at every row to be sure which value is smallest or largest, so more rows mean more work.
Understanding how MIN and MAX scale helps you explain query performance clearly and shows you know how databases handle data behind the scenes.
"What if the products table had an index on the price column? How would the time complexity change?"
Practice
MIN function do in MySQL?Solution
Step 1: Understand the purpose of MIN function
The MIN function is designed to find the smallest value in a specified column of a table.Step 2: Compare with other aggregate functions
Unlike MAX which finds the largest value, MIN specifically returns the minimum value.Final Answer:
Finds the smallest value in a column -> Option AQuick Check:
MIN = smallest value [OK]
- Confusing MIN with MAX
- Thinking MIN counts rows
- Assuming MIN calculates average
price column of a table named products?Solution
Step 1: Recall correct SQL aggregate function syntax
The correct syntax for using MAX is: SELECT MAX(column_name) FROM table_name;Step 2: Check each option for syntax errors
SELECT MAX(price) FROM products; uses correct parentheses and FROM clause. Options A, B, and D have syntax errors or incomplete clauses.Final Answer:
SELECT MAX(price) FROM products; -> Option DQuick Check:
MAX(price) syntax = SELECT MAX(price) FROM table [OK]
- Omitting parentheses in MAX function
- Using IN instead of FROM
- Leaving incomplete WHERE clause
employees with a column salary containing values (3000, 4500, 2500, 5000), what will this query return?SELECT MIN(salary), MAX(salary) FROM employees;Solution
Step 1: Identify the smallest and largest salary values
The salaries are 3000, 4500, 2500, and 5000. The smallest is 2500 and the largest is 5000.Step 2: Understand the query output format
The query returns a single row with two columns: MIN(salary) and MAX(salary), so the output is (2500, 5000).Final Answer:
(2500, 5000) -> Option BQuick Check:
MIN=2500, MAX=5000 [OK]
- Mixing up MIN and MAX values
- Assuming output is multiple rows
- Confusing order of returned values
users table?SELECT MIN age FROM users;Solution
Step 1: Check the syntax of the MIN function usage
The MIN function requires parentheses around the column name, like MIN(age).Step 2: Identify the error in the query
The query is missing parentheses, so it will cause a syntax error.Final Answer:
Missing parentheses around the column name -> Option AQuick Check:
MIN requires parentheses [OK]
- Omitting parentheses in function calls
- Assuming WHERE clause is mandatory
- Thinking MIN can't be used on numbers
orders with columns order_id, customer_id, and amount. How would you write a query to find the highest order amount for each customer?Solution
Step 1: Understand the requirement to group by customer
We want the highest order amount per customer, so we must group results by customer_id.Step 2: Use MAX with GROUP BY
Using MAX(amount) with GROUP BY customer_id returns the maximum amount for each customer.Step 3: Check each option
SELECT customer_id, MAX(amount) FROM orders GROUP BY customer_id; correctly groups by customer_id and selects MAX(amount). SELECT MAX(amount) FROM orders GROUP BY customer_id; misses customer_id in SELECT. SELECT customer_id, amount FROM orders WHERE amount = MAX(amount); uses invalid WHERE with aggregate. SELECT customer_id, MAX(amount) FROM orders; lacks GROUP BY.Final Answer:
SELECT customer_id, MAX(amount) FROM orders GROUP BY customer_id; -> Option CQuick Check:
MAX with GROUP BY returns max per group [OK]
- Not using GROUP BY when aggregating per group
- Using WHERE with aggregate functions incorrectly
- Selecting columns not in GROUP BY without aggregation
Ready to prove it?
Pass the test and this pattern moves unknown
