0
0
SQLquery~20 mins

How an index works (B-tree mental model) in SQL - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
B-tree Index Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
How does a B-tree index speed up data search?

Imagine a phone book organized as a B-tree index. What is the main reason this structure helps find a name faster than scanning every page?

AIt randomly picks entries to check, hoping to find the name quickly.
BIt stores all data in one big list, so the computer reads everything faster.
CIt divides the data into smaller groups, allowing quick jumps to the right section instead of checking every entry.
DIt duplicates the entire data multiple times to speed up reading.
Attempts:
2 left
💡 Hint

Think about how you find a word in a dictionary without reading every page.

query_result
intermediate
2:00remaining
What rows does this indexed query return?

Given a table Employees with a B-tree index on LastName, what rows will this query return?

SELECT * FROM Employees WHERE LastName = 'Smith';
SQL
CREATE TABLE Employees (ID INT, FirstName VARCHAR(50), LastName VARCHAR(50));
CREATE INDEX idx_lastname ON Employees(LastName);
-- Assume data includes multiple 'Smith' entries
AOnly the first row where LastName is 'Smith', ignoring others.
BAll rows where LastName is exactly 'Smith', found quickly using the index.
CAll rows, because the index is ignored in equality searches.
DNo rows, because indexes do not affect query results.
Attempts:
2 left
💡 Hint

Indexes help find matching rows faster but do not change which rows are returned.

📝 Syntax
advanced
2:00remaining
Which SQL statement correctly creates a B-tree index?

Choose the correct SQL command to create a B-tree index on the email column of the Users table.

ACREATE INDEX idx_email USING BTREE ON Users(email);
BCREATE BTREE INDEX idx_email ON Users(email);
CCREATE INDEX idx_email ON Users USING BTREE(email);
DCREATE INDEX idx_email ON Users(email);
Attempts:
2 left
💡 Hint

Standard SQL uses a simple syntax for creating indexes; specifying B-tree is often default.

optimization
advanced
2:00remaining
Why might a B-tree index be inefficient for a column with many repeated values?

Consider a B-tree index on a column that has only three distinct values repeated many times. Why might this index not improve query speed much?

ABecause the index nodes become large and searching them is slower than scanning the table.
BBecause B-tree indexes only work with unique values and ignore duplicates.
CBecause the database cannot use indexes on columns with repeated values.
DBecause the index will cause the database to scan the entire table anyway.
Attempts:
2 left
💡 Hint

Think about how useful an index is when many rows share the same value.

🔧 Debug
expert
2:00remaining
What error occurs with this incorrect B-tree index creation?

What error will this SQL statement produce?

CREATE INDEX idx_wrong ON Users(email,);
ASyntax error due to trailing comma in column list.
BRuntime error because the index name is invalid.
CNo error; index created successfully.
DError because 'email' column does not exist.
Attempts:
2 left
💡 Hint

Check the syntax carefully, especially commas in lists.