SELECT all columns in SQL - Time & Space Complexity
We want to understand how the time to run a query changes when we select all columns from a table.
Specifically, how does the work grow as the table gets bigger?
Analyze the time complexity of the following SQL query.
SELECT *
FROM employees;
This query retrieves every column and every row from the employees table.
Look for repeated actions in the query execution.
- Primary operation: Reading each row from the employees table.
- How many times: Once for every row in the table.
As the number of rows grows, the work grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 reads of all columns |
| 100 | 100 reads of all columns |
| 1000 | 1000 reads of all columns |
Pattern observation: The work increases directly with the number of rows.
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 all columns is slower because of the * symbol itself."
[OK] Correct: The * just means all columns; the real time depends on how many rows and columns there are, not the symbol.
Understanding how query time grows with table size helps you explain database performance clearly and confidently.
"What if we added a WHERE clause to filter rows? How would the time complexity change?"
Practice
SELECT * FROM employees; do?Solution
Step 1: Understand the SELECT * syntax
The asterisk (*) means select all columns from the specified table.Step 2: Apply to the employees table
The query fetches every column and every row from the employees table.Final Answer:
It retrieves all columns and all rows from the employees table. -> Option DQuick Check:
SELECT * = all columns and rows [OK]
- Thinking SELECT * deletes or updates data
- Confusing SELECT * with creating tables
- Assuming it selects only one column
products?Solution
Step 1: Recall the correct SELECT syntax
The correct way to select all columns is using SELECT * FROM table_name.Step 2: Check each option
SELECT * FROM products; matches the correct syntax exactly.Final Answer:
SELECT * FROM products; -> Option AQuick Check:
SELECT * FROM table = correct syntax [OK]
- Using SELECT ALL instead of SELECT *
- Forgetting FROM keyword
- Trying to select 'columns' literally
students with columns id, name, and age, what will the query SELECT * FROM students WHERE age > 20; return?Solution
Step 1: Understand SELECT * with WHERE clause
SELECT * fetches all columns, and WHERE filters rows based on condition.Step 2: Apply condition age > 20
Only rows where age is greater than 20 are returned, with all columns shown.Final Answer:
All columns for students whose age is greater than 20. -> Option CQuick Check:
SELECT * + WHERE filters rows [OK]
- Thinking WHERE limits columns instead of rows
- Assuming * selects only one column
- Believing * cannot be used with WHERE
SELECT * employees;Solution
Step 1: Check SQL SELECT syntax
The correct syntax requires FROM before the table name.Step 2: Identify missing keyword
The query misses FROM between SELECT * and employees.Final Answer:
Missing FROM keyword before table name. -> Option BQuick Check:
SELECT * FROM table is correct syntax [OK]
- Omitting FROM keyword
- Thinking * is invalid
- Adding unnecessary quotes around table name
orders. Which query is best to avoid slowing down your database?Solution
Step 1: Understand SELECT * impact on large tables
SELECT * fetches all rows and columns, which can be slow on big tables.Step 2: Use LIMIT to reduce rows fetched
Adding LIMIT 10 fetches only 10 rows, making the query faster and safer.Final Answer:
SELECT * FROM orders LIMIT 10; -> Option AQuick Check:
LIMIT reduces rows, speeding up SELECT * [OK]
- Running SELECT * without LIMIT on large tables
- Confusing DELETE with SELECT
- Selecting only one column when all data is needed
Ready to prove it?
Pass the test and this pattern moves unknown
