0
0
MySQLquery~20 mins

Creating indexes in MySQL - Practice Exercises

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

Consider a table users with columns id (primary key), email, and name. A UNIQUE index is created on the email column.

What will happen if you try to insert two rows with the same email value?

MySQL
CREATE TABLE users (id INT PRIMARY KEY, email VARCHAR(255), name VARCHAR(255));
CREATE UNIQUE INDEX idx_email ON users(email);

INSERT INTO users VALUES (1, 'alice@example.com', 'Alice');
INSERT INTO users VALUES (2, 'alice@example.com', 'Alicia');
AThe second insert will fail with a duplicate key error.
BThe second insert will be ignored silently.
CThe second insert will overwrite the first row.
DBoth inserts will succeed, allowing duplicate emails.
Attempts:
2 left
💡 Hint

Think about what a UNIQUE index enforces on the column values.

query_result
intermediate
2:00remaining
Query performance with and without an index

Given a table orders with columns order_id, customer_id, and order_date, you run this query:

SELECT * FROM orders WHERE customer_id = 12345;

Which index will most improve the speed of this query?

ACREATE INDEX idx_order_date ON orders(order_date);
BCREATE UNIQUE INDEX idx_order_id ON orders(order_id);
CCREATE INDEX idx_customer ON orders(customer_id);
DCREATE FULLTEXT INDEX idx_customer ON orders(customer_id);
Attempts:
2 left
💡 Hint

Which column is used in the WHERE clause?

📝 Syntax
advanced
2:00remaining
Correct syntax to create 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 users(last_name first_name);
BCREATE INDEX idx_name ON users(last_name, first_name);
CCREATE INDEX idx_name ON users(last_name AND first_name);
DCREATE INDEX idx_name ON users(last_name + first_name);
Attempts:
2 left
💡 Hint

Think about how multiple columns are listed in an index.

optimization
advanced
2:00remaining
Choosing the best index for a query with multiple conditions

Given a table products with columns category, price, and stock, you run this query:

SELECT * FROM products WHERE category = 'Books' AND price < 20;

Which index will most improve this query's performance?

ACREATE INDEX idx_category_price ON products(category, price);
BCREATE INDEX idx_price_category ON products(price, category);
CCREATE INDEX idx_category ON products(category);
DCREATE INDEX idx_price ON products(price);
Attempts:
2 left
💡 Hint

Consider the order of columns in a composite index and how the query filters.

🔧 Debug
expert
2:00remaining
Why does this index creation fail?

Consider this SQL statement:

CREATE INDEX idx_description ON products(description(10));

It fails with an error. What is the most likely reason?

APrefix length can only be used with UNIQUE indexes.
BThe index name idx_description is already used by another index.
CMySQL requires a prefix length for indexing TEXT or BLOB columns, but the syntax is incorrect here.
DThe column description is not a string type and cannot have a prefix length.
Attempts:
2 left
💡 Hint

Think about indexing long text columns and how prefix lengths work.