0
0
SQLquery~20 mins

ER diagram to table mapping in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ER Diagram Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Mapping a One-to-Many Relationship

Consider an ER diagram with two entities: Author and Book. Each author can write many books, but each book has only one author.

How should this relationship be represented in the relational tables?

AMerge <code>Author</code> and <code>Book</code> into a single table with all attributes.
BAdd a foreign key column <code>book_id</code> in the <code>Author</code> table referencing <code>Book</code>.
CCreate a separate table <code>AuthorBook</code> with foreign keys to both <code>Author</code> and <code>Book</code>.
DAdd a foreign key column <code>author_id</code> in the <code>Book</code> table referencing <code>Author</code>.
Attempts:
2 left
💡 Hint

Think about which entity can have multiple related records.

query_result
intermediate
2:00remaining
Result of Mapping a Many-to-Many Relationship

Given an ER diagram with entities Student and Course having a many-to-many relationship, which table structure correctly represents this in SQL?

ATables: <code>Student</code>, <code>Course</code>, and a join table <code>StudentCourse</code> with foreign keys to both.
BTables: <code>Student</code> with a foreign key to <code>Course</code>.
CTables: <code>Course</code> with a foreign key to <code>Student</code>.
DSingle table combining <code>Student</code> and <code>Course</code> attributes.
Attempts:
2 left
💡 Hint

Many-to-many relationships require an extra table to connect the two entities.

📝 Syntax
advanced
2:30remaining
Identify the Correct SQL Table Creation for a Weak Entity

A weak entity Dependent depends on Employee with a partial key dependent_name. Which SQL table definition correctly represents this weak entity?

SQL
CREATE TABLE Dependent (
  employee_id INT,
  dependent_name VARCHAR(50),
  birth_date DATE,
  PRIMARY KEY (employee_id, dependent_name),
  FOREIGN KEY (employee_id) REFERENCES Employee(employee_id)
);
AUse <code>employee_id</code> as primary key and foreign key without <code>dependent_name</code>.
BUse only <code>dependent_name</code> as primary key without foreign keys.
CUse a composite primary key of <code>employee_id</code> and <code>dependent_name</code> with a foreign key to <code>Employee</code>.
DUse <code>dependent_id</code> as a surrogate primary key without foreign keys.
Attempts:
2 left
💡 Hint

Weak entities need the owner's key plus their partial key as primary key.

optimization
advanced
2:30remaining
Optimizing Table Design for Subclass Entities

An ER diagram has a superclass Vehicle and subclasses Car and Truck. Which table design optimizes storage and query performance?

ACreate separate tables for Vehicle, Car, and Truck with Car and Truck referencing Vehicle by foreign key.
BCreate only Car and Truck tables without a Vehicle table.
CCreate one table for Vehicle with all attributes of Car and Truck, using NULLs where not applicable.
DCreate a single table combining Car and Truck attributes but no Vehicle table.
Attempts:
2 left
💡 Hint

Think about normalization and avoiding NULLs.

🔧 Debug
expert
3:00remaining
Debugging Incorrect Foreign Key Placement

Given an ER diagram with entities Order and Customer where each order belongs to one customer, the following SQL tables are created:

CREATE TABLE Customer (
  customer_id INT PRIMARY KEY,
  name VARCHAR(100)
);

CREATE TABLE `Order` (
  order_id INT PRIMARY KEY,
  order_date DATE,
  customer_id INT,
  FOREIGN KEY (order_id) REFERENCES Customer(customer_id)
);

What is the error in this design?

AThe <code>Order</code> table is missing a primary key.
BThe foreign key references the wrong column; it should be <code>customer_id</code> referencing <code>Customer(customer_id)</code>.
CThe <code>Customer</code> table should have a foreign key to <code>Order</code>.
DThe <code>Order</code> table should not have a foreign key.
Attempts:
2 left
💡 Hint

Check which column should reference which in the foreign key.