1.

Explain the set operators in Teradata.

Answer»

Operators such as SET combine the results from multiple SELECT statements. It may seem similar to Joins, but Joins combine columns from different tables, whereas SET operators combine rows from different tables. The following set operators are supported by Teradata SQL

  • UNION: It combines results from several SELECT statements. Duplicate values are ignored.
  • UNION ALL: It combines results from several SELECT statements, including duplicate rows.
  • INTERSECT: It combines results from several SELECT statements. The function can be used to return those rows in the first SELECT statement that match those in the second SELECT statement. Basically, it returns the rows which exist in both SELECT statements.
  • MINUS/EXCEPT: It combines results from several SELECT statements. The function can be used to return those rows in the first SELECT statement that are not matched with those in the second SELECT statement.

Example: Assume the student table below is TABLE1 and the attendance table is Table2.

Roll_NoFirst_NameLast_NameDateofBirth
101AshaBisht18/1/1996
102RahulPatidar1/1/1997
103VishalBairagi25/9/1997
104GirjaShankar14/6/1994
105SonalDange20/8/1997
    
    
    
    
    

1. UNION

Following is a UNION query that combines the Roll_No values from both Table1 and Table2.

SELECT Roll_No FROM Table1 UNION SELECT Roll_No FROM Table2;

Output: Following is the output of the above query when executed. It does not includes duplicate values.

Roll_No
101
102
103
104
105

2. UNION ALL

Here is an example of a UNION ALL statement.

SELECT Roll_No FROM Table1 UNION ALL SELECT Roll_No FROM Table2;

Output: Following is the output of the above query when executed. It includes duplicate values as well.

Roll_No
101
102
103
104
105
101
102
103
104

3. INTERSECT

Here is an example of an INTERSECT statement. This command returns the Roll_No value that exists or present in both tables i.e., Table1 and Table2.

SELECT Roll_No FROM Table1 INTERSECT SELECT Roll_No FROM Table2;

Output: Following is the output of the above query when executed. The Roll_No105 is EXCLUDED because there is no such record in Table2.

Roll_No
101
102
103
104

4. MINUS/EXCEPT

Here is an example of a MINUS/EXCEPT statement.

SELECT Roll_No FROM Table1 MINUSSELECT Roll_No FROM Table2;

Output: Following is the output of the above query when executed. Only Roll_No105 is INCLUDED because it is present in Table1 but there is no such record in Table2.

Roll_No
105


Discussion

No Comment Found