Tables, rows, and columns concept in SQL - Time & Space Complexity
When working with tables in a database, it is important to understand how the time to access data grows as the table gets bigger.
We want to know how the number of rows and columns affects the time it takes to find or process data.
Analyze the time complexity of the following SQL query.
SELECT *
FROM Employees
WHERE Department = 'Sales';
This query searches the Employees table for all rows where the Department column equals 'Sales'.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each row in the Employees table to see if the Department matches 'Sales'.
- How many times: Once for every row in the table.
As the number of rows grows, the database must check more rows one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of operations grows directly with the number of rows.
Time Complexity: O(n)
This means the time to find matching rows grows in a straight line as the table gets bigger.
[X] Wrong: "Adding more columns will make the query slower in the same way as adding more rows."
[OK] Correct: The query time mainly depends on how many rows are checked, not how many columns each row has.
Understanding how table size affects query time helps you explain database performance clearly and confidently.
"What if we added an index on the Department column? How would the time complexity change?"
Practice
row represent in a database table?Solution
Step 1: Understand the structure of a table
A table is made up of rows and columns where columns define data types and rows hold data.Step 2: Identify what a row represents
A row is one complete record with values for each column in the table.Final Answer:
A single record containing data for each column -> Option DQuick Check:
Row = single record [OK]
- Confusing rows with columns
- Thinking rows are table names
- Mixing up rows with database schemas
Employees?Solution
Step 1: Recall SQL syntax for selecting all columns
The standard SQL command to select all columns isSELECT * FROM table_name;.Step 2: Match the correct syntax
SELECT * FROM Employees; matches the correct syntax exactly.Final Answer:
SELECT * FROM Employees; -> Option CQuick Check:
SELECT * = all columns [OK]
- Using GET instead of SELECT
- Writing SELECT ALL COLUMNS which is invalid
- Omitting FROM keyword
Students with columns id, name, and age, what will this query return?SELECT name FROM Students WHERE age > 20;
Solution
Step 1: Analyze the SELECT clause
The query selects only thenamecolumn from the table.Step 2: Analyze the WHERE condition
It filters rows whereageis greater than 20, so only those students are included.Final Answer:
Only the names of students older than 20 -> Option AQuick Check:
SELECT name WHERE age > 20 = names only [OK]
- Thinking all columns are returned
- Assuming age column is returned without selecting it
- Believing query causes error due to WHERE clause
SELECT id, name FROM Students WHERE age = 'twenty';
Solution
Step 1: Check the WHERE clause comparison
The query comparesageto the string 'twenty', butageis numeric.Step 2: Understand effect of comparing number to string
This will not cause a syntax error but likely returns zero rows because no numeric age equals 'twenty'.Final Answer:
Comparing age to a string 'twenty' may cause no matches -> Option BQuick Check:
Wrong data type comparison = no results [OK]
- Thinking quotes cause syntax error
- Assuming query returns matching rows
- Ignoring data type mismatch
Orders with columns order_id, customer_id, and amount. You want to find the total amount spent by each customer. Which SQL query correctly groups and sums the data?Solution
Step 1: Identify the goal
We want total amount spent per customer, so we must group bycustomer_id.Step 2: Use aggregation function
SUM(amount) calculates total amount per group.Step 3: Check query correctness
SELECT customer_id, SUM(amount) FROM Orders GROUP BY customer_id; correctly groups bycustomer_idand sumsamount.Final Answer:
SELECT customer_id, SUM(amount) FROM Orders GROUP BY customer_id; -> Option AQuick Check:
Group by customer_id + SUM(amount) = total per customer [OK]
- Grouping by wrong column
- 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
