0
0
SQLquery~20 mins

Unique index behavior in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Unique Index Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Effect of Unique Index on Duplicate Inserts
Given a table users with a unique index on the email column, what happens when you try to insert two rows with the same email?
SQL
CREATE TABLE users (id INT PRIMARY KEY, email VARCHAR(255));
CREATE UNIQUE INDEX idx_email ON users(email);

INSERT INTO users (id, email) VALUES (1, 'alice@example.com');
INSERT INTO users (id, email) VALUES (2, 'alice@example.com');
AThe second insert is ignored silently without error.
BBoth inserts succeed and the table contains two rows with the same email.
CThe second insert overwrites the first row with the same email.
DThe second insert fails with a unique constraint violation error.
Attempts:
2 left
💡 Hint
Think about what a unique index enforces on the column values.
🧠 Conceptual
intermediate
1:30remaining
Unique Index vs Primary Key
Which statement correctly describes the difference between a unique index and a primary key in SQL?
AA primary key uniquely identifies each row and cannot be NULL; a unique index allows NULLs and enforces uniqueness.
BA unique index can only be created on numeric columns; a primary key can be any data type.
CA primary key allows duplicate values; a unique index does not.
DA unique index automatically creates a primary key.
Attempts:
2 left
💡 Hint
Consider NULL values and uniqueness rules.
📝 Syntax
advanced
2:00remaining
Creating a Unique Index on Multiple Columns
Which SQL statement correctly creates a unique index on the combination of first_name and last_name columns in the employees table?
ACREATE UNIQUE INDEX idx_name ON employees(first_name, last_name);
BCREATE UNIQUE KEY idx_name ON employees(first_name, last_name);
CCREATE INDEX UNIQUE idx_name ON employees(first_name, last_name);
DCREATE UNIQUE CONSTRAINT idx_name ON employees(first_name, last_name);
Attempts:
2 left
💡 Hint
Focus on the correct syntax order for creating unique indexes.
optimization
advanced
1:30remaining
Performance Impact of Unique Indexes
How does adding a unique index on a frequently updated column affect database performance?
AIt has no impact on performance since indexes are only used during SELECT queries.
BIt speeds up insert and update operations by automatically sorting the data.
CIt can slow down insert and update operations because the database must check uniqueness on each change.
DIt causes the database to lock the entire table on every update.
Attempts:
2 left
💡 Hint
Think about what the database must do to maintain uniqueness.
🔧 Debug
expert
2:30remaining
Diagnosing Unique Constraint Violation
You have a unique index on the username column. After deleting a row with username 'john', you try to insert a new row with username 'john' but get a unique constraint violation error. What is the most likely cause?
AThe insert statement has a syntax error causing the violation error.
BThe deleted row is still locked or not fully committed, so the unique index still sees the old value.
CUnique indexes do not allow reusing deleted values under any circumstances.
DThe username column is case-sensitive and 'john' differs from the deleted row's username.
Attempts:
2 left
💡 Hint
Consider transaction states and locking behavior.