CompTIA Data+ (DA0-002)Data MiningHard

A data analyst is performing a complex query on a database with two tables: `Employees (emp_id, emp_name, dept_id)` and `Departments (dept_id, dept_name, location)`. The analyst needs to retrieve the names of all employees and their respective department names, including employees who are not currently assigned to any department (i.e., `dept_id` is NULL in the `Employees` table) and departments that currently have no employees. Which SQL join type is necessary to satisfy these requirements?

  1. ARIGHT JOIN
  2. BFULL OUTER JOIN
  3. CINNER JOIN
  4. DLEFT JOIN
Show answer & explanation

Correct answer: B. FULL OUTER JOIN

A FULL OUTER JOIN returns all rows from both the left table and the right table, including rows where there is no match in the other table. This ensures that employees without departments and departments without employees are both included in the result set, with NULLs in the non-matching columns.

Why the other options are wrong

  • A. RIGHT JOIN would include all departments (and their employees if matched), but would exclude employees who are not assigned to any department.
  • C. INNER JOIN would only return employees who have a matching department and departments that have employees, excluding those with no match.
  • D. LEFT JOIN would include all employees (and their departments if matched), but would exclude departments that have no employees.

SQL FULL OUTER JOIN

An SQL join type that returns all rows from both the left and right tables, including unmatched rows from either side, with NULL values for the columns of the table that has no match.

  • Combines the results of both LEFT JOIN and RIGHT JOIN.
  • Used when you need to see all records from both datasets, regardless of a match.
  • Can result in a large dataset with many NULL values.

Memory trick: Joins connect, but some are more inclusive than others.

More Data Mining questions