How the database engine processes a SELECT in SQL - Performance & Efficiency
When you run a SELECT query, the database engine works to find and return the data you asked for.
We want to understand how the time it takes grows as the data gets bigger.
Analyze the time complexity of the following code snippet.
SELECT *
FROM employees
WHERE department = 'Sales';
This query finds all employees who work in the Sales department.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each row in the employees table to see if the department is 'Sales'.
- How many times: Once for every row in the table.
As the number of employees grows, the database checks more rows one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The work grows directly with the number of rows.
Time Complexity: O(n)
This means the time to run the query grows in a straight line as the table gets bigger.
[X] Wrong: "The database instantly finds the rows without checking each one."
[OK] Correct: Without special help like indexes, the database must look at every row to be sure.
Understanding how queries scale helps you explain your thinking clearly and shows you know what happens behind the scenes.
"What if we added an index on the department column? How would the time complexity change?"
Practice
SELECT query?Solution
Step 1: Understand the role of the
TheWHEREclauseWHEREclause filters rows early to reduce data for later steps.Step 2: Recognize the order of operations in
The database first filters rows, then picks columns, groups, and sorts.SELECTFinal Answer:
It reads the table and filters rows based on theWHEREclause. -> Option CQuick Check:
Filtering rows happens before selecting columns [OK]
- Thinking
ORDER BYhappens before filtering - Assuming columns are picked before filtering rows
- Confusing grouping with filtering order
name and age columns from a table called people?Solution
Step 1: Recall correct
Columns are listed separated by commas afterSELECTsyntax for columnsSELECT.Step 2: Check each option's syntax
SELECT name, age FROM people; uses commas correctly; others use wrong keywords or miss commas.Final Answer:
SELECT name, age FROM people; -> Option DQuick Check:
Columns separated by commas inSELECT[OK]
- Using AND instead of commas between columns
- Trying to filter columns in WHERE clause
- Omitting commas between column names
employees with columns id, name, and salary, what will this query return?SELECT name FROM employees WHERE salary > 50000 ORDER BY name;
Solution
Step 1: Analyze the
The query filters employees with salary greater than 50000.WHEREclauseStep 2: Understand
Results are sorted byORDER BYeffectnamealphabetically.Final Answer:
All employee names sorted alphabetically with salary above 50000. -> Option BQuick Check:
Filter then sort results [OK]
- Ignoring the salary filter
- Thinking GROUP BY is needed for ORDER BY
- Assuming all rows are returned
SELECT id, name FROM users WHERE age > 18 ORDER BY;
Solution
Step 1: Check
ORDER BYsyntaxORDER BYmust specify at least one column to sort by.Step 2: Identify error in query
The query endsORDER BY;without column name, causing syntax error.Final Answer:
Missing column name after ORDER BY causes syntax error. -> Option AQuick Check:
ORDER BYneeds a column name [OK]
- Placing WHERE after ORDER BY
- Thinking ORDER BY needs all SELECT columns
- Believing ORDER BY can't be used with WHERE
Solution
Step 1: Apply
Filter employees with salary > 40000 before grouping by department.WHEREbefore groupingStep 2: Use
Group filtered rows by department and compute average salary.GROUP BYon department and calculate average salaryFinal Answer:
SELECT department, AVG(salary) FROM employees WHERE salary > 40000 GROUP BY department; -> Option AQuick Check:
Filter rows before grouping for correct average [OK]
- Placing WHERE after GROUP BY
- Using HAVING without aggregation condition
- Grouping by a condition instead of column
Ready to prove it?
Pass the test and this pattern moves unknown
