0
0
SQLquery~20 mins

One-to-one relationship design in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
One-to-One Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of a one-to-one join query

Given two tables Users and Profiles with a one-to-one relationship on user_id, what is the output of the following query?

SELECT u.user_id, u.name, p.bio FROM Users u JOIN Profiles p ON u.user_id = p.user_id ORDER BY u.user_id;

Tables:

Users
user_id | name
1 | Alice
2 | Bob
3 | Carol

Profiles
user_id | bio
1 | Loves cats
3 | Enjoys hiking

SQL
SELECT u.user_id, u.name, p.bio FROM Users u JOIN Profiles p ON u.user_id = p.user_id ORDER BY u.user_id;
A
1, Alice, Loves cats
2, Bob, NULL
3, Carol, Enjoys hiking
B
1, Alice, NULL
3, Carol, Enjoys hiking
C
1, Alice, Loves cats
2, Bob, 
3, Carol, Enjoys hiking
D
1, Alice, Loves cats
3, Carol, Enjoys hiking
Attempts:
2 left
💡 Hint

Remember that an INNER JOIN only returns rows with matching keys in both tables.

🧠 Conceptual
intermediate
1:30remaining
Identifying one-to-one relationship design

Which of the following best describes a one-to-one relationship in database design?

AEach row in Table A matches one or more rows in Table B
BEach row in Table A matches zero or more rows in Table B
CEach row in Table A matches exactly one row in Table B, and vice versa
DEach row in Table A matches zero or one row in Table B, but Table B can have many matches
Attempts:
2 left
💡 Hint

Think about the meaning of 'one-to-one' literally.

📝 Syntax
advanced
2:30remaining
Correct syntax for creating one-to-one tables

Which SQL statement correctly creates two tables with a one-to-one relationship using a foreign key?

A
CREATE TABLE Users (user_id INT PRIMARY KEY, name VARCHAR(50));
CREATE TABLE Profiles (profile_id INT PRIMARY KEY, user_id INT, bio TEXT, FOREIGN KEY (user_id) REFERENCES Users(user_id));
B
CREATE TABLE Users (user_id INT PRIMARY KEY, name VARCHAR(50));
CREATE TABLE Profiles (profile_id INT PRIMARY KEY, user_id INT UNIQUE, bio TEXT, FOREIGN KEY (user_id) REFERENCES Users(user_id));
C
CREATE TABLE Users (user_id INT PRIMARY KEY, name VARCHAR(50));
CREATE TABLE Profiles (profile_id INT PRIMARY KEY, user_id INT NOT NULL, bio TEXT);
D
CREATE TABLE Users (user_id INT PRIMARY KEY, name VARCHAR(50));
CREATE TABLE Profiles (profile_id INT PRIMARY KEY, user_id INT UNIQUE NOT NULL, bio TEXT);
Attempts:
2 left
💡 Hint

One-to-one requires the foreign key to be unique in the child table.

optimization
advanced
2:00remaining
Optimizing queries on one-to-one tables

You have two tables Employees and EmployeeDetails in a one-to-one relationship. Which query is the most efficient to get all employees with their details?

ASELECT * FROM Employees e JOIN EmployeeDetails d ON e.emp_id = d.emp_id;
BSELECT * FROM Employees e RIGHT JOIN EmployeeDetails d ON e.emp_id = d.emp_id;
CSELECT * FROM Employees e, EmployeeDetails d WHERE e.emp_id = d.emp_id;
DSELECT * FROM Employees e LEFT JOIN EmployeeDetails d ON e.emp_id = d.emp_id;
Attempts:
2 left
💡 Hint

Consider that every employee has exactly one detail record.

🔧 Debug
expert
2:30remaining
Debugging one-to-one relationship constraint violation

Given these tables:

CREATE TABLE Customers (customer_id INT PRIMARY KEY, name VARCHAR(50));
CREATE TABLE CustomerProfiles (profile_id INT PRIMARY KEY, customer_id INT UNIQUE, details TEXT, FOREIGN KEY (customer_id) REFERENCES Customers(customer_id));

When inserting this data:

INSERT INTO Customers VALUES (1, 'John');
INSERT INTO CustomerProfiles VALUES (10, 1, 'Likes sports');
INSERT INTO CustomerProfiles VALUES (11, 1, 'Likes music');

What error will the last insert cause?

AUnique constraint violation on customer_id
BPrimary key violation on profile_id
CForeign key constraint violation
DNo error, insert succeeds
Attempts:
2 left
💡 Hint

Think about the UNIQUE constraint on customer_id in CustomerProfiles.