Challenge - 5 Problems
Primary Key Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What is the output of this query?
Given the table users with columns
id INT and name VARCHAR(50), and id declared as the primary key, what will this query return?SELECT COUNT(*) FROM users WHERE id IS NULL;Attempts:
2 left
💡 Hint
Primary keys cannot have NULL values.
✗ Incorrect
Since the primary key column
id cannot contain NULL values, the count of rows where id IS NULL is always zero.📝 Syntax
intermediate2:00remaining
Which option correctly declares a primary key on the
order_id column?You want to create a table
orders with a primary key on order_id. Which of the following CREATE TABLE statements is correct?Attempts:
2 left
💡 Hint
The primary key constraint syntax requires parentheses around the column name when declared separately.
✗ Incorrect
Option B correctly uses
PRIMARY KEY (order_id) syntax to declare the primary key constraint on the column.❓ schema
advanced2:00remaining
Which table schema correctly enforces a composite primary key on
user_id and session_id?You want to ensure that the combination of
user_id and session_id is unique and not null. Which table definition achieves this?Attempts:
2 left
💡 Hint
Composite primary keys require listing all columns inside the PRIMARY KEY parentheses.
✗ Incorrect
Option A correctly declares a composite primary key on both
user_id and session_id, ensuring uniqueness and non-null for the pair.🔧 Debug
advanced2:00remaining
Why does this table creation fail?
Consider this SQL statement:
Why does it cause an error?
CREATE TABLE products (product_id INT, name VARCHAR(100), PRIMARY KEY product_id);Why does it cause an error?
Attempts:
2 left
💡 Hint
Check the syntax for PRIMARY KEY constraints.
✗ Incorrect
The PRIMARY KEY declaration requires parentheses around the column name(s). Missing parentheses cause a syntax error.
❓ optimization
expert3:00remaining
Which primary key choice optimizes query speed for a large table with many lookups by
email?You have a large
customers table. You want to optimize queries that search by email. Which primary key declaration is best?Attempts:
2 left
💡 Hint
Primary keys create clustered indexes that speed up lookups.
✗ Incorrect
Declaring
email as the primary key creates a clustered index on it, optimizing queries searching by email. Other options either index a different column or do not cluster on email.