Example: Performing a Union Operation to Find Hours Worked - Teradata VantageCloud Lake

Lake - Working with SQL

Deployment
VantageCloud
Edition
Lake
Product
Teradata VantageCloud Lake
Release Number
Published
February 2025
ft:locale
en-US
ft:lastEdition
2025-11-21
dita:mapPath
jbe1714339405530.ditamap
dita:ditavalPath
pny1626732985837.ditaval
dita:id
jbe1714339405530

The following statement uses a union operation to return the number of man-hours charged by each employee who is working on a project and the names of employees who are not working on a project.

   SELECT Name, Proj_Id, Hours 
   FROM Employee, Charges 
   WHERE Employee.EmpNo = Charges.EmpNo
   UNION
   SELECT Name, Null (CHAR(8)), Null (DECIMAL(4,2)), 
   FROM Employee 
   WHERE EmpNo NOT IN 
   (SELECT EmpNo 
   FROM Charges
   )
   UNION
   SELECT Null (VARCHAR(12)), Proj_Id, Hours 
   FROM Charges 
   WHERE EmpNo NOT IN 
   (SELECT EmpNo 
   FROM Employee
   );

The first portion of the statement joins the Employee table with the Charges table on the EmpNo column. The second portion accounts for the employees who may be listed in the Employee table, but not the Charges table. The third portion of the statement accounts for the employees who may be listed in the Charges table and not in the Employee table. This makes sure that all the information asked for is included in the response.