0
0
SQLquery~20 mins

Primary keys and uniqueness in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Primary Key Mastery
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 on a table with a primary key?

Consider a table Users with columns id (primary key) and name. What will this query return?

SELECT COUNT(*) FROM Users WHERE id IS NULL;
ANumber of rows in Users
BNULL
C0
DError: Cannot check NULL on primary key
Attempts:
2 left
💡 Hint

Primary keys cannot be NULL by definition.

🧠 Conceptual
intermediate
2:00remaining
Which statement about UNIQUE constraints is true?

Choose the correct statement about UNIQUE constraints in SQL.

AA UNIQUE constraint automatically creates a primary key.
BA UNIQUE constraint allows multiple NULL values in the column.
CA UNIQUE constraint prevents any NULL values in the column.
DA UNIQUE constraint allows duplicate values but only one NULL.
Attempts:
2 left
💡 Hint

Think about how NULL values are treated in UNIQUE columns.

📝 Syntax
advanced
2:00remaining
Which option correctly defines a composite primary key?

Given a table Orders with columns order_id and product_id, which SQL statement correctly defines a composite primary key on both columns?

ACREATE TABLE Orders (order_id INT, product_id INT, PRIMARY KEY (order_id, product_id));
BCREATE TABLE Orders (order_id INT, product_id INT, UNIQUE (order_id, product_id));
CCREATE TABLE Orders (order_id INT PRIMARY KEY, product_id INT PRIMARY KEY);
DCREATE TABLE Orders (order_id INT, product_id INT, PRIMARY KEY order_id, product_id);
Attempts:
2 left
💡 Hint

Composite primary keys list columns inside parentheses after PRIMARY KEY.

🔧 Debug
advanced
2:00remaining
Why does this table creation fail?

Consider this SQL statement:

CREATE TABLE Employees (id INT, email VARCHAR(100) UNIQUE, PRIMARY KEY (id, email));

Why does this statement cause an error?

ABecause email cannot be both UNIQUE and part of the PRIMARY KEY.
BBecause PRIMARY KEY cannot include a UNIQUE column.
CBecause a column cannot be declared UNIQUE and also be part of a composite PRIMARY KEY.
DBecause declaring UNIQUE on email is redundant when it is part of the PRIMARY KEY.
Attempts:
2 left
💡 Hint

Think about the uniqueness guarantees of primary keys.

optimization
expert
2:00remaining
Which index is most efficient for enforcing uniqueness on a large table?

You have a large table Products with a column sku that must be unique. Which option is the most efficient way to enforce uniqueness and optimize lookups?

ACreate a PRIMARY KEY on <code>sku</code> if it is the only key.
BCreate a non-unique index on <code>sku</code> and check uniqueness in application code.
CCreate a UNIQUE index on the <code>sku</code> column.
DCreate a UNIQUE constraint without an index.
Attempts:
2 left
💡 Hint

Primary keys automatically create unique indexes and optimize lookups.