0
0
SQLquery~20 mins

RIGHT JOIN execution behavior in SQL - Practice Problems & Coding Challenges

Choose your learning style9 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
Output of RIGHT JOIN with matching rows

Given two tables Employees and Departments:

Employees:
id | name | dept_id
1 | Alice | 10
2 | Bob | 20
3 | Carol | 30

Departments:
dept_id | dept_name
10 | HR
20 | IT
30 | Finance

What is the output of this query?

SELECT e.name, d.dept_name
FROM Employees e
RIGHT JOIN Departments d ON e.dept_id = d.dept_id
ORDER BY d.dept_id;
SQL
SELECT e.name, d.dept_name
FROM Employees e
RIGHT JOIN Departments d ON e.dept_id = d.dept_id
ORDER BY d.dept_id;
A
name | dept_name
Alice | HR
Bob   | IT
Carol | Finance
B
name | dept_name
Alice | HR
Bob   | IT
C
name | dept_name
Alice | HR
Bob   | IT
Carol | Finance
NULL  | NULL
D
name | dept_name
NULL  | HR
NULL  | IT
NULL  | Finance
Attempts:
2 left
💡 Hint

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

query_result
intermediate
2:00remaining
RIGHT JOIN with unmatched rows in right table

Consider the same tables as before but with an extra department:

Departments:
dept_id | dept_name
10 | HR
20 | IT
30 | Finance
40 | Marketing

What is the output of this query?

SELECT e.name, d.dept_name
FROM Employees e
RIGHT JOIN Departments d ON e.dept_id = d.dept_id
ORDER BY d.dept_id;
SQL
SELECT e.name, d.dept_name
FROM Employees e
RIGHT JOIN Departments d ON e.dept_id = d.dept_id
ORDER BY d.dept_id;
A
name  | dept_name
Alice | HR
Bob   | IT
Carol | Finance
NULL  | Marketing
B
name  | dept_name
Alice | HR
Bob   | IT
Carol | Finance
C
name  | dept_name
NULL  | HR
NULL  | IT
NULL  | Finance
NULL  | Marketing
D
name  | dept_name
Alice | HR
Bob   | IT
Carol | Finance
Marketing | NULL
Attempts:
2 left
💡 Hint

RIGHT JOIN includes all rows from the right table, even if no match exists in the left table.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in RIGHT JOIN query

Which of the following RIGHT JOIN queries will cause a syntax error?

ASELECT * FROM A RIGHT JOIN B ON A.id = B.id;
BSELECT * FROM A RIGHT JOIN B ON A.id = B.id LIMIT 5;
CSELECT * FROM A RIGHT JOIN B WHERE A.id = B.id;
DSELECT * FROM A RIGHT JOIN B ON A.id = B.id ORDER BY B.id;
Attempts:
2 left
💡 Hint

Check the placement of the ON clause in JOIN syntax.

optimization
advanced
2:00remaining
Optimizing RIGHT JOIN with large tables

You have two large tables: Orders (millions of rows) and Customers (thousands of rows). You want to get all customers and their orders if any.

Which query is likely to perform best?

ASELECT c.name, o.order_id FROM Orders o RIGHT JOIN Customers c ON o.customer_id = c.id;
BSELECT c.name, o.order_id FROM Customers c LEFT JOIN Orders o ON o.customer_id = c.id;
CSELECT c.name, o.order_id FROM Orders o INNER JOIN Customers c ON o.customer_id = c.id;
DSELECT c.name, o.order_id FROM Customers c FULL JOIN Orders o ON o.customer_id = c.id;
Attempts:
2 left
💡 Hint

Consider which table is smaller and how join direction affects performance.

🧠 Conceptual
expert
2:00remaining
Effect of RIGHT JOIN on NULL values in result

Given tables Products and Sales, a RIGHT JOIN is performed:

SELECT p.product_name, s.sale_date
FROM Products p
RIGHT JOIN Sales s ON p.product_id = s.product_id;

Which statement about the NULL values in the output is true?

ANo NULLs appear because RIGHT JOIN always matches all rows.
BNULLs appear only in sale_date when a product exists without a matching sale.
CNULLs appear in both columns when no matching rows exist in either table.
DNULLs appear only in product_name when a sale exists without a matching product.
Attempts:
2 left
💡 Hint

Think about which table is on the right and how RIGHT JOIN works.