SQL SET OPERATION
Seena.K
Assistant Professor
Department of Vocational Studies
St. Mary’s College, Thrissur
Sql Set Operations,Seena.k.,St.Mary’s College
SET OPERATION
The SQL Set operation is used to combine the two or more SQL SELECT
statements.
Types of Set operation
1. Union
2. UnionAll
3. Intersect
4. Minus
Sql Set operations,Seena.k.,St.Mary’s College
UNION OPERATOR
 This operator is used to combine two similar queries results into one
single result.
 Here both the queries should have same number of columns and all the
respective columns should be of same datatypes.
 The order of columns in both the queries should also be same.
 This operator eliminates the duplicate records and displays single
record. The resultant records are sorted in ascending order by default.
Sql Set operations,Seena.k.,St.Mary’s College
Rules For Using Union Operator
1. To use this UNION clause, each SELECT statement must have
2. The same number of columns selected
3. The same number of column expressions
4. The same data type and
5. Have them in the same order
6. But they need not have to be in the same length
Sql Set operations,Seena.k.,St.Mary’s College
Syntax For Union Operator
SELECT Column_ name From table1
UNION
SELECT Column_name From table2
 If we need to retain the duplicate value in the resultset,then we have to
use UNION ALL operator
Example For Union Operator
First table Second table
ID NAME
1 Jack
2 Harry
3 Jackson
ID NAME
3 Jackson
4 Stephan
5 David
QUERY : Result Set table
SELECT * FROM First
UNION
SELECT * FROM Second;
Sql Set operations,Seena.k.,St.Mary’s College
ID NAME
1 Jack
2 Harry
3 Jackson
4 Stephan
5 David
UNION ALL OPERATOR
Union All operation is equal to the Union operation.it returns the
set without removing duplication and sorting the data
Syntax
SELECT Column_name FROM table1
UNION ALL
SELECT Column_name FROM table2;
Union Query
SELECT * FROM First
UNION ALL
SELECT * FROM Second
Sql Set operations,Seena.k.,St.Mary’s College
The Result Set table
ID NAME
1 Jack
2 Harry
3 Jackson
3 Jackson
4 Stephan
5 David
Sql Set operations,Seena.k.,St.Mary’s College
Sql Set operations,Seena.k.,St.Mary’s College
INTERSECTION OPERATOR
 operator compares the result of two queries and returns the distinct
rows that are output by both queries Here both the queries should
have same number of columns and all the respective columns should
be of same datatypes.
 Syntax
SELECT column_name From Table1
INTERSECT
SELECT column_name From Table2
Sql Set operations,Seena.k.,St.Mary’s College
Rules for Using Intersect Operator
 The number and the order of the column must be the same in the two
queries
 The data type of the corresponding columns must be in the same data
type group such as numeric or character.
SELECT *FROM First
INTERSECT
SELECT * FROM Second
Result Table :- ID NAME
3 Jackson
Sql Set operations,Seena.k.,St.Mary’s College
MINUS OPERATOR
 This operator is used to display the records that are present only in the
first table or query, and doesn’t present in second table / query. It basically
subtracts the first query results from the second.
 Syntax
SELECT Column_list1,Column_list2 From Table1
MINUS
SELECT Column_lis1,Column_list2 From Table2
Sql Set operations,Seena.k.,St.Mary’s College
MINUS OPERATOR
Minus query
SELECT * FROM First
MINUS
SELECT * From Second
The Result Set Table
ID NAME
1 Jack
2 Harry
Sql Set operations,Seena.k,St.Mar’ys College
REFERENCE
https://www.javapoint.com

Computer Science:Sql Set Operation

  • 1.
    SQL SET OPERATION Seena.K AssistantProfessor Department of Vocational Studies St. Mary’s College, Thrissur
  • 2.
    Sql Set Operations,Seena.k.,St.Mary’sCollege SET OPERATION The SQL Set operation is used to combine the two or more SQL SELECT statements. Types of Set operation 1. Union 2. UnionAll 3. Intersect 4. Minus
  • 3.
    Sql Set operations,Seena.k.,St.Mary’sCollege UNION OPERATOR  This operator is used to combine two similar queries results into one single result.  Here both the queries should have same number of columns and all the respective columns should be of same datatypes.  The order of columns in both the queries should also be same.  This operator eliminates the duplicate records and displays single record. The resultant records are sorted in ascending order by default.
  • 4.
    Sql Set operations,Seena.k.,St.Mary’sCollege Rules For Using Union Operator 1. To use this UNION clause, each SELECT statement must have 2. The same number of columns selected 3. The same number of column expressions 4. The same data type and 5. Have them in the same order 6. But they need not have to be in the same length
  • 5.
    Sql Set operations,Seena.k.,St.Mary’sCollege Syntax For Union Operator SELECT Column_ name From table1 UNION SELECT Column_name From table2  If we need to retain the duplicate value in the resultset,then we have to use UNION ALL operator
  • 6.
    Example For UnionOperator First table Second table ID NAME 1 Jack 2 Harry 3 Jackson ID NAME 3 Jackson 4 Stephan 5 David QUERY : Result Set table SELECT * FROM First UNION SELECT * FROM Second; Sql Set operations,Seena.k.,St.Mary’s College ID NAME 1 Jack 2 Harry 3 Jackson 4 Stephan 5 David
  • 7.
    UNION ALL OPERATOR UnionAll operation is equal to the Union operation.it returns the set without removing duplication and sorting the data Syntax SELECT Column_name FROM table1 UNION ALL SELECT Column_name FROM table2; Union Query SELECT * FROM First UNION ALL SELECT * FROM Second Sql Set operations,Seena.k.,St.Mary’s College
  • 8.
    The Result Settable ID NAME 1 Jack 2 Harry 3 Jackson 3 Jackson 4 Stephan 5 David Sql Set operations,Seena.k.,St.Mary’s College
  • 9.
    Sql Set operations,Seena.k.,St.Mary’sCollege INTERSECTION OPERATOR  operator compares the result of two queries and returns the distinct rows that are output by both queries Here both the queries should have same number of columns and all the respective columns should be of same datatypes.  Syntax SELECT column_name From Table1 INTERSECT SELECT column_name From Table2
  • 10.
    Sql Set operations,Seena.k.,St.Mary’sCollege Rules for Using Intersect Operator  The number and the order of the column must be the same in the two queries  The data type of the corresponding columns must be in the same data type group such as numeric or character. SELECT *FROM First INTERSECT SELECT * FROM Second Result Table :- ID NAME 3 Jackson
  • 11.
    Sql Set operations,Seena.k.,St.Mary’sCollege MINUS OPERATOR  This operator is used to display the records that are present only in the first table or query, and doesn’t present in second table / query. It basically subtracts the first query results from the second.  Syntax SELECT Column_list1,Column_list2 From Table1 MINUS SELECT Column_lis1,Column_list2 From Table2
  • 12.
    Sql Set operations,Seena.k.,St.Mary’sCollege MINUS OPERATOR Minus query SELECT * FROM First MINUS SELECT * From Second The Result Set Table ID NAME 1 Jack 2 Harry
  • 13.
    Sql Set operations,Seena.k,St.Mar’ysCollege REFERENCE https://www.javapoint.com