SQL Tutorial

SQL Tutorial SQL Introduction SQL Syntax SQL Data Types SQL OPERATORS SQL COMMANDS SQL Queries

SQL Database

SQL Create Database SQL DROP Database SQL SELECT Database

SQL Table

SQL TABLE SQL CREATE TABLE SQL COPY TABLE SQL ALTER TABLE SQL DELETE SQL TRUNCATE TABLE SQL DROP TABLE SQL UPDATE TABLE SQL INSERT TABLE

SQL SELECT

SQL SELECT Statement SQL SELECT WHERE Clause SQL SELECT IN Operator SQL BETWEEN Operator SQL SELECT BETWEEN Operator SQL SELECT AND Operator SQL SELECT OR Operator SQL SELECT LIKE Operator SQL SELECT DISTINCT SQL SELECT SUM SQL SELECT MAX SQL SELECT MIN SQL SELECT AVG

SQL Clause

SQL WHERE Clause SQL GROUP BY CLAUSE SQL ORDER BY Clause SQL HAVING Clause

SQL INSERT

SQL INSERT Statement SQL INSERT INTO Statement SQL INSERT INTO SELECT

SQL JOIN

SQL JOIN SQL Inner Join SQL Left Join SQL Right Join SQL Full Join SQL CROSS Join

SQL OPERATOR

SQL Comparison SQL LOGICAL Operator SQL Cast Operator SQL Arithmetic

Difference

SQL vs NOSQL WHERE vs HAVING DELETE vs DROP GROUP BY vs ORDER BY DROP vs TRUNCATE SQL IN vs SQL EXISTS

MISC

SQL SubQuery SQL CASE Commit and Rollback in SQL Pattern Matching in SQL DDL Commands in SQL DML Commands in SQL Types of SQL Commands SQL COUNT SQL Primary Key SQL FOREIGN KEY SET Operators in SQL Check Constraint in SQL SQL EXCEPT SQL VIEW SQL WHERE Statement SQL CRUD Operation Where Condition in SQL TCL Commands in SQL Types of SQL JOINS SQL Nth Highest Salary SQL NOT OPERATOR SQL UNION ALL SQL INTERSECT SQL Data Definition Language SQL Data Manipulation Language SQL Data Control Language SQL CONSTRAINTS SQL Aggregate Operators SQL KEYS Codd’s Rules in SQL What is SQL Injection? Trigger In SQL SQL WHERE Multiple Conditions Truncate function in SQL SQL Formatter WEB SQL SQL Auto Increment Save Point in SQL space() function in SQL SQL Aggregate Functions SQL Topological Sorting SQL Injection SQL Cloning Tables SQL Aliases SQL Handling Duplicate

How To

How to use the BETWEEN operator in SQL How To Use INNER JOIN In SQL How to use LIKE in SQL How to use HAVING Clause in SQL How to use GROUP BY Clause in SQL How To Remove Duplicates In SQL How To Delete A Row In SQL How to add column in table in SQL ? How to drop a column in SQL? How to create a database in SQL? How to use COUNT in SQL? How to Create Temporary Table in SQL? How to Add Foreign Key in SQL? How to Add Comments in SQL? How To Use Group By Clause In SQL How To Use Having Clause In SQL How To Delete Column In Table How To Compare Date In SQL

SET Operators in SQL

The operator used to join or combine two queries is none other than SET operators. Operators categorized into SET operators are as follows:

  1. UNION Operator.
  2. UNION ALL’ Operator.
  3. INTERSECT Operator.
  4. MINUS Operator.

Rules to be followed to operate using SET Operator are as follows:

  • The number of Columns and Order of Columns must be the same.
  • Data Type must be Compatible.

Let’s understand each of the SET Operators with examples.

Consider the following tables along with the given records.

Table1: Employees

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERIDWORKING_HOURSGENDER
1001VAIBHAVIMISHRA65000PUNEORACLE112F
1002VAIBHAVSHARMA60000NOIDAORACLE19M
1003NIKHILVANI50000JAIPURFMW210M
2001PRACHISHARMA55500CHANDIGARHORACLE110F
2002BHAVESHJAIN65500PUNEFMW212M
2003RUCHIKAJAIN50000MUMBAITESTING49F
3001PRANOTISHENDE55500PUNEJAVA39F
3002ANUJAWHERE50500JAIPURFMW29F
3003DEEPAMJAUHARI58500MUMBAIJAVA312M
4001RAJESHGOUD60500MUMBAITESTING410M

Table2: Employee.

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERIDWORKING_HOURSGENDER
1001VaibhavSharma65000PUNEORACLE112M
1002NikhilVani60000NOIDAORACLE19M
1003VaibhaviMishra50000JAIPURFMW210F
2001RuchikaJain55500CHANDIGARHORACLE110F
2002PrachiSharma65500PUNEFMW212F
2003BhaveshJain50000MUMBAITESTING49M
3001DeepamJauhari55500PUNEJAVA39M
3002ANUJAWHERE50500JAIPURFMW29F
3003PranotiShende58500MUMBAIJAVA312F
4001RAJESHGOUD60500MUMBAITESTING410M

