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;
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;
RIGHT JOIN returns all rows from the right table and matching rows from the left table.
Since all department IDs in Employees match Departments, the RIGHT JOIN returns all departments with their employee names.
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;
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;
RIGHT JOIN includes all rows from the right table, even if no match exists in the left table.
The Marketing department has no matching employee, so employee name is NULL for that row.
Which of the following RIGHT JOIN queries will cause a syntax error?
Check the placement of the ON clause in JOIN syntax.
Option C uses WHERE instead of ON for the join condition, causing a syntax error.
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?
Consider which table is smaller and how join direction affects performance.
LEFT JOIN starting from the smaller Customers table is more efficient than RIGHT JOIN starting from large Orders.
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?
Think about which table is on the right and how RIGHT JOIN works.
RIGHT JOIN returns all rows from Sales (right table). If a sale has no matching product, product_name is NULL.