0
0
PostgreSQLquery~20 mins

LEFT JOIN and RIGHT JOIN in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Join Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of LEFT JOIN with unmatched rows

Given two tables Employees and Departments:

Employees(id, name, dept_id)
Departments(id, dept_name)

What is the output of this query?

SELECT e.name, d.dept_name
FROM Employees e
LEFT JOIN Departments d ON e.dept_id = d.id
ORDER BY e.id;
PostgreSQL
CREATE TABLE Employees (id INT, name TEXT, dept_id INT);
CREATE TABLE Departments (id INT, dept_name TEXT);
INSERT INTO Employees VALUES (1, 'Alice', 10), (2, 'Bob', 20), (3, 'Charlie', NULL);
INSERT INTO Departments VALUES (10, 'HR'), (30, 'Finance');
A
name | dept_name
-----|----------
Alice | HR
Charlie | NULL
B
name | dept_name
-----|----------
Alice | HR
Bob | Finance
Charlie | NULL
C
name | dept_name
-----|----------
Alice | HR
Bob | NULL
Charlie | NULL
D
name | dept_name
-----|----------
Bob | NULL
Charlie | NULL
Attempts:
2 left
💡 Hint

LEFT JOIN keeps all rows from the left table even if there is no matching row in the right table.

query_result
intermediate
2:00remaining
Output of RIGHT JOIN with unmatched rows

Using the same tables Employees and Departments, 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.id
ORDER BY d.id;
PostgreSQL
CREATE TABLE Employees (id INT, name TEXT, dept_id INT);
CREATE TABLE Departments (id INT, dept_name TEXT);
INSERT INTO Employees VALUES (1, 'Alice', 10), (2, 'Bob', 20), (3, 'Charlie', NULL);
INSERT INTO Departments VALUES (10, 'HR'), (30, 'Finance');
A
name | dept_name
-----|----------
Alice | HR
NULL | Finance
B
name | dept_name
-----|----------
Alice | HR
Bob | Finance
C
name | dept_name
-----|----------
Alice | HR
Bob | NULL
NULL | Finance
D
name | dept_name
-----|----------
NULL | HR
NULL | Finance
Attempts:
2 left
💡 Hint

RIGHT JOIN keeps all rows from the right table even if there is no matching row in the left table.

🧠 Conceptual
advanced
2:00remaining
Difference between LEFT JOIN and RIGHT JOIN

Which statement correctly describes the difference between LEFT JOIN and RIGHT JOIN?

ALEFT JOIN returns only rows with matches in both tables; RIGHT JOIN returns all rows from the left table only.
BLEFT JOIN and RIGHT JOIN always return the same results but in different order.
CLEFT JOIN returns all rows from the right table; RIGHT JOIN returns all rows from the left table.
DLEFT JOIN returns all rows from the left table and matching rows from the right; RIGHT JOIN returns all rows from the right table and matching rows from the left.
Attempts:
2 left
💡 Hint

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

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

Which option contains a syntax error in the JOIN clause?

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

Check the placement of the ON and WHERE keywords in JOIN syntax.

optimization
expert
3:00remaining
Optimizing query with LEFT JOIN and filtering

You want to find all employees and their departments, but only those employees who belong to departments with name starting with 'S'. Which query is most efficient?

ASELECT e.name, d.dept_name FROM Employees e LEFT JOIN Departments d ON e.dept_id = d.id AND d.dept_name LIKE 'S%';
BSELECT e.name, d.dept_name FROM Employees e LEFT JOIN Departments d ON e.dept_id = d.id WHERE d.dept_name LIKE 'S%';
CSELECT e.name, d.dept_name FROM Employees e INNER JOIN Departments d ON e.dept_id = d.id WHERE d.dept_name LIKE 'S%';
DSELECT e.name, d.dept_name FROM Employees e RIGHT JOIN Departments d ON e.dept_id = d.id WHERE d.dept_name LIKE 'S%';
Attempts:
2 left
💡 Hint

Consider where to put the filtering condition to avoid filtering after the join.