0
0
MySQLquery~20 mins

Index selection strategy in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Index Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Effect of Index on Query Result

Consider a table employees with columns id, name, and department_id. An index exists on department_id. What will be the output of the following query?

SELECT department_id, COUNT(*) AS total FROM employees GROUP BY department_id;

Assume the table has 3 departments with 5, 7, and 8 employees respectively.

MySQL
SELECT department_id, COUNT(*) AS total FROM employees GROUP BY department_id;
A[{"department_id": 1, "total": 5}, {"department_id": 2, "total": 7}, {"department_id": 3, "total": 8}]
B[{"department_id": 1, "total": 7}, {"department_id": 2, "total": 5}, {"department_id": 3, "total": 8}]
C[{"department_id": 1, "total": 8}, {"department_id": 2, "total": 7}, {"department_id": 3, "total": 5}]
D[{"department_id": 1, "total": 20}]
Attempts:
2 left
💡 Hint

Grouping counts employees per department. The index helps speed but does not change counts.

🧠 Conceptual
intermediate
2:00remaining
Choosing Index for Query Optimization

You have a table orders with columns order_id, customer_id, order_date, and status. Which index would best optimize this query?

SELECT * FROM orders WHERE customer_id = 123 AND status = 'shipped';
AAn index on (customer_id, status)
BNo index needed
CAn index on (status, order_date)
DAn index on (order_date)
Attempts:
2 left
💡 Hint

Think about which columns appear in the WHERE clause together.

📝 Syntax
advanced
2:00remaining
Syntax for Creating a Composite Index

Which of the following is the correct syntax to create a composite index on columns last_name and first_name in MySQL?

ACREATE INDEX idx_name ON employees (last_name) AND (first_name);
BCREATE INDEX idx_name ON employees last_name, first_name;
CCREATE INDEX idx_name (last_name, first_name) ON employees;
DCREATE INDEX idx_name ON employees (last_name, first_name);
Attempts:
2 left
💡 Hint

Remember the order of clauses in the CREATE INDEX statement.

optimization
advanced
2:00remaining
Index Selection for Range Queries

Given a table products with columns product_id, category_id, and price, which index will best optimize this query?

SELECT * FROM products WHERE category_id = 10 AND price BETWEEN 100 AND 200;
AAn index on (price, category_id)
BAn index on (category_id) only
CAn index on (category_id, price)
DAn index on (price) only
Attempts:
2 left
💡 Hint

Consider the order of columns in the index for equality and range conditions.

🔧 Debug
expert
2:00remaining
Why Does This Index Not Improve Query Performance?

A developer created an index on email column of the users table to speed up this query:

SELECT * FROM users WHERE email LIKE '%@example.com';

Why does the index not improve performance?

ABecause the index was created on the wrong column
BBecause the LIKE pattern starts with a wildcard, the index cannot be used efficiently
CBecause the query is missing an ORDER BY clause
DBecause the email column is not unique
Attempts:
2 left
💡 Hint

Think about how indexes work with LIKE patterns.