Skip to main content

Set Operator uses and Benefits in Oracle Database #DOTC_Mdb

 

              Set Operator uses and Benefits in Oracle Database 


Today we will discuss about different Set operators used in SQL with examples. SET operator used to select data from multiple tables as Set operators combine the results of two component queries into a single result. Queries containing set operators are called compound queries.

Through SET Operator in SQL we don’t need to use Joins and make alias of table to get the common record and all distinct row selected by query.

SQL Set Operators are of 4 parts which work similar to Mathematics which we learn in class 10th or so.

1.     UNION

2.     UNION ALL

3.     INTERSECT

4.     MINUS/EXCEPT.

   

Things to Remember while using SET operator:

Select clause should have the same number of columns and columns must be of same data type, Column needs to be specified in the same order in the select clause.

 

UNION: UNION set operator use to select all distinct rows selected by either query. UNION returns all the unique rows returned from both the queries. UNION set operator mainly used to combine the both select statement result set and removes duplicate records between the tables. So it returns distinct record.

Using tables we can use UNION set operator to see the result.

To see the distinct record from two tables we use below query to get the data .

Select location from employee

UNION

Select location from department;

Output of the above query is as below:


 

 

Here you can see 7 distinct records from both the table as Chennai was in Employee table and Ranchi was in Department table but we get all the distinct records no duplicates. You can also select 2 or more column with same data type to retrieve the data.

 

UNION ALL : UNION ALL : UNION ALL set operators  mainly use to select all rows selected by either query, including all duplicates. In other way we can say UNION ALL combines the both SELECT statement result sets and return  as it is. So it can contain duplicate depends on the data from the tables.

 

For Example we have location_name common in both table employee and department . To see the records from  both the tables by using UNION ALL we use below query to get the data.

Select Location from Employee

UNION ALL

Select Location from Department;

Output of the above query is as below :

 


 

Here we can see all the records from both the table , Also if we have multiple duplicate records in column UNION ALL will display all that records as well for example if Patna comes 2 times in Employee table and 1 times in Department table then it will give 3 Patna as output.You can also select 2 or more column with same data type to retrieve the data.

Difference between Union and Union ALL is UNION returns the distinct rows whereas Union all returns all the rows. So we need to be careful while choosing these set operators.

 

INTERSECT: INTERSECT operator used to get all distinct rows selected by both queries. INTERSECT mainly used to combines the both SELECT statement data-sets and return  the distinct common rows between the statements. So If a record exists in one query and not in the other, it will be omitted by using INTERSECT set operator.

 

For Example we have location_name common in both table employee and department . To see the records from  both the tables by using INTERSECT we use below query to get the data.

 

Select Location from employee

INTERSECT

Select location from department;

 


 

Here you are able to see the match records from both the table , not any unmatched records from both the tables . INTERSECT operator is used to retrieve the common records from both the select clause of the Intersect Operator while we have seen in UNION and UNION all it gives all the records from both the tables.You can also select 2 or more column with same data type to retrieve the data.

 

MINUS : MINUS Set Oprator get  all distinct rows selected by the first query but not the second. In other word It combines  both SELECT statement and return  the  rows which is selected from first select statement and then remove from the data-set all the rows retrieved from second select statement.

 

 

For Example we have location_name common in both table employee and department . To see the records from  both the tables by using MINUS we use below query to get the data.

 

Select Location from employee

MINUS

Select  location from department;

 

 


 

Here we see only 1 record as this records not match in department table so we get only 1 record.You can also select 2 or more column with same data type to retrieve the data.

MINUS operator is not supported in  RDBMS like MYSQL and SQL server.

This is all about set operators in SQL and I hope you are able to understand all the set Operators please try with different columns and table to see the result and output. 

Please go through this blog and let me know if you have any doubts.

Thanks

Comments

Post a Comment

Popular posts from this blog

SQL and Classification of SQL in Oracle Database #Diksha Oracle Training Centre

  SQL and Classification of SQL in Oracle Database.   SQL is Structured Query Language , which is used for storing, manipulating and retrieving data stored in a relational database .SQL is the standard language for RDBMS. All the Relational Database Management Systems (RDMS) like Oracle, MySQL, Sybase, Informix, IBM DB2 and Microsoft SQL Server use SQL as their standard database language. Oracle is one of the more secured database as compared to other databases. Importance of   SQL : SQL and PL/SQL is a backend process where all data is stored and retrieved in GUI which created either by any programming languages like Java, C++, PHP etc. so we need to have very secure database so that there will be no impact for users. SQL allows users to access data in the relational database management systems. SQL is used to communicate with a database.SQL and PL/SQL allows users to create and drop databases tables , views , stored procedures , functions , packages , trigger et...

Top 50 Interview Questions On SQL and PL/SQL #DOTC_Mdb

                    Top 50 Interview Questions On SQL and PL/SQL. Today we will Discuss Top 50 interview questions and answers of SQL and PL/SQL which is frequently asked in interview.     Question 1: What is SQL and Classification of SQL? Answer SQL is a Structure Query Language which is vastly used in RDBMS database like Oracle, Sybase, DB2 , Microsoft SQL server etc.   Classification of SQL is as below: DDL (Data Definition Language):  Commands are  create , alter , drop , truncate etc DML (Data Manipulation Language) : Commands are  insert,update and delete . TCL (Transaction Control Language ) : Commands are  Commit , Rollback and Save point. DCL (Data Control Language) : Commands are Grant , Revoke Question 2:    What is meant by Joins? What are the types of join? Answer Joins are basically used to extract/get data from multiple tables using s...

Aggregate Functions and Group By Clause in Oracle with Examples #Diksha Oracle Training Centre

  Aggregate Functions and Group By Clause Oracle with Examples.   Hello Everyone, Today I will   discuss about   Aggregate function in SQL by using Group By Clause and different clauses with some examples . Please go tjrough my previous blogs in Archive folder for classification of SQL, Commands and   SQL joins for your understanding.   Aggregate Functions Allows us to perform a calculation on a set of values to return a single value . We can use Group by Clause to group the result-set by one or more columns. Also we can use Having clause to restrict or filter the data as per our requirement. Note: Whenever we use Aggregate function in SQL we can’t able to use where condition. To restrict or filter the record we need to use having clause instead of Where. Below is the most commonly used Aggregate function in SQL.   MAX : Max function   used to get the maximum values in a set of values. COUNT : This function used to count rows in ...