Bird
Raised Fist0
MySQLquery~20 mins

RIGHT JOIN in MySQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
RIGHT JOIN Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
What is the output of this RIGHT JOIN query?

Given two tables, Employees and Departments, what will be the result of the following query?

SELECT Employees.Name, Departments.DepartmentName
FROM Employees
RIGHT JOIN Departments ON Employees.DepartmentID = Departments.ID;
MySQL
CREATE TABLE Employees (ID INT, Name VARCHAR(20), DepartmentID INT);
INSERT INTO Employees VALUES (1, 'Alice', 10), (2, 'Bob', 20), (3, 'Charlie', NULL);
CREATE TABLE Departments (ID INT, DepartmentName VARCHAR(20));
INSERT INTO Departments VALUES (10, 'HR'), (20, 'Engineering'), (30, 'Marketing');

SELECT Employees.Name, Departments.DepartmentName
FROM Employees
RIGHT JOIN Departments ON Employees.DepartmentID = Departments.ID;
A[('Alice', 'HR'), ('Bob', 'Engineering'), (NULL, 'Marketing')]
B[('Alice', 'HR'), ('Bob', 'Engineering')]
C[('Alice', 'HR'), ('Bob', 'Engineering'), ('Charlie', NULL), (NULL, 'Marketing')]
D[('Charlie', NULL), (NULL, 'Marketing')]
Attempts:
2 left
💡 Hint

Remember, RIGHT JOIN returns all rows from the right table, and matching rows from the left table.

🧠 Conceptual
intermediate
1:30remaining
Which statement best describes RIGHT JOIN?

Choose the correct description of what a RIGHT JOIN does in SQL.

AReturns all rows from the left table and matching rows from the right table.
BReturns all rows from both tables, combining them fully.
CReturns all rows from the right table and matching rows from the left table.
DReturns only rows that have matching values in both tables.
Attempts:
2 left
💡 Hint

Think about which table's rows are always included in the result.

📝 Syntax
advanced
1:30remaining
Identify the syntax error in this RIGHT JOIN query

Which option contains a syntax error in the RIGHT JOIN usage?

SELECT e.Name, d.DepartmentName
FROM Employees e
RIGHT JOIN Departments d ON e.DepartmentID = d.ID;
ASELECT e.Name, d.DepartmentName FROM Employees e RIGHT JOIN Departments d WHERE e.DepartmentID = d.ID;
B;DI.d = DItnemtrapeD.e NO d stnemtrapeD NIOJ THGIR e seeyolpmE MORF emaNtnemtrapeD.d ,emaN.e TCELES
CELECT e.Name, d.DepartmentName FROM Employees e RIGHT JOIN Departments d ON e.DepartmentID = d.ID;
DSELECT e.Name, d.DepartmentName FROM Employees e RIGHT JOIN Departments d ON e.DepartmentID = d.ID;
Attempts:
2 left
💡 Hint

Check the placement of the ON clause in JOIN syntax.

❓ optimization
advanced
2:00remaining
How to optimize a RIGHT JOIN query for better performance?

You have a RIGHT JOIN query joining large tables. Which option is the best way to optimize it?

AReplace RIGHT JOIN with LEFT JOIN and swap table order.
BAdd indexes on the join columns in both tables.
CUse SELECT * to fetch all columns for faster retrieval.
DRemove the ON clause to reduce complexity.
Attempts:
2 left
💡 Hint

Think about how databases speed up join operations.

🔧 Debug
expert
2:30remaining
Why does this RIGHT JOIN query return fewer rows than expected?

Given these tables:

Employees: ID, Name, DepartmentID
Departments: ID, DepartmentName

Query:

SELECT e.Name, d.DepartmentName
FROM Employees e
RIGHT JOIN Departments d ON e.DepartmentID = d.ID
WHERE e.Name IS NOT NULL;

Why might this query return fewer rows than a RIGHT JOIN without the WHERE clause?

AThe query syntax is invalid and causes an error.
BRIGHT JOIN ignores the WHERE clause, so it returns all rows anyway.
CThe ON condition is incorrect, causing fewer matches.
DThe WHERE clause filters out rows where e.Name is NULL, removing unmatched right table rows.
Attempts:
2 left
💡 Hint

Consider how WHERE filters rows after the join.

Practice

(1/5)
1. What does a RIGHT JOIN do in SQL?
easy
A. Returns all rows from the right table and matching rows from the left table.
B. Returns all rows from the left table and matching rows from the right table.
C. Returns only rows that have matching values in both tables.
D. Returns all rows from both tables, matching where possible.

Solution

  1. Step 1: Understand the direction of RIGHT JOIN

    A RIGHT JOIN returns all rows from the right table regardless of matches in the left table.
  2. Step 2: Identify matching rows from the left table

    It includes matching rows from the left table and fills NULL where no match exists.
  3. Final Answer:

    Returns all rows from the right table and matching rows from the left table. -> Option A
  4. Quick Check:

    RIGHT JOIN = all right table rows + matched left rows [OK]
Hint: RIGHT JOIN keeps all right table rows, left table may have NULLs [OK]
Common Mistakes:
  • Confusing RIGHT JOIN with LEFT JOIN
  • Thinking RIGHT JOIN returns only matching rows
  • Assuming RIGHT JOIN returns all rows from left table
2. Which of the following is the correct syntax for a RIGHT JOIN in MySQL?
easy
A. SELECT * FROM table1 RIGHT OUTER JOIN table2 WHERE table1.id = table2.id;
B. SELECT * FROM table1 JOIN RIGHT table2 ON table1.id = table2.id;
C. SELECT * FROM table1 RIGHT JOIN table2 ON table1.id = table2.id;
D. SELECT * FROM table1 RIGHT JOIN table2 USING id;