Table3: Manager.

Manageridmanager_namemanager_department
1Snehdeep KaurORACLE
2Kirti KirtaneFMW
3Abhishek ManishJAVA
4Anupam MishraTESTING

Table4: Manager1.

Manageridmanager_namemanager_department
1Ishita AgrawalORACLE
2Kirti KirtaneFMW
3Abhishek ManishJAVA
4Paul OakipTESTING

UNION Operator

UNION Operator is the First Operator used to merge or combine the two or more SELECT statements. If and only if their number of columns and order of columns are the same.

The duplicated rows will not be considered in the result obtained after the UNION operation is performed.

The syntax for UNION operation is as follows,

SELECT * FROM TABLE_NAME1 UNION SELECT * FROM TABLE_NAME2;

Example 1: Execute a query to perform UNION operation between Employees table and Employee table.

SELECT * FROM EMPLOYEES UNION SELECT * FROM EMPLOYEE;

In the above query, we have used two SELECT queries. The First SELECT query retrieves the data from employees and the Second SELECT query retrieves the data from employee data, and UNION operation is performed on both the SELECT queries.

UNION operation will discard duplicates rows between both the tables. The following output is shown as:

SET Operators in SQL

Both table records are displayed except duplicate records.

Example 2: Execute a query to perform UNION operation between Employees table and Employee table. But display only those employees’ records from employees table that belong to city Mumbai and from employee table only those employees’ records whose employee salary is greater than 50000 and less than 60000.

SELECT * FROM EMPLOYEES WHERE CITY = 'MUMBAI' UNION SELECT * FROM EMPLOYEE WHERE SALARY > 50000 AND SALARY < 60000;

In the above query, we have used two SELECT queries. The First SELECT query fetches only those records whose employee belongs to Mumbai city from the Employees table. And perform UNION operation with the second SELECT query fetches only those records whose employee salary is greater than 50000 but lesser than 60000 from the employee table. UNION operation will discard duplicates rows between both the tables.

The following output is shown as:

SET Operators in SQL

Both table records are displayed except duplicate records.

Example 3: Execute a query to perform UNION operation between Employees table and Employee table. We want only those records from the Employees table whose employee city belongs to 'Pune’ and ‘Jaipur’.

SELECT* FROM EMPLOYEES WHERE CITY IN ('PUNE', 'JAIPUR') UNION SELECT * FROM EMPLOYEE;

In the above query, we have used two SELECT queries. The first SELECT query fetches those records whose employees belong to 'Pune’ and ‘Jaipur' city from the Employees table. The UNION operation is performed with the records retrieved from the second SELECT query from the table employee.

UNION operation will discard duplicates rows between both the tables.  The following output is shown as:

SET Operators in SQL

As we all can see, the result first five records are those employees who belong to Pune or Jaipur city. And rest records are from the employee table, the second SELECT query.

Both table records are displayed except duplicate records.

Example 4: Execute a query to perform UNION operation between Employees table and Employee table. We want only those records from the Employee table whose employee the first name starts with V.

SELECT * FROM EMPLOYEES UNION SELECT * FROM EMPLOYEE WHERE FIRST_NAME LIKE 'V%';

In the above query, we have used two SELECT queries. The first SELECT query fetches all the records from the Employees table. The UNION operation is performed with the records retrieved from the second SELECT query from the table employee whose first name starts with 'V'.

UNION operation will discard duplicates rows between both the tables.  The following output is shown as:

SET Operators in SQL

As we all can first 10 results are from the Employees table, and the rest are from the Employee table whose employee the first name starts with 'V'.

Example 5: Execute a query to perform UNION operation between Manager table and Manager1 table.

SELECT * FROM MANAGER UNION SELECT * FROM MANAGER1;

In the above query, we have used two SELECT queries. The First SELECT query retrieves the data from the Manager and the Second SELECT query retrieves the data from manager1 data, and UNION operation is performed on both the SELECT queries.

UNION operation will discard duplicates rows between both the tables. The following output is shown as:

SET Operators in SQL

Both table records are displayed except duplicate records.

UNION ALL Operators

UNION ALL operator combines all the data from both the queries. In the UNION operator, duplicates records were not considered in the result, but UNION ALL considered duplicate records in the result obtained after the UNION ALL operation is performed.

The syntax for UNION ALL operation are as follows,

SELECT * FROM TABLE_NAME1 UNION ALL SELECT * FROM TABLE_NAME2;

Example 1: Execute a query to perform UNION ALL operation between Employees table and Employee table.

SELECT * FROM EMPLOYEES UNION ALL SELECT * FROM EMPLOYEE;

In the above query, we have used two SELECT queries. The First SELECT query fetches the data from employees and performs UNION ALL operation with the data fetched by the Second SELECT query retrieves the data from the employee table.

The following output is shown as:

SET Operators in SQL

