MIN and MAX functions in SQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using MIN and MAX functions in SQL, it's important to understand how the time to find these values grows as the data grows.
We want to know how the work changes when the number of rows increases.
Analyze the time complexity of the following SQL query.
SELECT MIN(price) AS LowestPrice, MAX(price) AS HighestPrice
FROM products;
This query finds the smallest and largest price from the products table.
Look for repeated steps that the database does to find the answer.
- Primary operation: Scanning each row's price to compare values.
- How many times: Once for each row in the products table.
As the number of rows grows, the database checks more prices 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.
Time Complexity: O(n)
This means the time to find MIN and MAX grows in a straight line as the table gets bigger.
[X] Wrong: "MIN and MAX run instantly no matter how big the table is."
[OK] Correct: The database must look at each row to be sure of the smallest or largest value, 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.
"What if the products table had an index on the price column? How would that affect the time complexity?"
Practice
MIN() do when applied to a column of numbers?Solution
Step 1: Understand the purpose of MIN()
TheMIN()function is designed to find the smallest value in a column of data.Step 2: Compare with other functions
UnlikeMAX()which finds the largest, orAVG()which calculates average,MIN()specifically returns the minimum value.Final Answer:
Finds the smallest number in the column -> Option CQuick Check:
MIN() = smallest value [OK]
- Confusing MIN() with MAX()
- Thinking MIN() calculates average
- Using MIN() to count rows
Employees?Solution
Step 1: Recall correct function syntax
The correct syntax for SQL aggregate functions likeMAX()requires parentheses around the column name.Step 2: Check syntax
SELECT MAX(salary) FROM Employees;usesMAX(salary)which is correct. Forms without parentheses or using [] {} are invalid.Final Answer:
SELECT MAX(salary) FROM Employees; -> Option DQuick Check:
MAX() uses parentheses around column [OK]
- Omitting parentheses in function calls
- Using square or curly brackets instead of parentheses
- Misspelling function names
Products with a column Price containing values (100, 250, 50, 400), what will the query SELECT MIN(Price), MAX(Price) FROM Products; return?Solution
Step 1: Identify minimum price
From the values (100, 250, 50, 400), the smallest price is 50.Step 2: Identify maximum price
The largest price in the list is 400.Final Answer:
(50, 400) -> Option BQuick Check:
MIN = 50, MAX = 400 [OK]
- Mixing up MIN and MAX values
- Assuming default zero minimum
- Confusing order of returned values
Users table?SELECT MAX age FROM Users;Solution
Step 1: Check function syntax
TheMAX()function requires parentheses around the column name, soMAX ageis invalid syntax.Step 2: Verify other options
MAX works on numeric columns, table names don't need quotes, and function names are case-insensitive in SQL.Final Answer:
Missing parentheses around the column name -> Option AQuick Check:
MAX() needs parentheses [OK]
- Omitting parentheses in function calls
- Thinking MAX() only works on strings
- Adding unnecessary quotes around table names
Orders with columns OrderID, CustomerID, and TotalAmount. How would you write a query to find the highest order amount for each customer?Solution
Step 1: Understand grouping requirement
To find the highest order amount per customer, we must group data byCustomerID.Step 2: Use MAX() with GROUP BY
UsingMAX(TotalAmount)withGROUP BY CustomerIDreturns the maximum order amount for each customer.Step 3: Why others fail
Using MAX() in a WHERE clause is invalid, WHERE CustomerID; is meaningless, and selecting CustomerID with MAX() without GROUP BY causes an error because CustomerID is not aggregated.Final Answer:
SELECT CustomerID, MAX(TotalAmount) FROM Orders GROUP BY CustomerID; -> Option AQuick Check:
Use GROUP BY with MAX() for per-group max [OK]
- Forgetting GROUP BY when using MAX per group
- Using MAX() in WHERE clause incorrectly
- Selecting columns without grouping
