Selection operation implementation in DBMS Theory - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we run a selection operation in a database, we want to find rows that match a condition.
We ask: How does the time to find these rows grow as the table gets bigger?
Analyze the time complexity of the following SQL query execution.
SELECT *
FROM Employees
WHERE Department = 'Sales';
This query looks through the Employees table to find all rows where the Department is 'Sales'.
In this selection operation:
- Primary operation: Checking each row's Department value.
- How many times: Once for every row in the Employees table.
As the number of rows 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 number of checks 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: "The database only looks at a few rows, so time stays the same no matter the table size."
[OK] Correct: Without an index, the database must check every row to be sure it finds all matches.
Understanding how selection scales helps you explain database performance clearly and shows you know how queries behave with growing data.
"What if the Department column had an index? How would the time complexity change?"
Practice
SELECT statement with a WHERE clause in a database?Solution
Step 1: Understand the role of SELECT
The SELECT statement is used to get data from a table.Step 2: Understand the role of WHERE clause
The WHERE clause filters rows to include only those that meet given conditions.Final Answer:
To retrieve only rows that meet specific conditions -> Option DQuick Check:
SELECT + WHERE = filtered rows [OK]
- Confusing WHERE with DELETE
- Thinking WHERE adds columns
- Believing WHERE changes table structure
Employees where the Age is greater than 30?Solution
Step 1: Check SELECT syntax
The correct syntax starts with SELECT, then columns or *, then FROM table name.Step 2: Check WHERE clause syntax
WHERE must be followed by a condition like Age > 30.Final Answer:
SELECT * FROM Employees WHERE Age > 30; -> Option AQuick Check:
Correct SELECT + FROM + WHERE syntax [OK]
- Omitting FROM keyword
- Placing WHERE before FROM
- Missing semicolon at end
Products with columns ProductID, Name, and Price. What will be the result of this query?SELECT Name FROM Products WHERE Price <= 50;
Solution
Step 1: Understand the SELECT clause
The query selects only the Name column from the Products table.Step 2: Understand the WHERE condition
The condition Price <= 50 filters rows to those with price 50 or less.Final Answer:
All product names with price less than or equal to 50 -> Option BQuick Check:
WHERE Price <= 50 filters products [OK]
- Confusing <= with >= operator
- Expecting all products without filter
- Thinking query causes error
SELECT * FROM Customers WHERE City = 'New York'
Solution
Step 1: Check SQL syntax completeness
SQL statements should end with a semicolon to mark the end.Step 2: Verify other parts
FROM keyword is present, single quotes around string are correct, WHERE comes before ORDER BY.Final Answer:
Missing semicolon at the end -> Option CQuick Check:
SQL statements end with ; [OK]
- Forgetting semicolon
- Misplacing WHERE clause
- Using double quotes instead of single quotes
Orders with columns OrderID, CustomerID, and Status. You want to select all orders that are either 'Pending' or 'Processing'. Which SQL query correctly implements this selection?Solution
Step 1: Understand the condition for multiple values
To select rows where Status matches multiple values, use IN or multiple OR conditions.Step 2: Compare options
IN ('Pending', 'Processing') is correct and concise. OR requires full conditions like Status = 'Pending' OR Status = 'Processing'. A lone string after OR like 'Processing' makes the condition always true, selecting extra rows. AND between values or commas cause syntax errors.Final Answer:
SELECT * FROM Orders WHERE Status IN ('Pending', 'Processing'); -> Option AQuick Check:
Use IN for multiple values in WHERE [OK]
- Using AND instead of OR
- Incorrect syntax with commas in WHERE
- Not using quotes around string values
