Bird
Raised Fist0
MySQLquery~20 mins

Relational database concepts in MySQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Relational Database Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
Output of JOIN with NULL values
Consider two tables Employees and Departments. Employees may or may not belong to a department. What is the output of this query?
SELECT e.name, d.department_name FROM Employees e LEFT JOIN Departments d ON e.department_id = d.id ORDER BY e.name;
MySQL
CREATE TABLE Employees (id INT, name VARCHAR(20), department_id INT);
INSERT INTO Employees VALUES (1, 'Alice', 10), (2, 'Bob', NULL), (3, 'Charlie', 20);
CREATE TABLE Departments (id INT, department_name VARCHAR(20));
INSERT INTO Departments VALUES (10, 'HR'), (20, 'IT');

SELECT e.name, d.department_name FROM Employees e LEFT JOIN Departments d ON e.department_id = d.id ORDER BY e.name;
A[{"name": "Alice", "department_name": "HR"}, {"name": "Charlie", "department_name": "IT"}]
B[{"name": "Alice", "department_name": "HR"}, {"name": "Bob", "department_name": ""}, {"name": "Charlie", "department_name": "IT"}]
C[{"name": "Alice", "department_name": null}, {"name": "Bob", "department_name": null}, {"name": "Charlie", "department_name": null}]
D[{"name": "Alice", "department_name": "HR"}, {"name": "Bob", "department_name": null}, {"name": "Charlie", "department_name": "IT"}]
Attempts:
2 left
💡 Hint
LEFT JOIN keeps all rows from the left table even if no matching row in the right table.
🧠 Conceptual
intermediate
1:30remaining
Understanding Primary Key Constraints
Which statement best describes the role of a primary key in a relational database table?
AIt uniquely identifies each row and cannot contain NULL values.
BIt allows duplicate values but must be indexed for faster queries.
CIt is used to store large text data in the table.
DIt automatically encrypts the data in the table.
Attempts:
2 left
💡 Hint
Think about what makes each row unique and how databases enforce uniqueness.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in the SQL query
Which option contains a syntax error when creating a table with a foreign key constraint?
MySQL
CREATE TABLE Orders (
  order_id INT PRIMARY KEY,
  customer_id INT,
  FOREIGN KEY (customer_id) REFERENCES Customers(id)
);
ACREATE TABLE Orders (order_id INT PRIMARY KEY, customer_id INT, FOREIGN KEY customer_id REFERENCES Customers(id));
BCREATE TABLE Orders (order_id INT PRIMARY KEY, customer_id INT, FOREIGN KEY (customer_id) REFERENCES Customers(id));
C;))di(sremotsuC SECNEREFER )di_remotsuc( YEK NGIEROF ,TNI di_remotsuc ,YEK YRAMIRP TNI di_redro( sredrO ELBAT ETAERC
DCREATE TABLE Orders (order_id INT PRIMARY KEY, customer_id INT, FOREIGN KEY (customer_id) REFERENCES Customers(id) ON DELETE CASCADE);
Attempts:
2 left
💡 Hint
Check the syntax for declaring foreign keys in SQL.
❓ optimization
advanced
2:30remaining
Optimizing a query with multiple JOINs
Given three tables: Customers, Orders, and Products, which query is optimized to retrieve customer names and product names for all orders without unnecessary data duplication?
ASELECT c.name, p.product_name FROM Customers c, Orders o, Products p WHERE c.id = o.customer_id AND o.product_id = p.id;
BSELECT DISTINCT c.name, p.product_name FROM Customers c JOIN Orders o ON c.id = o.customer_id JOIN Products p ON o.product_id = p.id;
CSELECT c.name, p.product_name FROM Customers c LEFT JOIN Orders o ON c.id = o.customer_id LEFT JOIN Products p ON o.product_id = p.id;
DSELECT c.name, p.product_name FROM Customers c JOIN Orders o ON c.id = o.customer_id JOIN Products p ON o.product_id = p.id;
Attempts:
2 left
💡 Hint
Consider how to avoid duplicate rows when joining multiple tables.
🔧 Debug
expert
3:00remaining
Debugging a foreign key constraint failure
You try to insert a row into the Orders table with customer_id = 5, but get a foreign key constraint error. Which is the most likely cause?
AThe Orders table does not have a primary key defined.
BThe customer_id column in Orders allows NULL values.
CThere is no customer with id = 5 in the Customers table.
DThe Products table is missing a foreign key constraint.
Attempts:
2 left
💡 Hint
Foreign key constraints require the referenced value to exist in the parent table.

Practice

(1/5)
1. What is the main purpose of a primary key in a relational database table?
easy
A. To speed up database backups
B. To store large text data
C. To link two tables together
D. To uniquely identify each record in the table

Solution

  1. Step 1: Understand the role of primary keys

    A primary key is a column or set of columns that uniquely identifies each row in a table.
  2. Step 2: Compare options with the definition

    Only To uniquely identify each record in the table describes this unique identification role correctly.
  3. Final Answer:

    To uniquely identify each record in the table -> Option D
  4. Quick Check:

    Primary key = unique record ID [OK]
Hint: Primary key means unique ID for each row [OK]
Common Mistakes:
  • Confusing primary key with foreign key
  • Thinking primary key stores large data
  • Assuming primary key speeds backups
2. Which of the following is the correct syntax to declare a primary key on the column id in a MySQL table?
easy
A. PRIMARY KEY (id);
B. KEY PRIMARY id;
C. PRIMARY KEY id;
D. PRIMARY id KEY;

Solution

  1. Step 1: Recall MySQL primary key syntax

    In MySQL, the primary key is declared using PRIMARY KEY (column_name).
  2. Step 2: Match syntax with options

    PRIMARY KEY (id); matches the correct syntax with parentheses around the column name.
  3. Final Answer:

    PRIMARY KEY (id); -> Option A
  4. Quick Check:

    Primary key syntax uses parentheses [OK]
Hint: Primary key syntax uses parentheses around column [OK]
Common Mistakes:
  • Omitting parentheses around column name
  • Swapping order of keywords
  • Using incorrect keywords like KEY PRIMARY
3. Given two tables: Customers(id, name) and Orders(order_id, customer_id), which SQL query correctly retrieves all orders with the customer names?
medium
A. SELECT Orders.order_id, Customers.name FROM Orders JOIN Customers ON Orders.id = Customers.customer_id;
B. SELECT Orders.order_id, Customers.name FROM Orders JOIN Customers ON Orders.customer_id = Customers.id;
C. SELECT order_id, name FROM Orders, Customers WHERE Orders.id = Customers.id;
D. SELECT order_id, name FROM Orders LEFT JOIN Customers ON Orders.customer_id = Customers.order_id;

Solution

  1. Step 1: Identify the correct join condition

    The foreign key customer_id in Orders links to id in Customers.
  2. Step 2: Check each option's join condition

    SELECT Orders.order_id, Customers.name FROM Orders JOIN Customers ON Orders.customer_id = Customers.id; correctly joins on Orders.customer_id = Customers.id. Others use wrong columns or join types.
  3. Final Answer:

    SELECT Orders.order_id, Customers.name FROM Orders JOIN Customers ON Orders.customer_id = Customers.id; -> Option B
  4. Quick Check:

    Join on foreign key = primary key [OK]
Hint: Join tables on foreign key = primary key [OK]
Common Mistakes:
  • Swapping join columns incorrectly
  • Using WHERE instead of JOIN condition properly
  • Mixing up LEFT JOIN with INNER JOIN
4. You wrote this SQL to create a foreign key: FOREIGN KEY (customer_id) REFERENCES Customers(customer_id). But it gives an error. What is the likely cause?
medium
A. You must declare foreign keys before primary keys
B. Foreign keys cannot reference columns in the same table
C. The referenced column customer_id does not exist or is not a primary key in Customers
D. Foreign keys must reference columns named 'id' only

Solution

  1. Step 1: Understand foreign key requirements

    A foreign key must reference a column that exists and is a primary or unique key in the referenced table.
  2. Step 2: Analyze the error cause

    If Customers has no customer_id column or it is not a key, the foreign key declaration fails.
  3. Final Answer:

    The referenced column customer_id does not exist or is not a primary key in Customers -> Option C
  4. Quick Check:

    Foreign key must reference existing primary key [OK]
Hint: Foreign key must reference existing primary key column [OK]
Common Mistakes:
  • Referencing non-existent columns
  • Assuming foreign keys must reference 'id' column
  • Incorrect order of key declarations
5. You have two tables: Employees(emp_id, name) and Departments(dept_id, dept_name, manager_id). The manager_id in Departments references emp_id in Employees. Which of these statements is TRUE about this relationship?
hard
A. An employee can manage multiple departments, but a department can have only one manager
B. Each department must have exactly one manager who is an employee
C. A department can have multiple managers if multiple employees share the same emp_id
D. The manager_id column in Departments can be NULL only if emp_id is NULL in Employees

Solution

  1. Step 1: Understand foreign key relationship

    The manager_id in Departments points to emp_id in Employees, linking each department to one employee as manager.
  2. Step 2: Analyze relationship cardinality

    One department has one manager, but an employee can manage many departments (one-to-many).
  3. Step 3: Evaluate options

    An employee can manage multiple departments, but a department can have only one manager correctly states this one-to-many relationship. Each department must have exactly one manager who is an employee is wrong because departments can have one manager, but not necessarily exactly one if NULL allowed. A department can have multiple managers if multiple employees share the same emp_id is wrong because emp_id is unique. The manager_id column in Departments can be NULL only if emp_id is NULL in Employees is incorrect because NULL in manager_id is independent of emp_id NULL.
  4. Final Answer:

    An employee can manage multiple departments, but a department can have only one manager -> Option A
  5. Quick Check:

    One-to-many foreign key = An employee can manage multiple departments, but a department can have only one manager [OK]
Hint: Foreign key one-to-many means one employee, many departments [OK]
Common Mistakes:
  • Assuming multiple managers per department
  • Confusing NULL constraints
  • Thinking emp_id can be duplicated
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard