Bird
Raised Fist0
SQLquery~20 mins

Why indexes matter in SQL - Challenge Your Understanding

Choose your learning style10 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 Speed

Consider a table Employees with 1 million rows. It has a column LastName without an index. You run this query:

SELECT * FROM Employees WHERE LastName = 'Smith';

Now, an index is added on LastName. What is the most likely effect on the query execution?

AThe query will run faster because the index allows quick lookup of 'Smith' rows.
BThe query result will change and return fewer rows.
CThe query will run slower because the index adds overhead to the search.
DThe query will fail because indexes prevent filtering on columns.
Attempts:
2 left
💡 Hint

Think about how indexes help find data quickly like a book's index helps find pages fast.

🧠 Conceptual
intermediate
2:00remaining
Why Indexes Can Slow Down Inserts

Why can adding indexes to a table slow down inserting new rows?

ABecause indexes cause the database to scan all rows before inserting.
BBecause the database must update the index data structure for each new row inserted.
CBecause indexes lock the entire table during inserts.
DBecause indexes prevent new rows from being added.
Attempts:
2 left
💡 Hint

Think about what happens behind the scenes when you add a new row and the index exists.

📝 Syntax
advanced
2:00remaining
Creating an Index Syntax

Which SQL statement correctly creates an index named idx_lastname on the LastName column of the Employees table?

AINDEX CREATE idx_lastname ON Employees LastName;
BCREATE Employees INDEX idx_lastname ON LastName;
CCREATE INDEX idx_lastname ON Employees (LastName);
DCREATE INDEX ON Employees (LastName) idx_lastname;
Attempts:
2 left
💡 Hint

Remember the order: CREATE INDEX <name> ON <table> (<column>);

❓ query_result
advanced
2:00remaining
Index Usage in Query Plans

You have a table Orders with an index on OrderDate. You run this query:

EXPLAIN SELECT * FROM Orders WHERE OrderDate = '2023-01-01';

What is the expected behavior in the query plan?

AThe plan will show an index scan on OrderDate, using the index to find matching rows.
BThe plan will show a full table scan ignoring the index.
CThe plan will show a join operation instead of a scan.
DThe plan will show an error because EXPLAIN cannot be used with indexes.
Attempts:
2 left
💡 Hint

EXPLAIN shows how the database plans to execute the query, including index usage.

🔧 Debug
expert
3:00remaining
Diagnosing Slow Query Despite Index

You created an index on the Email column of the Users table. But this query is still slow:

SELECT * FROM Users WHERE Email LIKE '%@example.com';

Why is the index not helping?

ABecause indexes only work with numeric columns.
BBecause the index was created on the wrong column.
CBecause the query syntax is invalid and causes a full scan.
DBecause the LIKE pattern starts with a wildcard '%', the index cannot be used efficiently.
Attempts:
2 left
💡 Hint

Think about how indexes work with string patterns and wildcards.

Practice

(1/5)
1. Why do indexes matter in a database?
easy
A. They increase the size of the database without any benefit.
B. They automatically fix data errors in tables.
C. They slow down all queries regardless of type.
D. They make searching data faster by creating a quick lookup structure.

Solution

  1. Step 1: Understand what indexes do

    Indexes create a special structure that helps the database find data quickly, like a book's index.
  2. Step 2: Identify the main benefit

    This structure speeds up searching and filtering operations, making queries faster.
  3. Final Answer:

    They make searching data faster by creating a quick lookup structure. -> Option D
  4. Quick Check:

    Indexes improve search speed = B [OK]
Hint: Indexes speed up searches by acting like a book's index [OK]
Common Mistakes:
  • Thinking indexes slow down all queries
  • Believing indexes fix data errors
  • Assuming indexes only increase database size
2. Which SQL statement correctly creates an index on the column email in the users table?
easy
A. CREATE INDEX users ON idx_email(email);
B. MAKE INDEX idx_email ON users(email);
C. CREATE INDEX idx_email ON users(email);
D. INDEX CREATE idx_email ON users(email);

