0
0
SQLquery~20 mins

UNIQUE constraint in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
UNIQUE Constraint Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the result of inserting duplicate values in a UNIQUE column?

Consider a table Users with a UNIQUE constraint on the email column. What happens if you try to insert two rows with the same email?

SQL
CREATE TABLE Users (
  id INT PRIMARY KEY,
  email VARCHAR(255) UNIQUE
);

INSERT INTO Users (id, email) VALUES (1, 'user@example.com');
INSERT INTO Users (id, email) VALUES (2, 'user@example.com');
AThe second insert fails with a UNIQUE constraint violation error.
BThe database ignores the second insert silently.
CThe second insert overwrites the first row.
DBoth inserts succeed, allowing duplicate emails.
Attempts:
2 left
💡 Hint

Think about what UNIQUE means for a column in a database table.

🧠 Conceptual
intermediate
1:30remaining
Which statement about UNIQUE constraints is true?

Choose the correct statement about UNIQUE constraints in SQL.

AA UNIQUE constraint allows duplicate values but no NULLs.
BA UNIQUE constraint automatically creates a primary key.
CA UNIQUE constraint allows multiple NULL values in the column.
DA UNIQUE constraint can only be applied to numeric columns.
Attempts:
2 left
💡 Hint

Think about how NULL values are treated in UNIQUE columns.

📝 Syntax
advanced
2:00remaining
Identify the correct syntax to add a UNIQUE constraint to an existing table column

You have a table Employees with a column phone_number. Which SQL statement correctly adds a UNIQUE constraint to this column?

AALTER TABLE Employees ADD UNIQUE (phone_number);
BALTER TABLE Employees ADD CONSTRAINT UNIQUE phone_number;
CALTER TABLE Employees MODIFY phone_number UNIQUE;
DALTER TABLE Employees ADD CONSTRAINT unique_phone UNIQUE (phone_number);
Attempts:
2 left
💡 Hint

Adding a named UNIQUE constraint requires a constraint name and column list.

optimization
advanced
1:30remaining
How does a UNIQUE constraint affect query performance?

Which of the following is a true effect of adding a UNIQUE constraint on a column in terms of query performance?

AIt slows down all queries because of extra checks.
BIt creates an index that can speed up searches on that column.
CIt disables indexing on that column.
DIt has no effect on query performance.
Attempts:
2 left
💡 Hint

Think about how databases enforce uniqueness internally.

🔧 Debug
expert
2:30remaining
Why does this UNIQUE constraint fail on multiple NULLs in this database?

In this database, a UNIQUE constraint on column code causes an error when inserting multiple rows with NULL in code. Why?

CREATE TABLE Products (
  id INT PRIMARY KEY,
  code VARCHAR(10) UNIQUE
);

INSERT INTO Products (id, code) VALUES (1, NULL);
INSERT INTO Products (id, code) VALUES (2, NULL);
AThe database treats NULLs as equal, so duplicates violate UNIQUE.
BThe UNIQUE constraint is not enforced on NULL values.
CThe table lacks a primary key, causing the error.
DThe code column must be NOT NULL to have a UNIQUE constraint.
Attempts:
2 left
💡 Hint

Some databases handle NULLs differently in UNIQUE constraints.