Selecting specific columns in MySQL - Time & Space Complexity
When we ask for specific columns in a database, we want to know how the work grows as the table gets bigger.
We want to see how the time to get results changes when we select only some columns instead of all.
Analyze the time complexity of the following code snippet.
SELECT name, email
FROM users
WHERE active = 1;
This query gets only the name and email columns from all active users in the users table.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning each row in the users table to check if active = 1.
- How many times: Once for each row in the table.
As the number of rows grows, the database checks more rows to find active users.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 row checks |
| 100 | About 100 row checks |
| 1000 | About 1000 row checks |
Pattern observation: The work grows directly with the number of rows in the table.
Time Complexity: O(n)
This means the time to run the query grows in a straight line as the table gets bigger.
[X] Wrong: "Selecting fewer columns makes the query run much faster in terms of time complexity."
[OK] Correct: The main time cost is checking each row, not the number of columns selected. Selecting fewer columns helps with data size but does not change how many rows are checked.
Understanding how selecting columns affects query time helps you explain database efficiency clearly and confidently.
"What if we add an index on the active column? How would the time complexity change?"
Practice
SELECT name, age FROM users; do?Solution
Step 1: Understand the SELECT statement
The SELECT keyword is used to choose specific columns from a table.Step 2: Identify the columns listed
The columns listed after SELECT arenameandage, so only these will be retrieved.Final Answer:
It retrieves only the name and age columns from the users table. -> Option DQuick Check:
Select specific columns = name, age [OK]
- Thinking SELECT * returns specific columns
- Confusing SELECT with DELETE or UPDATE
- Forgetting commas between column names
id and email from a table named customers?Solution
Step 1: Recall correct SELECT syntax
Columns must be separated by commas without parentheses.Step 2: Check each option
SELECT id, email FROM customers; uses commas correctly:SELECT id, email FROM customers;.Final Answer:
SELECT id, email FROM customers; -> Option BQuick Check:
Columns separated by commas = SELECT id, email FROM customers; [OK]
- Omitting commas between columns
- Using parentheses around columns
- Misusing WHERE clause for column selection
products with columns product_id, name, and price, what will be the output of this query?SELECT name, price FROM products WHERE price > 100;
Solution
Step 1: Analyze the SELECT clause
The query selects onlynameandpricecolumns.Step 2: Analyze the WHERE clause
It filters rows whereprice > 100.Final Answer:
Only the name and price of products with price greater than 100. -> Option AQuick Check:
Select specific columns with condition = Only the name and price of products with price greater than 100. [OK]
- Assuming all columns are returned
- Confusing WHERE with column selection
- Thinking missing commas cause error here
SELECT id name FROM employees;
Solution
Step 1: Check column list syntax
Columns must be separated by commas. Here,idandnameare missing a comma.Step 2: Verify other parts
Table nameemployeesand SELECT keyword are correct.Final Answer:
Missing comma between column names. -> Option CQuick Check:
Comma missing between columns = Missing comma between column names. [OK]
- Forgetting commas between columns
- Assuming space works as separator
- Misreading error as table or keyword issue
orders with columns order_id, customer_id, order_date, and total. You want to get only the order_id and total for orders placed in 2023. Which query is correct?Solution
Step 1: Select specific columns
The query must selectorder_idandtotalonly, soSELECT order_id, totalis correct.Step 2: Filter orders from 2023
UsingWHERE YEAR(order_date) = 2023correctly filters orders placed in 2023.Step 3: Check other options
SELECT order_id total FROM orders WHERE order_date = 2023; misses comma and uses wrong date filter. SELECT order_id, total FROM orders WHERE order_date LIKE '2023%'; uses LIKE which may work but is less precise. SELECT * FROM orders WHERE order_date = 2023; selects all columns, not specific ones.Final Answer:
SELECT order_id, total FROM orders WHERE YEAR(order_date) = 2023; -> Option AQuick Check:
Select columns + correct year filter = SELECT order_id, total FROM orders WHERE YEAR(order_date) = 2023; [OK]
- Omitting commas between columns
- Using wrong date filter syntax
- Selecting all columns instead of specific ones
Ready to prove it?
Pass the test and this pattern moves unknown
