0
0
SQLquery~10 mins

Foreign key linking mental model in SQL - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a foreign key linking the orders table to the customers table.

SQL
ALTER TABLE orders ADD CONSTRAINT fk_customer_id FOREIGN KEY ([1]) REFERENCES customers(id);
Drag options to blanks, or click blank then click option'
Acustomer_id
Border_id
Cid
Dcustomer_name
Attempts:
3 left
💡 Hint
Common Mistakes
Using the primary key column id of the orders table instead of the foreign key column.
Using a column that does not exist in the orders table.
2fill in blank
medium

Complete the code to select all orders with customer names by joining orders and customers using the foreign key.

SQL
SELECT orders.id, customers.name FROM orders JOIN customers ON orders.[1] = customers.id;
Drag options to blanks, or click blank then click option'
Aid
Bname
Ccustomer_id
Dorder_id
Attempts:
3 left
💡 Hint
Common Mistakes
Joining on the wrong columns like orders.id = customers.id.
Using a column that does not exist in orders.
3fill in blank
hard

Fix the error in the foreign key constraint by completing the code correctly.

SQL
ALTER TABLE orders ADD CONSTRAINT fk_customer FOREIGN KEY ([1]) REFERENCES customers([2]);
Drag options to blanks, or click blank then click option'
Acustomer_id
Border_id
Cid
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Referencing a non-primary key column in customers.
Using the wrong column name in orders.
4fill in blank
hard

Fill both blanks to create a table payments with a foreign key linking to orders.

SQL
CREATE TABLE payments (id INT PRIMARY KEY, order_id INT, FOREIGN KEY ([1]) REFERENCES orders([2]));
Drag options to blanks, or click blank then click option'
Aorder_id
Bid
Dpayment_id
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong column name for the foreign key.
Referencing a non-primary key column in orders.
5fill in blank
hard

Fill all three blanks to select payment ids and customer names by joining payments, orders, and customers using foreign keys.

SQL
SELECT payments.id, customers.name FROM payments JOIN orders ON payments.[1] = orders.[2] JOIN customers ON orders.[3] = customers.id;
Drag options to blanks, or click blank then click option'
Aorder_id
Bid
Ccustomer_id
Dpayment_id
Attempts:
3 left
💡 Hint
Common Mistakes
Joining on wrong columns like payments.id or orders.customer_id incorrectly.
Mixing up foreign key and primary key columns.