Experimentdbms 7
Experimentdbms 7
Aim: Apply the SQL operator in oracle like UNION, EXCEPT and INTERSECT Operators to the tables.
Procedure:
The Differences between UNION EXCEPT and INTERSECT Operators in SQL Server with
some examples. Please read UNION and UNION ALL, EXCEPT and INTERSECT Operators
of this article series before proceeding to this article. The SET operators are mainly used to
combine the result of more than 1 select statement and return a single result set to the user.
The set operators work on complete rows of the queries, so the results of the queries must have
the same column name, same column order and the types of columns must be compatible.
There are the following 4 set operators in SQL Server:
1. UNION: Combine two or more result sets into a single set, without duplicates.
2. UNION ALL: Combine two or more result sets into a single set, including all duplicates.
3. INTERSECT: Takes the data from both result sets which are in common.
4. EXCEPT: Takes the data from the first result set, but not in the second result set (i.e. no
matching to each other)
Use below SQL Script to create and populate the two tables that we are going to use in our
examples.
Use below SQL Script to create and populate the two tables that we are going to use in our
examples.
UNION Operator:
The Union operator will return all the unique rows from both the queries. Notice that the
duplicates are removed from the result set.
The UNION ALL operator returns all the rows from both the queries, including the duplicates.
SELECT ID, Name, Gender, Department FROM TableA
UNION ALL
SELECT ID, Name, Gender, Department FROM TableB
Result:
INTERSECT Operator:
The INTERSECT operator retrieves the common unique rows from both the left and the right
query. Notice the duplicates are removed.
SELECT ID, Name, Gender, Department FROM TableA
INTERSECT
SELECT ID, Name, Gender, Department FROM TableB
Result:
EXCEPT Operator:
The EXCEPT operator will return unique rows from the left query that aren’t present in the right
query’s results.
In other word The SQL EXCEPT operator is used to return all rows in the first SELECT
statement that are not returned by the second SELECT statement. Each SELECT statement will
define a dataset. The EXCEPT operator will retrieve all records from the first dataset and then
remove from the results all records from the second dataset.
Explanation: The EXCEPT query will return the records in the blue shaded area. These
are the records that exist in Dataset1 and not in Dataset2.
Each SELECT statement within the EXCEPT query must have the same number of
fields in the result sets with similar data types.
If you want the rows that are present in Table B but not in Table A, reverse the queries.
SELECT ID, Name, Gender, Department FROM TableB
EXCEPT
SELECT ID, Name, Gender, Department FROM TableA;
Result:
For all these 4 operators to work the following 2 conditions must be met
1. The number and the order of the columns must be the same in both the queries
2. The data types must be the same or at least compatible
Note: - The EXCEPT operator is not supported in all SQL databases. It can be used in
databases such as SQL Server, PostgreSQL, and SQLite.
For databases such as Oracle, use the MINUS operator to perform this type of query.
Syntax-
Note: - The MINUS operator is not supported in all SQL databases. It can used in
databases such as Oracle.
For databases such as SQL Server, PostgreSQL, and SQLite, use the EXCEPT
operator to perform this type of query.
If you want the rows that are present in Table B but not in Table A, reverse the queries.
SELECT ID, Name, Gender, Department FROM TableB
MINUS
SELECT ID, Name, Gender, Department FROM TableA;
Result: