Last answered:

18 Jun 2022

Posted on:

26 May 2022

1

UNION - It didn't remove the duplicates

Please why didn't my code remove the duplicates when i applied the UNION

SELECT
    e.emp_no,
    e.first_name,
    e.last_name,
    NULL AS dept_no,
    NULL AS from_date
FROM
    employees_dup e
WHERE
    e.emp_no = 10001
UNION SELECT
    NULL AS emp_no,
    NULL AS first_name,
    NULL AS last_name,
    dm.dept_no,
    dm.from_date
FROM
    dept_manager dm;













image

1 answers ( 0 marked as helpful)
Instructor
Posted on:

18 Jun 2022

0

Hi Ashakah!

Thanks for reaching out.

By construction, UNION and UNION ALL are operators used to unify two datasets. In other words, data from the first data set you've used (as extracted from the employees_dup e table) will be added to the data obtained from the other data set you've mentioned: dept_manager dm.
Otherwise, you can use other MySQL tools, such as joins and GROUP BY to obtain an output removing duplicate records.

Hope this helps.
Best,
Martin

Submit an answer