Solution

  1. Step 1: Recall correct SQL syntax for creating an index

    The correct syntax is CREATE INDEX index_name ON table_name(column_name);.
  2. Step 2: Match the syntax with options

    CREATE INDEX idx_email ON users(email); matches the correct syntax exactly, others have wrong keywords or order.
  3. Final Answer:

    CREATE INDEX idx_email ON users(email); -> Option C
  4. Quick Check:

    Correct CREATE INDEX syntax = C [OK]
Hint: Remember: CREATE INDEX index_name ON table(column) [OK]
Common Mistakes:
  • Using wrong keywords like MAKE or INDEX CREATE
  • Swapping table and index names
  • Missing ON keyword
3. Given a table orders with 1 million rows and an index on customer_id, what is the expected result of this query?
SELECT * FROM orders WHERE customer_id = 123;
medium
A. The query will use the index to quickly find matching rows.
B. The query will return no rows because indexes hide data.
C. The query will fail because indexes cannot be used in WHERE clauses.
D. The query will scan all 1 million rows, taking a long time.

Solution

  1. Step 1: Understand index usage in WHERE clause

    An index on customer_id helps the database quickly locate rows matching that value.
  2. Step 2: Predict query behavior

    The database uses the index to avoid scanning all rows, speeding up the query.
  3. Final Answer:

    The query will use the index to quickly find matching rows. -> Option A
  4. Quick Check:

    Index speeds WHERE search = D [OK]
Hint: Indexes speed up WHERE filters on indexed columns [OK]
Common Mistakes:
  • Thinking indexes cause full table scans
  • Believing indexes prevent queries from running
  • Assuming indexes hide data from results
4. You created an index on the username column, but your query is still slow:
SELECT * FROM users WHERE username LIKE '%admin%';

What is the most likely reason?
medium
A. The index was created on the wrong table.
B. Indexes cannot speed up LIKE queries with a leading wildcard '%'.
C. The query syntax is incorrect and causes errors.
D. Indexes always slow down SELECT queries.

Solution

  1. Step 1: Analyze the LIKE pattern

    A LIKE pattern starting with '%' means the search can match anywhere in the string, preventing index use.
  2. Step 2: Understand index limitations

    Indexes work best when the search starts from the beginning of the string, so leading wildcards disable index usage.
  3. Final Answer:

    Indexes cannot speed up LIKE queries with a leading wildcard '%'. -> Option B
  4. Quick Check:

    Leading '%' disables index use = A [OK]
Hint: Leading '%' in LIKE disables index use [OK]
Common Mistakes:
  • Assuming all LIKE queries use indexes
  • Thinking index on wrong table causes this
  • Believing indexes always slow SELECT
5. You have a table sales with columns sale_id, product_id, and sale_date. You often run this query:
SELECT * FROM sales WHERE product_id = 101 AND sale_date >= '2024-01-01';

Which indexing strategy will likely improve query speed the most?
hard
A. Create a composite index on (product_id, sale_date).
B. Create separate indexes on product_id and sale_date.
C. Create a single index on sale_date only.
D. Do not create any index to keep inserts fast.

Solution

  1. Step 1: Understand query filters

    The query filters by product_id and a range on sale_date.
  2. Step 2: Choose best index type

    A composite index on both columns helps the database quickly find rows matching both conditions together.
  3. Step 3: Compare with other options

    Separate indexes may help but are less efficient than a composite index for combined filters.
  4. Final Answer:

    Create a composite index on (product_id, sale_date). -> Option A
  5. Quick Check:

    Composite index matches multi-column filter = A [OK]
Hint: Use composite index for multi-column WHERE filters [OK]
Common Mistakes:
  • Creating index on only one column
  • Using separate indexes instead of composite
  • Avoiding indexes to speed inserts without reason
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