Bird
Raised Fist0
MySQLquery~20 mins

Query optimization techniques in MySQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Query Optimization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Index Usage in Query Optimization
Which of the following statements best explains how indexes improve query performance in MySQL?
AIndexes create a quick lookup structure that helps MySQL find rows without scanning the whole table.
BIndexes store a sorted copy of the entire table data, allowing faster full table scans.
CIndexes duplicate all columns of a table to speed up data retrieval.
DIndexes compress the data to reduce disk space, which indirectly speeds up queries.
Attempts:
2 left
💡 Hint
Think about how you find a word quickly in a dictionary without reading every page.
❓ query_result
intermediate
1:30remaining
Effect of WHERE Clause on Query Execution
Given the table employees(id, name, department, salary) with an index on department, what will be the output count of this query?

SELECT COUNT(*) FROM employees WHERE department = 'Sales';

Assume there are 1000 rows total, and 200 belong to 'Sales'.
MySQL
SELECT COUNT(*) FROM employees WHERE department = 'Sales';
A1000
B0
CSyntax error
D200
Attempts:
2 left
💡 Hint
The WHERE clause filters rows matching the condition.
📝 Syntax
advanced
1:30remaining
Identifying the Correct Query to Use EXPLAIN
Which of the following queries correctly uses EXPLAIN to analyze the query plan for selecting all columns from orders where status is 'pending'?
ASELECT EXPLAIN * FROM orders WHERE status = 'pending';
BEXPLAIN SELECT * FROM orders WHERE status = 'pending';
CEXPLAIN WHERE status = 'pending' SELECT * FROM orders;
DSELECT * FROM orders EXPLAIN WHERE status = 'pending';
Attempts:
2 left
💡 Hint
EXPLAIN is placed before the SELECT statement.
❓ optimization
advanced
2:00remaining
Choosing the Best Query for Performance
Which query is generally faster when retrieving the top 5 highest salaries from employees table with an index on salary?
ASELECT * FROM employees ORDER BY salary ASC LIMIT 5;
BSELECT * FROM employees WHERE salary IN (SELECT DISTINCT salary FROM employees ORDER BY salary DESC LIMIT 5);
CSELECT * FROM employees ORDER BY salary DESC LIMIT 5;
DSELECT * FROM employees WHERE salary >= ALL (SELECT salary FROM employees);
Attempts:
2 left
💡 Hint
Think about how indexes help with sorting and limiting results.
🔧 Debug
expert
2:30remaining
Diagnosing a Slow Query with JOINs
You have two tables: customers(id, name) and orders(id, customer_id, amount). The query below runs very slowly:

SELECT customers.name, orders.amount FROM customers JOIN orders ON customers.id = orders.customer_id WHERE orders.amount > 1000;

What is the most likely cause of the slow performance?
AMissing index on <code>orders.customer_id</code> causing full table scan on orders.
BUsing JOIN instead of UNION causes slowness.
CThe WHERE clause should be on customers table, not orders.
DSelecting columns instead of using SELECT * causes slow query.
Attempts:
2 left
💡 Hint
Indexes on join columns help speed up matching rows.

Practice

(1/5)
1. What is the main purpose of using indexes in MySQL query optimization?
easy
A. To encrypt the data for security
B. To speed up data retrieval by reducing the search area
C. To store backup copies of data
D. To increase the size of the database

Solution

  1. Step 1: Understand what indexes do

    Indexes help the database find rows faster by creating a quick lookup structure.
  2. Step 2: Relate indexes to query speed

    By reducing the number of rows scanned, indexes speed up data retrieval significantly.
  3. Final Answer:

    To speed up data retrieval by reducing the search area -> Option B
  4. Quick Check:

    Indexes improve search speed = D [OK]
Hint: Indexes speed up searches by narrowing data to scan [OK]
Common Mistakes:
  • Thinking indexes increase database size only
  • Confusing indexes with backups
  • Believing indexes encrypt data