Solution

  1. Step 1: Check standard RIGHT JOIN syntax

    The correct syntax is SELECT ... FROM table1 RIGHT JOIN table2 ON condition;.
  2. Step 2: Validate each option

    SELECT * FROM table1 RIGHT JOIN table2 ON table1.id = table2.id; uses correct syntax with ON clause. SELECT * FROM table1 JOIN RIGHT table2 ON table1.id = table2.id; has wrong keyword order. SELECT * FROM table1 RIGHT OUTER JOIN table2 WHERE table1.id = table2.id; uses WHERE instead of ON. SELECT * FROM table1 RIGHT JOIN table2 USING id; uses invalid syntax (parentheses required: USING (id)).
  3. Final Answer:

    SELECT * FROM table1 RIGHT JOIN table2 ON table1.id = table2.id; -> Option C
  4. Quick Check:

    RIGHT JOIN syntax = FROM table1 RIGHT JOIN table2 ON condition [OK]
Hint: RIGHT JOIN needs ON clause, USING only if columns match exactly [OK]
Common Mistakes:
  • Using WHERE instead of ON for join condition
  • Incorrect keyword order like JOIN RIGHT
  • Confusing RIGHT JOIN with RIGHT OUTER JOIN (both valid but syntax matters)
3. Given these tables:

Employees
id | name
1 | Alice
2 | Bob

Departments
id | dept_name
2 | Sales
3 | Marketing

What is the result of:
SELECT Employees.name, Departments.dept_name FROM Employees RIGHT JOIN Departments ON Employees.id = Departments.id;
medium
A. [('Bob', 'Sales')]
B. [('Alice', NULL), ('Bob', 'Sales')]
C. [('Alice', NULL), ('Bob', 'Sales'), (NULL, 'Marketing')]
D. [('Bob', 'Sales'), (NULL, 'Marketing')]

Solution

  1. Step 1: Identify RIGHT JOIN behavior with given tables

    RIGHT JOIN keeps all rows from Departments (right table). Departments have ids 2 and 3.
  2. Step 2: Match Employees.id to Departments.id

    Employees with id=2 is Bob, matches Sales. No employee with id=3, so NULL for name and Marketing for dept_name.
  3. Final Answer:

    [('Bob', 'Sales'), (NULL, 'Marketing')] -> Option D
  4. Quick Check:

    RIGHT JOIN keeps all right rows, unmatched left = NULL [OK]
Hint: RIGHT JOIN shows all right rows, NULL for unmatched left [OK]
Common Mistakes:
  • Expecting all employees instead of all departments
  • Including unmatched left rows only
  • Confusing LEFT JOIN with RIGHT JOIN results
4. Consider this query:

SELECT a.id, b.value FROM tableA a RIGHT JOIN tableB b ON a.id = b.id;

It returns no rows even though tableB has data. What is the most likely cause?
medium
A. The join condition is incorrect or mismatched column names.
B. RIGHT JOIN does not return any rows if left table is empty.
C. RIGHT JOIN requires WHERE clause to filter rows.
D. The query syntax is invalid and causes an error.

Solution

  1. Step 1: Understand RIGHT JOIN behavior

    RIGHT JOIN returns all rows from right table even if left table has no matching rows.
  2. Step 2: Analyze why no rows returned

    If no rows appear, likely the join condition a.id = b.id does not match any rows due to wrong column names or data mismatch.
  3. Final Answer:

    The join condition is incorrect or mismatched column names. -> Option A
  4. Quick Check:

    No rows means join condition mismatch [OK]
Hint: No rows? Check join condition and column names first [OK]
Common Mistakes:
  • Assuming RIGHT JOIN needs WHERE clause
  • Thinking RIGHT JOIN returns no rows if left table empty
  • Believing syntax error causes no rows instead of error
5. You have two tables:

Orders
order_id | customer_id
1 | 101
2 | 102

Customers
customer_id | name
101 | John
103 | Alice

You want to list all customers and their orders if any. Which query using RIGHT JOIN gives the correct result?
hard
A. SELECT Customers.name, Orders.order_id FROM Customers RIGHT JOIN Orders ON Customers.customer_id = Orders.customer_id;
B. SELECT Customers.name, Orders.order_id FROM Orders RIGHT JOIN Customers ON Orders.customer_id = Customers.customer_id;
C. SELECT Customers.name, Orders.order_id FROM Orders LEFT JOIN Customers ON Orders.customer_id = Customers.customer_id;
D. SELECT Customers.name, Orders.order_id FROM Customers LEFT JOIN Orders ON Customers.customer_id = Orders.customer_id;

Solution

  1. Step 1: Identify which table is right to keep all customers

    We want all customers, so Customers must be the right table in RIGHT JOIN.
  2. Step 2: Check join condition and table order

    SELECT Customers.name, Orders.order_id FROM Orders RIGHT JOIN Customers ON Orders.customer_id = Customers.customer_id; uses Orders RIGHT JOIN Customers ON matching customer_id, so all Customers appear with matching Orders or NULL.
  3. Final Answer:

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

    RIGHT JOIN keeps all right table rows (Customers) [OK]
Hint: Put table with all rows as right table in RIGHT JOIN [OK]
Common Mistakes:
  • Swapping table order and losing all customers
  • Using LEFT JOIN instead of RIGHT JOIN incorrectly
  • Confusing join condition direction
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