0
0
SQLquery~10 mins

Joining on primary key to foreign key 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 select all columns from both tables where the primary key matches the foreign key.

SQL
SELECT * FROM orders JOIN customers ON orders.customer_id = customers.[1];
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 foreign key column name instead of the primary key column name in the join condition.
Joining on columns that do not relate to each other.
2fill in blank
medium

Complete the code to join employees and departments on the foreign key department_id.

SQL
SELECT employees.name, departments.name FROM employees JOIN departments ON employees.[1] = departments.id;
Drag options to blanks, or click blank then click option'
Adept_id
Bid
Cname
Ddepartment_id
Attempts:
3 left
💡 Hint
Common Mistakes
Using the primary key column name from employees instead of the foreign key.
Mixing up column names between tables.
3fill in blank
hard

Fix the error in the join condition to correctly join sales and products.

SQL
SELECT * FROM sales JOIN products ON sales.product_id = products.[1];
Drag options to blanks, or click blank then click option'
Aid
Bsales_id
Cprice
Dproduct_name
Attempts:
3 left
💡 Hint
Common Mistakes
Joining on non-key columns like product_name.
Using the wrong table's column in the join condition.
4fill in blank
hard

Fill both blanks to join invoices and clients on the correct keys.

SQL
SELECT invoices.id, clients.name FROM invoices JOIN clients ON invoices.[1] = clients.[2];
Drag options to blanks, or click blank then click option'
Aclient_id
Binvoice_id
Cid
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the foreign key and primary key columns.
Joining on non-key columns like name.
5fill in blank
hard

Fill all three blanks to join payments and orders and select payment amount and order date.

SQL
SELECT payments.amount, orders.[1] FROM payments JOIN orders ON payments.[2] = orders.[3];
Drag options to blanks, or click blank then click option'
Aorder_date
Border_id
Cid
Damount
Attempts:
3 left
💡 Hint
Common Mistakes
Selecting the wrong column from orders.
Mixing up foreign key and primary key columns in the join.