0
0
MySQLquery~20 mins

Unique indexes in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Unique Index Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this query with a unique index?
Consider a table users with columns id (primary key) and email. A unique index is created on email. What happens when you run this insert query?
MySQL
INSERT INTO users (id, email) VALUES (1, 'alice@example.com'), (2, 'bob@example.com'), (3, 'alice@example.com');
AThe query inserts all three rows successfully.
BThe query fails immediately without inserting any rows because of duplicate email.
CThe query inserts the first two rows and fails on the third due to duplicate email.
DThe query inserts only the first row and skips the rest silently.
Attempts:
2 left
💡 Hint
Think about how unique indexes enforce uniqueness during batch inserts.
🧠 Conceptual
intermediate
1:30remaining
Why use a unique index instead of a primary key?
Which of the following is a valid reason to create a unique index on a column instead of making it a primary key?
ABecause a table can have multiple unique indexes but only one primary key.
BBecause unique indexes allow NULL values but primary keys do not.
CBecause unique indexes automatically create foreign key relationships.
DBecause primary keys do not enforce uniqueness.
Attempts:
2 left
💡 Hint
Think about the number of primary keys allowed per table.
📝 Syntax
advanced
1:30remaining
Which statement correctly creates a unique index?
Choose the correct SQL statement to create a unique index named idx_unique_username on the username column of the accounts table.
ACREATE INDEX UNIQUE idx_unique_username ON accounts (username);
BCREATE UNIQUE KEY idx_unique_username ON accounts (username);
CCREATE UNIQUE INDEX idx_unique_username ON accounts (username);
DCREATE UNIQUE CONSTRAINT idx_unique_username ON accounts (username);
Attempts:
2 left
💡 Hint
Check the correct order of keywords in MySQL syntax for unique indexes.
optimization
advanced
1:30remaining
How does a unique index improve query performance?
Which of the following best explains how a unique index can improve query speed?
AIt automatically caches all rows with unique values in memory for faster access.
BIt compresses the data in the column to reduce disk space and speed up queries.
CIt disables locking on the table, allowing faster concurrent reads.
DIt allows the database to quickly locate rows because it knows values are unique and can stop searching early.
Attempts:
2 left
💡 Hint
Think about how uniqueness helps the database find data faster.
🔧 Debug
expert
2:30remaining
Why does this unique index creation fail?
Given the table employees with existing data, the following command fails. Why? CREATE UNIQUE INDEX idx_unique_email ON employees (email);
MySQL
Table employees has duplicate emails in the email column.
ABecause the email column contains duplicate values violating the unique index constraint.
BBecause the syntax for creating a unique index is incorrect.
CBecause the employees table already has a primary key.
DBecause unique indexes cannot be created on columns with NULL values.
Attempts:
2 left
💡 Hint
Think about what data must look like before creating a unique index.