Bird
Raised Fist0
SQLquery~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is a key characteristic of a one-to-one relationship in database design?
easy
A. Each row in one table matches exactly one row in another table
B. Each row in one table can match many rows in another table
C. Rows in both tables have no connection
D. One table contains all data without links

Solution

  1. Step 1: Understand one-to-one relationship meaning

    A one-to-one relationship means each record in one table corresponds to exactly one record in another table.
  2. Step 2: Compare options to definition

    Each row in one table matches exactly one row in another table matches this definition perfectly, while others describe different relationships or no relationship.
  3. Final Answer:

    Each row in one table matches exactly one row in another table -> Option A
  4. Quick Check:

    One-to-one = single matching row [OK]
Hint: One-to-one means one row links to exactly one row [OK]
Common Mistakes:
  • Confusing one-to-one with one-to-many
  • Thinking tables have no relation
  • Assuming one table holds all data
2. Which SQL constraint is commonly used to enforce a one-to-one relationship between two tables?
easy
A. FOREIGN KEY without UNIQUE
B. CHECK constraint on any column
C. UNIQUE constraint on the foreign key column
D. NOT NULL constraint on primary key

Solution

  1. Step 1: Identify constraint enforcing uniqueness

    To ensure one-to-one, the foreign key must be unique so no duplicates link to the same row.
  2. Step 2: Match constraints to this need

    UNIQUE constraint on the foreign key column enforces this, while FOREIGN KEY alone does not guarantee uniqueness.
  3. Final Answer:

    UNIQUE constraint on the foreign key column -> Option C
  4. Quick Check:

    Unique foreign key = one-to-one [OK]
Hint: Use UNIQUE on foreign key to enforce one-to-one [OK]
Common Mistakes:
  • Using FOREIGN KEY without UNIQUE allows many-to-one
  • Confusing CHECK with uniqueness
  • Assuming NOT NULL enforces one-to-one
3. Given these tables:
CREATE TABLE Person (
  PersonID INT PRIMARY KEY,
  Name VARCHAR(50)
);

CREATE TABLE Passport (
  PassportID INT PRIMARY KEY,
  PersonID INT UNIQUE,
  Number VARCHAR(20),
  FOREIGN KEY (PersonID) REFERENCES Person(PersonID)
);

What does the UNIQUE constraint on PersonID in Passport ensure?
medium
A. Each person can have multiple passports
B. Each passport belongs to exactly one person, and each person has at most one passport
C. PersonID can be null in Passport
D. PassportID can be duplicated

Solution

  1. Step 1: Understand UNIQUE on PersonID in Passport

    The UNIQUE constraint means no two rows in Passport can have the same PersonID, so one person links to at most one passport.
  2. Step 2: Analyze relationship enforced

    Since Passport has a foreign key to Person and PersonID is unique, each passport belongs to one person, and each person can have only one passport.
  3. Final Answer:

    Each passport belongs to exactly one person, and each person has at most one passport -> Option B
  4. Quick Check:

    Unique foreign key = one-to-one link [OK]
Hint: UNIQUE foreign key means one-to-one link [OK]
Common Mistakes:
  • Thinking one person can have many passports
  • Ignoring UNIQUE constraint effect
  • Assuming null allowed without checking
4. Consider this table design:
CREATE TABLE Employee (
  EmployeeID INT PRIMARY KEY,
  Name VARCHAR(50)
);

CREATE TABLE EmployeeDetails (
  DetailID INT PRIMARY KEY,
  EmployeeID INT,
  Address VARCHAR(100),
  FOREIGN KEY (EmployeeID) REFERENCES Employee(EmployeeID)
);

What is missing to enforce a one-to-one relationship between Employee and EmployeeDetails?
medium
A. Add PRIMARY KEY on EmployeeID in EmployeeDetails
B. Add NOT NULL constraint on EmployeeID in EmployeeDetails
C. Remove FOREIGN KEY constraint
D. Add UNIQUE constraint on EmployeeID in EmployeeDetails

Solution

  1. Step 1: Identify current constraints

    EmployeeDetails has a foreign key to Employee but no uniqueness on EmployeeID, so multiple details can link to one employee.
  2. Step 2: Determine what enforces one-to-one

    Adding UNIQUE on EmployeeID ensures each employee links to at most one detail, enforcing one-to-one.
  3. Final Answer:

    Add UNIQUE constraint on EmployeeID in EmployeeDetails -> Option D
  4. Quick Check:

    Unique foreign key needed for one-to-one [OK]
Hint: Add UNIQUE on foreign key column for one-to-one [OK]
Common Mistakes:
  • Assuming NOT NULL enforces one-to-one
  • Removing foreign key breaks relationship
  • Confusing primary key with foreign key uniqueness
5. You want to split user data into two tables: User and UserProfile. Each user has exactly one profile. Which design best enforces this one-to-one relationship?
hard
A. User and UserProfile share the same primary key column
B. UserProfile has a foreign key to User without UNIQUE constraint
C. UserProfile has no foreign key but a separate primary key
D. UserProfile has a foreign key to User with UNIQUE constraint on that foreign key

Solution

  1. Step 1: Understand one-to-one enforcement methods

    One way is to share the same primary key in both tables, ensuring exactly one matching row.
  2. Step 2: Compare options

    User and UserProfile share the same primary key column uses the same primary key in both tables, which is a strong one-to-one design. UserProfile has a foreign key to User with UNIQUE constraint on that foreign key is valid but less strict. Options B and C do not enforce one-to-one properly.
  3. Final Answer:

    User and UserProfile share the same primary key column -> Option A
  4. Quick Check:

    Shared primary key = strict one-to-one [OK]
Hint: Use shared primary key for strict one-to-one [OK]
Common Mistakes:
  • Ignoring uniqueness on foreign key
  • Assuming foreign key alone enforces one-to-one
  • Not linking tables properly