0
0
SQLquery~20 mins

Composite primary keys in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Composite Key Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of a query on a table with composite primary key

Consider a table Orders with a composite primary key on (OrderID, ProductID). The table has the following rows:

OrderID | ProductID | Quantity
--------+-----------+---------
1       | 101       | 2
1       | 102       | 1
2       | 101       | 5
2       | 103       | 3

What will be the output of this query?

SELECT OrderID, ProductID FROM Orders WHERE OrderID = 1;
SQL
SELECT OrderID, ProductID FROM Orders WHERE OrderID = 1;
A[{"OrderID":1,"ProductID":101},{"OrderID":1,"ProductID":102}]
B[{"OrderID":1,"ProductID":101}]
C[{"OrderID":1,"ProductID":102},{"OrderID":2,"ProductID":101}]
D[]
Attempts:
2 left
💡 Hint

Remember that the composite primary key means each combination of OrderID and ProductID is unique, but multiple rows can share the same OrderID.

🧠 Conceptual
intermediate
1:30remaining
Understanding composite primary keys uniqueness

Which statement best describes the uniqueness enforced by a composite primary key on columns (A, B)?

AEach value in column A must be unique across the table.
BEither column A or column B must have unique values, but not necessarily both.
CEach combination of values in columns A and B must be unique across the table.
DEach value in column B must be unique across the table.
Attempts:
2 left
💡 Hint

Think about how composite keys combine columns to enforce uniqueness.

📝 Syntax
advanced
2:00remaining
Correct syntax to define a composite primary key

Which of the following SQL statements correctly defines a composite primary key on columns user_id and role_id in a table UserRoles?

ACREATE TABLE UserRoles (user_id INT PRIMARY KEY, role_id INT PRIMARY KEY);
BCREATE TABLE UserRoles (user_id INT, role_id INT, PRIMARY KEY (user_id, role_id));
CCREATE TABLE UserRoles (user_id INT, role_id INT, PRIMARY KEY user_id, role_id);
DCREATE TABLE UserRoles (user_id INT, role_id INT, PRIMARY KEY user_id AND role_id);
Attempts:
2 left
💡 Hint

Look for the correct syntax to declare multiple columns as a single primary key.

optimization
advanced
2:30remaining
Indexing considerations with composite primary keys

You have a table with a composite primary key on (customer_id, order_id). You frequently query the table filtering only by order_id. What is the best indexing strategy to optimize these queries?

ACreate a composite index on <code>(order_id, customer_id)</code>.
BDrop the primary key and create separate primary keys on each column.
CRely only on the composite primary key index on <code>(customer_id, order_id)</code>.
DCreate an additional index on <code>order_id</code> alone.
Attempts:
2 left
💡 Hint

Think about how indexes work with leading columns in composite keys.

🔧 Debug
expert
2:00remaining
Identify the error in composite primary key constraint

Given the following table creation statement, what error will occur?

CREATE TABLE Enrollment (
  student_id INT,
  course_id INT,
  PRIMARY KEY student_id, course_id
);
ASyntaxError: Missing parentheses around columns in PRIMARY KEY definition.
BNo error; table created successfully with composite primary key.
CTypeError: Columns must be declared as NOT NULL for primary key.
DRuntime error: Duplicate key values not allowed.
Attempts:
2 left
💡 Hint

Check the syntax for declaring composite primary keys.