SELECT with expressions and calculations in SQL - Time & Space Complexity
We want to understand how the time it takes to run a SELECT query with calculations changes as the data grows.
How does adding more rows affect the work the database does?
Analyze the time complexity of the following code snippet.
SELECT product_id, price, quantity, price * quantity AS total_cost
FROM sales;
This query selects each sale's product ID, price, quantity, and calculates the total cost by multiplying price and quantity for each row.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The database reads each row in the sales table once.
- How many times: Once per row, it performs the multiplication calculation.
As the number of rows grows, the database does more calculations, one for each row.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 multiplications |
| 100 | 100 multiplications |
| 1000 | 1000 multiplications |
Pattern observation: The work grows directly with the number of rows; double the rows, double the calculations.
Time Complexity: O(n)
This means the time to run the query grows in a straight line with the number of rows in the table.
[X] Wrong: "Calculations inside SELECT make the query much slower than just selecting columns."
[OK] Correct: The calculation is simple and done once per row, so it adds only a small, direct cost proportional to the number of rows.
Understanding how simple calculations affect query time helps you explain performance clearly and shows you know how databases handle data row by row.
"What if we added a JOIN to another table with m rows? How would the time complexity change?"
Practice
SELECT price * quantity AS total_cost FROM sales; do?Solution
Step 1: Understand the multiplication in SELECT
The expression multiplies the values in columns price and quantity for each row.Step 2: Recognize the alias usage
The result of multiplication is renamed as total_cost for clarity in output.Final Answer:
Calculates total cost by multiplying price and quantity for each row. -> Option AQuick Check:
price * quantity = total_cost [OK]
- Confusing multiplication with addition or division
- Ignoring the AS alias
- Thinking it filters rows
Solution
Step 1: Identify correct alias syntax
In SQL, the keyword AS is used to rename columns or expressions in SELECT.Step 2: Check each option's syntax
SELECT AVG(price) AS avg_price FROM products; uses AVG(price) AS avg_price which is the correct syntax; others use invalid keywords or omit AS.Final Answer:
SELECT AVG(price) AS avg_price FROM products; -> Option AQuick Check:
Use AS to rename columns [OK]
- Omitting AS keyword
- Using TO or RENAME instead of AS
- Misplacing parentheses
orders with columns price and quantity, what is the output of this query?SELECT price, quantity, price * quantity AS total FROM orders WHERE price = 10;
Solution
Step 1: Understand the WHERE clause effect
The query filters rows to only those where price equals 10.Step 2: Calculate total for filtered rows
For each filtered row, it shows price, quantity, and total which is price multiplied by quantity.Final Answer:
Rows where price is 10, showing price, quantity, and total (price*quantity). -> Option CQuick Check:
WHERE price=10 filters rows; total = price*quantity [OK]
- Ignoring WHERE clause filtering
- Expecting all rows instead of filtered
- Thinking GROUP BY is needed here
SELECT price + quantity AS total FROM sales WHERE total > 100;
Solution
Step 1: Understand alias scope in SQL
Aliases defined in SELECT cannot be used in WHERE clause because WHERE is evaluated before SELECT.Step 2: Identify correct fix
Use the full expression (price + quantity) in WHERE or use a subquery/CTE to filter by total.Final Answer:
You cannot use alias 'total' in WHERE clause directly. -> Option BQuick Check:
Alias not usable in WHERE [OK]
- Using alias in WHERE clause
- Thinking + is invalid operator
- Ignoring query evaluation order
discounted_price. Which query correctly applies the discount and rounds the result to 2 decimals?Solution
Step 1: Calculate 10% discount correctly
Discounted price is price minus 10% of price, which is price - (price * 0.10) or price * 0.90.Step 2: Round the result to 2 decimals
Use ROUND(expression, 2) to round to two decimal places for currency formatting.Final Answer:
SELECT ROUND(price - (price * 0.10), 2) AS discounted_price FROM products; -> Option DQuick Check:
Use ROUND(..., 2) for 2 decimals [OK]
- Not rounding the result
- Rounding without specifying decimals
- Using incorrect discount calculation
Ready to prove it?
Pass the test and this pattern moves unknown