2. Which of the following is the correct syntax to check the query execution plan in MySQL?
easy
A. EXPLAIN SELECT * FROM users;
B. PLAN SELECT * FROM users;
C. SHOW PLAN SELECT * FROM users;
D. DESCRIBE PLAN SELECT * FROM users;

Solution

  1. Step 1: Recall the command to view query plans

    MySQL uses the EXPLAIN keyword before a SELECT query to show the execution plan.
  2. Step 2: Identify the correct syntax

    Only EXPLAIN SELECT * FROM users; is valid syntax to check the plan.
  3. Final Answer:

    EXPLAIN SELECT * FROM users; -> Option A
  4. Quick Check:

    EXPLAIN shows query plan = C [OK]
Hint: Use EXPLAIN before SELECT to see query plan [OK]
Common Mistakes:
  • Using SHOW PLAN which is invalid in MySQL
  • Forgetting EXPLAIN keyword
  • Confusing DESCRIBE with EXPLAIN
3. Given the table employees(id INT PRIMARY KEY, name VARCHAR(50), department_id INT) with an index on department_id, what will be the effect of this query?
SELECT * FROM employees WHERE department_id = 5;
medium
A. The query will scan the entire table without using the index
B. The query will return no rows because indexes filter data
C. The query will fail due to missing index
D. The query will use the index to quickly find rows with department_id = 5

Solution

  1. Step 1: Understand index usage on department_id

    Since there is an index on department_id, MySQL can use it to locate matching rows efficiently.
  2. Step 2: Analyze query behavior

    The WHERE clause filters by department_id = 5, so the index helps avoid scanning all rows.
  3. Final Answer:

    The query will use the index to quickly find rows with department_id = 5 -> Option D
  4. Quick Check:

    Index on filter column speeds query = B [OK]
Hint: Indexed columns in WHERE speed up searches [OK]
Common Mistakes:
  • Assuming index is ignored automatically
  • Thinking query fails without index
  • Believing indexes filter out rows
4. You wrote this query:
SELECT * FROM orders WHERE customer_id = 10 AND status = 'shipped';
You notice it runs slowly. Which fix will most likely improve performance?
medium
A. Add ORDER BY customer_id
B. Remove the WHERE clause
C. Add an index on (customer_id, status)
D. Use SELECT COUNT(*) instead of SELECT *

Solution

  1. Step 1: Identify slow query cause

    The query filters on two columns but may lack a composite index to speed filtering.
  2. Step 2: Apply composite index for optimization

    Adding an index on (customer_id, status) helps MySQL quickly find matching rows using both filters.
  3. Final Answer:

    Add an index on (customer_id, status) -> Option C
  4. Quick Check:

    Composite index speeds multi-column filters = A [OK]
Hint: Use composite index for multiple WHERE columns [OK]
Common Mistakes:
  • Removing WHERE clause loses filtering
  • ORDER BY does not speed filtering
  • Changing SELECT * to COUNT(*) changes result, not speed
5. You have a large table sales(date DATE, product_id INT, amount DECIMAL) without indexes. You want to find total sales for product 101 in 2023 quickly. Which optimization technique is best?
hard
A. Create an index on (product_id, date) and use a query filtering both
B. Add a full table scan hint to force scanning all rows
C. Use SELECT * without WHERE to get all data
D. Drop the table and recreate it

Solution

  1. Step 1: Identify query needs

    The query filters by product_id and date range, so indexing these columns helps speed filtering.
  2. Step 2: Apply composite index for efficient filtering

    Creating an index on (product_id, date) allows MySQL to quickly find relevant rows without scanning the whole table.
  3. Final Answer:

    Create an index on (product_id, date) and use a query filtering both -> Option A
  4. Quick Check:

    Composite index on filter columns = A [OK]
Hint: Index columns used in WHERE for faster queries [OK]
Common Mistakes:
  • Forcing full table scan slows query
  • Selecting all rows wastes time
  • Dropping table loses data
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard