Bird
Raised Fist0
MySQLquery~20 mins

Composite indexes in MySQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Composite Index Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
Effect of composite index on query filtering

Given a table orders with columns customer_id, order_date, and status, and a composite index on (customer_id, order_date), which query will most efficiently use the index?

ASELECT * FROM orders WHERE order_date = '2024-01-01' AND status = 'shipped';
BSELECT * FROM orders WHERE order_date = '2024-01-01';
CSELECT * FROM orders WHERE status = 'shipped' AND customer_id = 123;
DSELECT * FROM orders WHERE customer_id = 123 AND order_date = '2024-01-01';
Attempts:
2 left
💡 Hint

Think about the order of columns in the composite index and which columns are filtered in the query.

🧠 Conceptual
intermediate
2:00remaining
Understanding leftmost prefix rule in composite indexes

Which of the following statements best describes the leftmost prefix rule for composite indexes?

AA composite index can only be used if the query filters on the first column of the index or a leftmost subset of columns in order.
BA composite index is only used if the query filters on the last column of the index.
CA composite index can be used by queries filtering on any combination of columns in the index, regardless of order.
DA composite index is ignored if the query filters on all columns in the index.
Attempts:
2 left
💡 Hint

Think about how the database uses the index starting from the first column.

📝 Syntax
advanced
2:00remaining
Creating a composite index in MySQL

Which of the following SQL statements correctly creates a composite index named idx_customer_date on the customer_id and order_date columns of the orders table?

ACREATE INDEX idx_customer_date ON orders (order_date, customer_id);
BCREATE COMPOSITE INDEX idx_customer_date ON orders (customer_id, order_date);
CCREATE INDEX idx_customer_date ON orders (customer_id, order_date);
DCREATE INDEX idx_customer_date ON orders customer_id, order_date;
Attempts:
2 left
💡 Hint

Remember the correct syntax for creating indexes in MySQL.

❓ optimization
advanced
2:00remaining
Choosing the best composite index for query performance

You have a table sales with columns region, product_id, and sale_date. You often run this query:

SELECT * FROM sales WHERE product_id = 101 AND region = 'North';

Which composite index will optimize this query best?

ACREATE INDEX idx_region_product ON sales (region, product_id);
BCREATE INDEX idx_product_region ON sales (product_id, region);
CCREATE INDEX idx_sale_date_product ON sales (sale_date, product_id);
DCREATE INDEX idx_region_sale_date ON sales (region, sale_date);
Attempts:
2 left
💡 Hint

Consider which column is filtered first in the query and the order of columns in the index.

🔧 Debug
expert
2:00remaining
Diagnosing why a composite index is not used

You created a composite index on (category, price) in a products table. The query below does not use the index:

SELECT * FROM products WHERE price > 100;

Why is the composite index not used?

ABecause the query filters only on the second column <code>price</code>, not the first column <code>category</code> of the composite index.
BBecause the query uses a greater than operator, which is not supported by composite indexes.
CBecause the index was created with the wrong column order; it should be (price, category).
DBecause composite indexes cannot be used for range queries.
Attempts:
2 left
💡 Hint

Recall the leftmost prefix rule for composite indexes.

Practice

(1/5)
1. What is the main purpose of a composite index in MySQL?
easy
A. To create a backup of the database automatically
B. To speed up queries that filter or sort by multiple columns together
C. To store data in multiple tables simultaneously
D. To encrypt data in multiple columns

Solution

  1. Step 1: Understand what an index does

    An index helps MySQL find data faster by organizing it for quick lookup.
  2. Step 2: Understand composite index purpose

    A composite index uses multiple columns together to speed up queries that filter or sort by those columns combined.
  3. Final Answer:

    To speed up queries that filter or sort by multiple columns together -> Option B
  4. Quick Check:

    Composite index = speed up multi-column queries [OK]
Hint: Composite indexes speed up multi-column filters and sorts [OK]
Common Mistakes:
  • Thinking composite indexes store data
  • Confusing indexes with backups
  • Assuming encryption is related to indexes
2. Which of the following is the correct syntax to create a composite index on columns first_name and last_name in a table users?
easy
A. CREATE INDEX idx_name ON users (first_name, last_name);
B. CREATE INDEX idx_name ON users first_name, last_name;
C. CREATE INDEX idx_name (first_name, last_name) ON users;
D. CREATE INDEX idx_name ON users WHERE first_name, last_name;

Solution

  1. Step 1: Recall correct CREATE INDEX syntax

    The syntax is: CREATE INDEX index_name ON table_name (column1, column2);
  2. Step 2: Match syntax with options

    CREATE INDEX idx_name ON users (first_name, last_name); matches the correct syntax with parentheses around columns and ON keyword before table name.
  3. Final Answer:

    CREATE INDEX idx_name ON users (first_name, last_name); -> Option A
  4. Quick Check:

    CREATE INDEX ... ON table (columns) [OK]
Hint: Use parentheses around columns after ON table_name [OK]
Common Mistakes:
  • Omitting parentheses around columns
  • Placing columns before ON keyword
  • Using WHERE instead of column list
3. Given a composite index on (city, age) in a table people, which query will use the index efficiently?
medium
A. SELECT * FROM people WHERE city = 'Paris' AND age = 30;
B. SELECT * FROM people WHERE age = 30;
C. SELECT * FROM people WHERE age BETWEEN 20 AND 40;
D. SELECT * FROM people WHERE city LIKE '%Paris%' ORDER BY age DESC;

Solution

  1. Step 1: Understand composite index column order

    The index is on (city, age), so queries filtering city first then age use it best.
  2. Step 2: Analyze queries for index usage

    SELECT * FROM people WHERE city LIKE '%Paris%' ORDER BY age DESC;: LIKE '%Paris%' prevents index use due to leading wildcard. SELECT * FROM people WHERE age = 30;: filters only age (second column), skips prefix. SELECT * FROM people WHERE age BETWEEN 20 AND 40;: range on age only, cannot use prefix. SELECT * FROM people WHERE city = 'Paris' AND age = 30;: city then age equality matches prefix perfectly.
  3. Final Answer:

    SELECT * FROM people WHERE city = 'Paris' AND age = 30; -> Option A
  4. Quick Check:

    Filter starts with first index column = SELECT * FROM people WHERE city = 'Paris' AND age = 30; [OK]
Hint: Filter starting with first index column uses composite index best [OK]
Common Mistakes:
  • Assuming any column filter uses composite index fully
  • Ignoring column order in composite index
  • Thinking ORDER BY always uses composite index
4. You created a composite index on (department, salary) but your query SELECT * FROM employees WHERE salary > 50000; is slow. What is the likely problem?
medium
A. The table has no data
B. The index syntax was incorrect
C. Composite indexes cannot be used with numeric columns
D. The query does not filter on the first column of the composite index

Solution

  1. Step 1: Recall composite index usage rules

    Composite indexes work best when queries filter starting from the first indexed column.
  2. Step 2: Analyze the query and index

    The index is on (department, salary), but query filters only salary (second column), so index is not used efficiently, causing slowness.
  3. Final Answer:

    The query does not filter on the first column of the composite index -> Option D
  4. Quick Check:

    Filter must start with first index column [OK]
Hint: Filter must include first index column to use composite index [OK]
Common Mistakes:
  • Assuming any column filter uses composite index
  • Blaming syntax without checking query
  • Thinking numeric columns can't be indexed
5. You have a table orders with columns customer_id, order_date, and status. You often run this query:
SELECT * FROM orders WHERE customer_id = 123 AND status = 'shipped' ORDER BY order_date DESC;
Which composite index will optimize this query best?
hard
A. CREATE INDEX idx_order_date_customer_status ON orders (order_date, customer_id, status);
B. CREATE INDEX idx_status_customer_date ON orders (status, customer_id, order_date);
C. CREATE INDEX idx_customer_status_date ON orders (customer_id, status, order_date);
D. CREATE INDEX idx_customer_order_date ON orders (customer_id, order_date);

Solution

  1. Step 1: Identify query filter and order columns

    The query filters by customer_id and status, then orders by order_date descending.
  2. Step 2: Choose index column order for best performance

    Index should start with columns used in WHERE filters in order: customer_id, then status, then order_date for ORDER BY.
  3. Step 3: Match index options

    CREATE INDEX idx_customer_status_date ON orders (customer_id, status, order_date); matches this order exactly, so it optimizes filtering and ordering best.
  4. Final Answer:

    CREATE INDEX idx_customer_status_date ON orders (customer_id, status, order_date); -> Option C
  5. Quick Check:

    Index columns match WHERE then ORDER BY order [OK]
Hint: Index columns order: filter columns first, then order column [OK]
Common Mistakes:
  • Putting ORDER BY column before filter columns
  • Ignoring filter columns order
  • Creating index missing important filter column
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