Answer: UNION ALL Set Operator
Find a detailed explanation of set operator UNION ALL.
Solution
The solution is given below:
MySQL
/* The query to use UNION ALL to combine result sets */SELECT e.EmpID, e.EmpName, s.SkillName, p.ProjectNameFROM Employees eLEFT JOIN Skills s ON e.EmpID = s.EmpIDLEFT JOIN Projects p ON e.EmpID = p.EmpID OR s.SkillID = p.SkillIDUNION ALLSELECT e.EmpID, e.EmpName, NULL AS SkillName, NULL AS ProjectNameFROM Employees eWHERE e.EmpID NOT IN (SELECT DISTINCT EmpID FROM Skills)AND e.EmpID NOT IN (SELECT DISTINCT EmpID FROM Projects)ORDER BY EmpName DESC;
Code explanation
The explanation of the solution code is given below:
Line 2: The first
SELECTquery selectsEmpID,EmpName,SkillName, andProjectName. The aliases used are forEmployees,Skills, andProjects.Line 3: The data is retrieved from the
Employeestable. We use alias for the table.Line 4: The
LEFT JOINoperation is applied withSkillsto combine the data based on theEmpIDcolumn present in both theEmployeesandSkillstables.Line 5: The
LEFT JOINoperation is applied withProjectsto combine the data based on theEmpIDorSkillID.Line 6: The
UNION ALLkeyword is used to combine the results of two queries, including duplicates.Line 7: The ...
Ask