All the records will get displayed from both the tables Employees and Employee, duplicates records also get displayed as we perform UNION ALL operation.

Example 2: Execute a query to perform UNION ALL operation between Employees table and Employee table. But display only those employees' records from employees table that belong to city Mumbai. From the employee table, only those employees' records whose employee salary is 60500 and city is ‘Mumbai’.

SELECT * FROM EMPLOYEES WHERE CITY = 'MUMBAI' UNION ALL SELECT * FROM EMPLOYEE WHERE SALARY = 60500 AND CITY = 'MUMBAI';

In the above query, we have used two SELECT queries. The First SELECT query fetches only those records whose employee belongs to Mumbai city from the Employees table. And perform UNION ALL operation with the second SELECT query fetches only those records whose employee salary is 60500 and city is ‘Mumbai’ from the employee table.

The following output is shown as:

SET Operators in SQL

Both table records are displayed, including duplicate records as we perform UNION ALL operations.

Example 3: Execute a query to perform UNION operation between Employees table and Employee table. We want only those records from the Employees table whose employee city belongs to 'Pune’, ‘Mumbai’ and ‘Jaipur’.

SELECT* FROM EMPLOYEES WHERE CITY IN ('PUNE', 'MUMBAI', 'JAIPUR') UNION ALL SELECT * FROM EMPLOYEE;

In the above query, we have used two SELECT queries. The first SELECT query fetches those records whose employees belong to 'Pune’, ‘Mumbai’ and ‘Jaipur' city from the Employees table. The UNION ALL operation is performed with the records retrieved from the second SELECT query from the table employee.

The following output is shown as:

SET Operators in SQL

Both table records are displayed, including duplicate records as we perform UNION ALL operations.

Example 4: Execute a query to perform UNION ALL operation between Manager table and Manager1 table.

SELECT * FROM MANAGER UNION ALL SELECT * FROM MANAGER1;

In the above query, we have used two SELECT queries. The First SELECT query fetches the data from Manager and performs UNION ALL operations with the data fetched by the Second SELECT query retrieves the data from the manager1 table.

The following output is shown as:

SET Operators in SQL

All the records will be displayed from both the tables Manager and Manager1; duplicate records are displayed as we perform UNION ALL operations.

INTERSECT Operators

Intersect operator is used to combining two or more SELECT statements, but it only displays the data similar to the SELECT statement.

The syntax for INTERSECT operation is as follows,

SELECT * FROM TABLE_NAME1 INTERSECT SELECT * FROM TABLE_NAME2;

Example 1: Execute a query to perform INTERSECT operation between Employees table and Employee table.

SELECT * FROM EMPLOYEES INTERSECT SELECT * FROM EMPLOYEE;

In the above query, we have used two SELECT queries. The First SELECT query fetches the data from employees and performs INTERSECT operation with the data fetched by the Second SELECT query retrieves the data from the employee table.

The following output is shown as:

SET Operators in SQL

Only similar records will get displayed from both the tables, as we performed INTERSECT operation between Employees table and Employee table.

Example 2: Execute a query to perform INTERSECT operation between Manager table and Manager1 table.

SELECT * FROM MANAGER INTERSECT SELECT * FROM MANAGER1;

In the above query, we have used two SELECT queries. The First SELECT query fetches the data from Manager and performs INTERSECT operation with the data fetched by the Second SELECT query retrieves the data from Manager1 table.

The following output is shown as:

SET Operators in SQL

Only similar records will get displayed from both the tables, as we performed INTERSECT operation between Manager table and Manager1 table.

MINUS Operators

MINUS operator is used to returning the rows present in the first query but absent in the rest of the queries with no duplicates.

The syntax for MINUS operation is as follows.

SELECT * FROM TABLE_NAME1 EXCEPT SELECT * FROM TABLE_NAME2;

Note: MINUS keyword is supported only in ORACLE databases. We can use EXCEPT keyword for other databases to perform a similar operation.

Example 1: Execute a query to perform MINUS operation between Employees table and Employee table.

SELECT * FROM EMPLOYEES EXCEPT SELECT * FROM EMPLOYEE;

In the above query, we have used two SELECT queries. The First SELECT query fetches the data from employees and performs MINUS operation with the data fetched by the Second SELECT query retrieves the data from the employee table.

The following output is shown as:

SET Operators in SQL

Only unmatched records will get displayed from both the tables, as we performed MINUS operation between Employees table and Employee table.

Example 2: Execute a query to perform MINUS operation between Manager table and Manager1 table.

SELECT * FROM MANAGER EXCEPT SELECT * FROM MANAGER1;

In the above query, we have used two SELECT queries. The First SELECT query fetches the data from Manager and performs MINUS operation with the data fetched by the Second SELECT query retrieves the data from Manager1 table.

The following output is shown as:

SET Operators in SQL

Only unmatched records will get displayed from both the tables, as we performed MINUS operation between Manager table and Manager1 table.



ADVERTISEMENT
ADVERTISEMENT