Selection operation in DBMS Theory - Time & Space Complexity
When we perform 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 code snippet.
SELECT * FROM Employees WHERE Department = 'Sales';
This query searches the Employees table to find all rows where the Department is 'Sales'.
Identify the loops, recursion, array traversals that repeat.
- 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 complete the selection grows in a straight line as the table gets bigger.
[X] Wrong: "The database only looks at some rows, so the time stays the same no matter the table size."
[OK] Correct: Without special indexes, the database must check each row to be sure, so time grows with table size.
Understanding how selection time grows helps you explain database performance clearly and shows you know how data size affects queries.
"What if the Department column had an index? How would the time complexity change?"
Practice
SELECT operation with a WHERE clause in a database?Solution
Step 1: Understand the SELECT operation
The SELECT operation retrieves data from a table.Step 2: Role of WHERE clause
The WHERE clause filters rows based on a condition, returning only matching rows.Final Answer:
To filter rows that meet a specific condition -> Option DQuick Check:
Selection with WHERE filters rows [OK]
- Confusing SELECT with INSERT or DELETE
- Thinking WHERE changes table structure
- Assuming SELECT without WHERE deletes data
Employees where the Age is greater than 30?Solution
Step 1: Check SELECT syntax
The correct syntax to select all columns is SELECT * FROM table_name.Step 2: Verify WHERE clause usage
The WHERE clause must follow FROM and include a condition like Age > 30.Final Answer:
SELECT * FROM Employees WHERE Age > 30; -> Option CQuick Check:
Correct SELECT with WHERE syntax [OK]
- Omitting FROM keyword
- Using ALL instead of *
- Placing condition outside WHERE
Students with columns ID, Name, and Score. What will be the result of the query?SELECT Name FROM Students WHERE Score >= 75;
Solution
Step 1: Analyze SELECT clause
The query selects only the Name column from Students.Step 2: Analyze WHERE condition
WHERE Score >= 75 filters students with scores 75 or higher.Final Answer:
Names of students with scores 75 or more -> Option AQuick Check:
WHERE filters Score >= 75, SELECT Name only [OK]
- Thinking it selects all columns
- Confusing >= with >
- Assuming it returns scores too
SELECT * FROM Orders WHERE OrderDate = '2023-13-01';
Solution
Step 1: Check date format in WHERE clause
The date '2023-13-01' has month 13, which is invalid.Step 2: Verify other syntax parts
SELECT * FROM Orders; and semicolon are correct; table name assumed valid.Final Answer:
Invalid date format in WHERE clause -> Option BQuick Check:
Date month must be 1-12 [OK]
- Ignoring invalid month number
- Thinking missing semicolon causes error
- Assuming SELECT * is wrong
Products with columns ProductID, Name, and Price. How would you write a query to select only products priced between 50 and 100 inclusive?Solution
Step 1: Understand inclusive range selection
Inclusive means including 50 and 100, so use >= and <= operators.Step 2: Construct the WHERE clause
Use Price >= 50 AND Price <= 100 to filter the range correctly.Step 3: Compare options
The query with > 50 AND < 100 excludes boundaries; the one with = 50 OR = 100 selects only extremes; the one with >= 50 OR <= 100 selects all rows.Final Answer:
SELECT * FROM Products WHERE Price >= 50 AND Price <= 100; -> Option AQuick Check:
Inclusive range uses >= and <= [OK]
- Using > and < excludes boundary values
- Using OR instead of AND for range
- Using OR with inequalities selects all rows
Ready to prove it?
Pass the test and this pattern moves unknown
