Projection operation in DBMS Theory - Time & Space Complexity
Projection operation selects specific columns from a database table.
We want to know how the time to perform projection changes as the table grows.
Analyze the time complexity of the following SQL query.
SELECT name, age FROM employees;
This query retrieves only the name and age columns from all rows in the employees table.
Look for repeated actions in the operation.
- Primary operation: Reading each row to extract selected columns.
- How many times: Once for every row in the table.
The time grows directly with the number of rows because each row is checked once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 reads |
| 100 | 100 reads |
| 1000 | 1000 reads |
Pattern observation: Doubling rows doubles the work; time grows in a straight line with input size.
Time Complexity: O(n)
This means the time to perform projection grows directly with the number of rows in the table.
[X] Wrong: "Projection is instant because it only selects columns, so size doesn't matter."
[OK] Correct: Even though only some columns are chosen, the database still reads every row to get those columns, so time grows with the number of rows.
Understanding how projection scales helps you explain query performance clearly and confidently in real situations.
"What if we add a WHERE clause to filter rows before projection? How would the time complexity change?"
Practice
Solution
Step 1: Understand the purpose of projection
Projection is used to choose certain columns from a table, not rows or sorting.Step 2: Compare options with definition
Only Select specific columns from a table matches the definition of projection as selecting columns.Final Answer:
Select specific columns from a table -> Option AQuick Check:
Projection = Selecting columns [OK]
- Confusing projection with selection (which filters rows)
- Thinking projection changes row order
- Mixing projection with join operations
name and age from a table students?Solution
Step 1: Recall correct SELECT syntax
The correct syntax uses SELECT followed by column names separated by commas, then FROM table.Step 2: Check each option
SELECT name, age FROM students; matches the correct syntax: SELECT name, age FROM students;Final Answer:
SELECT name, age FROM students; -> Option DQuick Check:
Correct SELECT syntax = SELECT name, age FROM students; [OK]
- Using * instead of column names for projection
- Using AND instead of comma between columns
- Omitting commas between column names
employees with columns id, name, and salary, what will the result of SELECT name FROM employees; be?Solution
Step 1: Understand the SELECT statement
SELECT name FROM employees returns all rows but only the 'name' column values.Step 2: Compare options with expected output
All rows with only thenamecolumn values correctly describes the output as all rows with only the 'name' column.Final Answer:
All rows with only the name column values -> Option AQuick Check:
Projection selects columns, so only 'name' column shown [OK]
- Thinking it returns only one row
- Expecting multiple columns without listing them
- Confusing projection with filtering rows
SELECT name age FROM employees;Solution
Step 1: Analyze the column list syntax
Column names must be separated by commas in SELECT statements.Step 2: Check the given query
The query has 'name age' without a comma, which is a syntax error.Final Answer:
Missing comma between column names -> Option CQuick Check:
Columns need commas between them [OK]
- Forgetting commas between columns
- Assuming space works as separator
- Ignoring syntax errors in column list
orders with columns order_id, customer_id, product, and quantity. You want to create a new table order_summary that only stores order_id and quantity. Which SQL statement correctly uses projection to do this?Solution
Step 1: Understand creating a new table with selected columns
To create a new table with specific columns, use CREATE TABLE ... AS SELECT with projection.Step 2: Evaluate each option
CREATE TABLE order_summary AS SELECT order_id, quantity FROM orders; correctly creates the table with only order_id and quantity columns from orders.Final Answer:
CREATE TABLE order_summary AS SELECT order_id, quantity FROM orders; -> Option BQuick Check:
Projection + CREATE TABLE AS SELECT = CREATE TABLE order_summary AS SELECT order_id, quantity FROM orders; [OK]
- Using SELECT * instead of specific columns
- Omitting CREATE TABLE keyword
- Selecting wrong columns for the new table
Ready to prove it?
Pass the test and this pattern moves unknown
