Given employees(employee_id, name, department_id) and departments(department_id, department_name), which ON condition matches employees to their departments using the stated key relationship?
4 SQL Joins Online Quiz Questions
Use this free practice quiz with 30 questions to review 4 SQL Joins, test your knowledge, and prepare for your next test or exam.
A query uses FROM employees AS e JOIN departments AS d ON e.department_id = d.department_id. An employee has no matching department. What happens to that employee's row?
- A
It keeps every employee and fills department columns with NULL when unmatched.
- B
It keeps every department and fills employee columns with NULL when unmatched.
- C
It returns only employees and departments whose department IDs match.
- D
It returns every possible employee-department pair.
You need a result that includes every employee, even employees without a matching department. Which join setup meets that requirement?
- A
Use
employees AS e LEFT JOIN departments AS dwith the department-ID match condition. - B
Use
employees AS e INNER JOIN departments AS dwith the department-ID match condition. - C
Use
employees AS e RIGHT JOIN departments AS dwith the department-ID match condition. - D
Use
employees AS e CROSS JOIN departments AS d.
Using employees AS e and departments AS d, which approach returns only employees who have no matching department?
- A
Use
INNER JOINandWHERE d.department_id IS NOT NULL. - B
Use
LEFT JOINandWHERE e.department_id IS NULL. - C
Use
FULL OUTER JOINandWHERE d.department_id IS NOT NULL. - D
Use
LEFT JOINandWHERE d.department_id IS NULL.
For A RIGHT JOIN B ON A.key = B.key, what happens to a row in B that has no matching row in A?
- A
Every row from the left table remains, with right-side columns set to NULL for unmatched rows.
- B
Every row from the right table remains, with left-side columns set to NULL for unmatched rows.
- C
Only matching row pairs remain.
- D
Every possible pair of rows is returned.
You need matched records plus unmatched records from both input tables. Which join type should you use?
- A
It returns only matching pairs.
- B
It preserves all rows from the left table but discards unmatched right rows.
- C
It preserves all rows from both tables and uses NULL for columns on an unmatched side.
- D
It returns every possible pair, whether or not the keys match.
A products table has 8 rows and a sizes table has 5 rows. How many rows does products CROSS JOIN sizes return?
- A
40 rows
- B
13 rows
- C
8 rows
- D
5 rows
Given orders(order_id, customer_id), customers(customer_id, name), order_lines(order_id, product_id, quantity), and products(product_id, product_name), which set of join conditions connects each order to its customer and its line-item products?
- A
Join
orderstoproductsonorder_id, thencustomerstoorder_linesoncustomer_id. - B
Join
orderstocustomersonorder_id, thenorder_linestoproductsonorder_id. - C
Join
orderstoorder_linesonproduct_id, thenorder_linestoproductsonorder_id. - D
Join
orderstocustomersoncustomer_id,orderstoorder_linesonorder_id, andorder_linestoproductsonproduct_id.
An order has three matching rows in order_lines. A query joins orders to order_lines on order_id. How can that order appear in the result?
- A
The order must appear only once because
order_idis unique inorders. - B
The order can appear on multiple result rows, one per matching order line.
- C
The join discards all but the first matching order line.
- D
The join produces every product for that order, even products not in its order lines.
A query uses employees AS e LEFT JOIN departments AS d ON e.department_id = d.department_id, followed by WHERE d.department_name = 'Sales'. Why might this fail to retain employees with no matching department, and how can the query be adjusted if those employees must remain?
- A
The WHERE condition makes the join preserve more unmatched employees.
- B
The query becomes a cross join because the filter refers to the right table.
- C
The WHERE condition removes NULL-extended rows; moving the restriction into ON can preserve unmatched employees.
- D
The filter has no effect on rows where
d.department_nameis NULL.
You want every employee in the result, but you want only Sales departments to match. Where should you put the department-name restriction?
- A
Put
d.department_name = 'Sales'in the ON condition and keep the LEFT JOIN. - B
Keep the LEFT JOIN, but put
d.department_name = 'Sales'in a WHERE condition. - C
Change the join to INNER JOIN and put
d.department_name = 'Sales'in the ON condition. - D
Change the join to CROSS JOIN and put
d.department_name = 'Sales'in the ON condition.
Why might a query that joins two tables use SELECT e.name, d.department_name instead of SELECT *?
- A
It forces unmatched rows to be excluded.
- B
It automatically removes duplicate result rows.
- C
It changes the join into an inner join.
- D
It makes the result easier to interpret and avoids unnecessary or duplicate-named columns.
Given employees(employee_id, name, department_id) and departments(department_id, department_name), which condition matches an employee to the referenced department?
- A
Match employees.employee_id to departments.department_id.
- B
Match employees.department_id to departments.department_id.
- C
Match employees.name to departments.department_name.
- D
Match employees.department_id to departments.department_name.
A query uses INNER JOIN to connect employees with departments. What happens to an employee whose department_id has no matching row in departments?
- A
Every employee, with NULL department fields for employees without a match.
- B
Every department, with NULL employee fields for departments without a match.
- C
Only employees and departments whose rows satisfy the join condition.
- D
Every possible employee-department pair.
The query starts with employees LEFT JOIN departments. If one employee has no matching department, how is that employee represented in the result?
- A
Every employee remains; right-table columns are NULL when no department matches.
- B
Every department remains; left-table columns are NULL when no employee matches.
- C
Only employees with a matching department remain.
- D
Every possible employee-department pair is returned.
In employees RIGHT JOIN departments, which unmatched rows are guaranteed to remain?
- A
Only matching rows remain, with no NULL values.
- B
Every employees row remains, whether or not it matches.
- C
Every row from both tables remains, whether or not it matches.
- D
Every departments row remains; left-table columns are NULL when no employee matches.
You need a result that retains every row from both employees and departments, including rows with no match. Which join type meets this requirement?
- A
Only matched rows from the two tables.
- B
Matched rows and unmatched rows from both tables, with NULLs on the missing side.
- C
All rows from the left table, but no unmatched rows from the right table.
- D
Every possible pair of rows, whether or not the join condition matches.
A products table has 4 rows and a sizes table has 3 rows. How many rows does products CROSS JOIN sizes return?
- A
7 rows
- B
1 row
- C
12 rows
- D
4 rows
An order query uses orders, customers, order_lines, and products. Which set of join relationships correctly follows the schema described for these tables?
- A
Join orders to customers on customer_id, orders to order_lines on order_id, and order_lines to products on product_id.
- B
Join orders to customers on order_id, orders to order_lines on product_id, and order_lines to products on customer_id.
- C
Join orders to customers on product_id, orders to order_lines on customer_id, and order_lines to products on order_id.
- D
Join every table to customers using customer_id, without conditions for the other relationships.
An order has three matching rows in order_lines. When the query joins orders to order_lines, how many result rows represent that order?
- A
One row, because each order can appear only once in a join result.
- B
Two rows, because the order is represented once for the order and once for its lines.
- C
Four rows, because the order row is repeated in addition to its three lines.
- D
Three rows, one for each matching order line.
A query uses employees LEFT JOIN departments, then filters with WHERE departments.department_name = 'Sales'. What happens to employees with no matching department?
- A
It retains all employees because the query uses LEFT JOIN.
- B
It removes employees without a matching department, so the unmatched left rows do not survive.
- C
It turns all unmatched department values into the text 'Sales'.
- D
It adds all departments with no employees to the result.
You want every employee to remain in the result, but you want only matching departments named 'Sales' to be included. Where should the department-name restriction go?
- A
Put the restriction in WHERE and add DISTINCT so unmatched employees remain.
- B
Replace LEFT JOIN with INNER JOIN and filter in ON.
- C
Put the department-name restriction in ON so employees without a qualifying match remain with NULL department fields.
- D
Use CROSS JOIN and filter out employees after the join.
Both joined tables have a column named department_id. What is a characteristic of writing the join condition as USING (department_id)?
- A
It matches on the shared column and returns one copy of that matching column.
- B
It returns two copies of the shared column, one from each table.
- C
It creates every possible pair of rows and ignores the shared column.
- D
It keeps only rows that have no match.
Why is selecting named columns often preferable to SELECT * in a query that joins tables?
- A
SELECT * guarantees that matching rows are not duplicated.
- B
SELECT * automatically changes an outer join into an inner join.
- C
Explicit columns prevent one-to-many relationships from producing repeated rows.
- D
Naming the needed columns makes the result easier to interpret and avoids unnecessary or duplicate-named columns.
Given employees and departments joined on employees.department_id = departments.department_id, what does an INNER JOIN return?
- A
It returns every employee and fills department columns with NULL when there is no match.
- B
It returns only employee-department row pairs that satisfy the join condition.
- C
It returns every possible employee-department pair.
- D
It returns every department, including those without employees.
A products table has 3 rows and a sizes table has 4 rows. How many rows does products CROSS JOIN sizes return?
- A
3
- B
7
- C
12
- D
24
A report must include every employee and every department, including employees without a department match and departments without employees. Which join behavior meets this requirement?
- A
Keep all rows from both tables; use NULLs for columns from the side without a match.
- B
Keep only rows with matches in both tables.
- C
Keep all rows from the left table but discard unmatched rows from the right table.
- D
Return every possible pair of rows from the two tables.
Using employees AS e and departments AS d, which approach returns employees who have no matching department?
- A
Use an inner join and filter with
d.department_id IS NULL. - B
Use a right join and filter with
e.department_id IS NULL. - C
Use a cross join and filter with
d.department_id IS NULL. - D
Use a left join and filter with
d.department_id IS NULL.
An order has three matching rows in order_lines. In a query joining orders to order_lines, why might the order's details appear on three result rows?
- A
The join removes the order whenever it has more than one line.
- B
The query creates one row for every customer, regardless of orders.
- C
Each matching order line contributes a result row, so the order details repeat.
- D
The join combines all order lines into one result row automatically.
You need every employee to remain in the result, but only want a right-table row to match when it meets an additional condition. Where should you put that right-table restriction?
- A
Put the restriction in
WHERE; it preserves all unmatched left rows. - B
Put the restriction in
ON; this restricts right-side matches while preserving unmatched left rows. - C
Replace the left join with an inner join; it retains unmatched employees.
- D
Put the restriction in
SELECT; this changes which right-side rows match.