Relational model mental model in SQL - Time & Space Complexity
We want to understand how the time to run queries grows as the amount of data grows in a relational database.
How does the database handle more rows and tables when we ask questions?
Analyze the time complexity of the following SQL query.
SELECT e.name, d.department_name
FROM employees e
JOIN departments d ON e.department_id = d.id
WHERE e.salary > 50000;
This query finds employees earning more than 50,000 and shows their department names by joining two tables.
Look for repeated steps the database must do to answer the query.
- Primary operation: Scanning the employees table and matching each employee to a department.
- How many times: Once for each employee row, the database checks the salary and then finds the matching department.
As the number of employees grows, the work grows too because each employee must be checked and matched.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks and matches |
| 100 | About 100 checks and matches |
| 1000 | About 1000 checks and matches |
Pattern observation: The work grows roughly in direct proportion to the number of employees.
Time Complexity: O(n)
This means the time to run the query grows linearly with the number of employee rows.
[X] Wrong: "Joining tables always takes the same time no matter how big the tables are."
[OK] Correct: The database must look at each row in the main table and find matching rows in the joined table, so more rows mean more work.
Understanding how query time grows helps you explain how databases handle more data, a useful skill when discussing real-world data problems.
"What if we added an index on the department_id column? How would the time complexity change?"
Practice
Solution
Step 1: Understand the relational model basics
In the relational model, data is organized into tables that represent entities or things.Step 2: Identify what a table holds
A table holds many rows, each row is a record about the thing the table represents.Final Answer:
A collection of related records about a thing -> Option AQuick Check:
Table = collection of records [OK]
- Confusing a table with a single data value
- Thinking a table is a program or software
- Mixing up rows and columns
Solution
Step 1: Recall the role of a primary key
A primary key uniquely identifies each record in a table, so no two rows share the same primary key value.Step 2: Check the options
Only A column or set of columns that uniquely identifies each row describes uniqueness and identification, others describe unrelated properties.Final Answer:
A column or set of columns that uniquely identifies each row -> Option AQuick Check:
Primary key = unique identifier [OK]
- Thinking primary key allows duplicates
- Confusing primary key with sorting or data type
- Assuming primary key is always numeric
Employees with columns EmpID (primary key), Name, and Department, what will the following SQL query return?SELECT Name FROM Employees WHERE Department = 'Sales';Solution
Step 1: Understand the SELECT and WHERE clauses
The query selects the Name column from Employees where Department equals 'Sales'.Step 2: Determine the output
It returns all employee names whose Department is exactly 'Sales'. It does not return others or cause errors.Final Answer:
All employee names who work in the Sales department -> Option CQuick Check:
WHERE filters rows, SELECT chooses columns [OK]
- Thinking it returns all employees ignoring WHERE
- Assuming Department must be primary key to filter
- Expecting error due to non-primary key in WHERE
CREATE TABLE Products (ProductID INT, Name VARCHAR(50), Price DECIMAL);
ALTER TABLE Products ADD PRIMARY KEY (ProductID);
INSERT INTO Products VALUES (1, 'Pen', 1.5), (1, 'Pencil', 0.5);What is the problem with this code?
Solution
Step 1: Check primary key uniqueness
Primary key ProductID must be unique. The insert tries to add two rows with ProductID = 1.Step 2: Identify the error cause
Inserting duplicate primary keys violates uniqueness, causing an error.Final Answer:
It tries to insert duplicate primary key values -> Option BQuick Check:
Primary key must be unique [OK]
- Ignoring duplicate key violation
- Thinking DECIMAL is invalid for Price
- Assuming primary key syntax is wrong
Orders(OrderID, CustomerID, Amount) and Customers(CustomerID, Name). Which relational model concept explains why CustomerID appears in both tables?Solution
Step 1: Understand the role of CustomerID in both tables
CustomerID uniquely identifies customers in Customers and links orders to customers in Orders.Step 2: Identify relational model concept
This linking is done by a foreign key, which references the primary key in another table to connect related data.Final Answer:
Foreign key linking related records between tables -> Option DQuick Check:
Foreign key = link between tables [OK]
- Confusing foreign key with primary key duplication
- Thinking it's just data redundancy
- Mixing foreign key with column aliasing
Ready to prove it?
Pass the test and this pattern moves unknown
