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...

Concept of Software Development life Cycle in IT industry #DOTC_Mdb

Concept of Software Development life Cycle in IT industry.   Hello Everyone, In my previous blog we discussed about Agile Model which is also a part of SDLC, but today we discussed about Software Development life cycle in details and how being software products developed or services provided through SDLC. I suggest you to please go through my previous blog that is related to Agile model also a part of SDLC which permits a company to develop software in small, quick segments. https://dheeraj60.blogspot.com/2020/07/overview-of-agile-modelmethodology.html Let’s discuss about SDLC as its play very important role in developing software products as well as providing services to the clients in simple word we can say SDLC(Software Development life Cycle ) is a process that produces software with the highest quality and lowest cost in the shortest time. SDLC includes a detailed plan for how to develop, alter, maintain, and replace a software system .SDLC is also known as Software devel...

Pseudo Column in Oracle #Diksha Oracle Training Centre

  Pseudo Column in Oracle. Hello Everyone, Today I will discuss about Pseudo column in Oracle. This topic is very important so before going to read this blog, I suggest you to please see my previous blogs which is in archive folder for your reference so that it’s easy for you to understand this topic. Let’s discuss about Pseudo Column in Oracle. Pseudo column: A pseudo-column is dynamic in Oracle as it behaves like a table column but is not stored in the table. Pseudo column behaves like a table column, but is not actually stored in the table. You can select from pseudo columns, but you cannot insert, update, or delete their values. A pseudo column is also similar to a function without arguments. In   Simple word we can say Pseudo column is a column that yields a value when selected, but which is not an actual column of the table. An example is RowID or SysDate. It can be use in combination with the DUAL table. Below are the Pseudo Column commonly used in Oracle Dat...