Why SELECT retrieves data in MySQL - Performance Analysis
When we use SELECT in MySQL, the database finds and returns data from tables.
We want to understand how the time it takes grows as the data grows.
Analyze the time complexity of the following code snippet.
SELECT * FROM employees WHERE department = 'Sales';
This query retrieves all employees who work in the Sales department.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each row in the employees table to see if the department matches 'Sales'.
- How many times: Once for every row in the table (n times).
As the number of employees grows, the database checks more rows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The work grows directly with the number of rows.
Time Complexity: O(n)
This means the time to get results grows in a straight line as the table gets bigger.
[X] Wrong: "SELECT finds data instantly no matter how big the table is."
[OK] Correct: Without special help like indexes, the database must check each row one by one, so bigger tables take more time.
Understanding how SELECT works helps you explain how databases handle data and why indexes matter, a useful skill in many jobs.
"What if we add an index on the department column? How would the time complexity change?"
Practice
SELECT statement in MySQL?Solution
Step 1: Understand the role of SELECT
The SELECT statement is designed to fetch or retrieve data stored in tables.Step 2: Compare with other SQL commands
DELETE removes data, UPDATE changes data, and CREATE TABLE makes new tables, so these are not for retrieving data.Final Answer:
To retrieve data from one or more tables -> Option AQuick Check:
SELECT = retrieve data [OK]
- Confusing SELECT with DELETE or UPDATE
- Thinking SELECT creates tables
- Assuming SELECT modifies data
employees?Solution
Step 1: Recall correct SELECT syntax
The correct syntax to select all columns isSELECT * FROM table_name;.Step 2: Check each option
SELECT * FROM employees;matches the correct syntax. The other options are invalid: table name before *, using ALL incorrectly, and using GET instead of SELECT.Final Answer:
SELECT * FROM employees; -> Option CQuick Check:
SELECT * FROM table = correct syntax [OK]
- Using GET instead of SELECT
- Placing table name before *
- Using ALL keyword incorrectly
products with columns id, name, and price, what will this query return?SELECT name FROM products WHERE price > 100;
Solution
Step 1: Analyze the SELECT clause
The query selects only thenamecolumn from theproductstable.Step 2: Understand the WHERE condition
The conditionprice > 100filters rows where price is greater than 100.Final Answer:
All product names with price greater than 100 -> Option BQuick Check:
SELECT name WHERE price > 100 = product names over 100 [OK]
- Confusing greater than with less than
- Expecting all columns instead of just name
- Thinking the query causes an error
SELECT FROM customers WHERE age > 30;
Solution
Step 1: Check SELECT syntax
SELECT must be followed by column names or * to specify what to retrieve.Step 2: Review the query structure
The query has SELECT but no columns listed before FROM, which is invalid syntax.Final Answer:
Missing column names after SELECT -> Option AQuick Check:
SELECT needs columns or * [OK]
- Leaving SELECT empty
- Placing table name after WHERE
- Assuming WHERE syntax is wrong
email addresses of customers who live in 'New York' from the customers table. Which query correctly retrieves this data?Solution
Step 1: Identify correct SELECT and WHERE syntax
The correct syntax isSELECT column FROM table WHERE condition;. The city value must be in quotes.Step 2: Evaluate each option
SELECT email FROM customers WHERE city = 'New York'; uses correct syntax and quotes around 'New York'. SELECT * FROM customers WHERE city = New York; misses quotes, causing error. SELECT email WHERE city = 'New York' FROM customers; has wrong clause order. SELECT email FROM customers WHERE city == 'New York'; uses double equals which is invalid in SQL.Final Answer:
SELECT email FROM customers WHERE city = 'New York'; -> Option DQuick Check:
Correct syntax with quotes and order [OK]
- Omitting quotes around string values
- Using == instead of = in WHERE
- Placing WHERE before FROM
Ready to prove it?
Pass the test and this pattern moves unknown
