# SQL SET OPERATORS: UNION, INTERSECT & MINUS

> It is used to combine the results of two or more select statements.

## SET OPERATOR TYPES :

1.  Union
    
2.  Union All
    
3.  Intersect
    
4.  Minus
    

* * *

## 1\. UNION

Union operators return only unique values from the tables.

```sql
-- SYNTAX

SELECT column_name
FROM table1
UNION
SELECT column_name
FROM table2;

-- EXAMPLE

SELECT country FROM asia
UNION
SELECT country FROM europe;

-- HERE WE COMBINED THE COUNTRY NAMES FROM BOTH TABLES AND REMOVED DUPLICATE RECORDS.
```

* * *

## 2\. UNION ALL

Union all returns both unique and duplicate values from the tables.

```sql
-- SYNTAX

SELECT column_name
FROM table1
UNION ALL
SELECT column_name
FROM table2;

-- EXAMPLE

SELECT id, city FROM department
UNION ALL
SELECT id, city FROM company;

-- HERE WE COMBINED THE ID AND CITY FROM BOTH TABLES WITHOUT REMOVING DUPLICATE RECORDS.
```

* * *

## 3\. INTERSECT

Intersect returns only common value from both tables.

```sql
-- SYNTAX

SELECT column_name
FROM table1
INTERSECT
SELECT column_name
FROM table2;


-- EXAMPLE

SELECT Name FROM department
INTERSECT
SELECT Name FROM company;

-- HERE WE DISPLAYED THE COMMON NAMES FROM BOTH TABLES.
```

* * *

## 4\. MINUS

Minus returns the rows which are present in first table but absent in the second table with no duplicates & ascending order.

```sql
-- SYNTAX

SELECT column_name
FROM table1
MINUS
SELECT column_name
FROM table2;

-- EXAMPLE

SELECT NAME, AGE, GRADE
FROM school
MINUS
SELECT NAME, AGE, GRADE
FROM college;

-- HERE WE DISPLAYED THE RECORDS FROM THE SCHOOL TABLE THAT ARE NOT PRESENT IN THE COLLEGE TABLE.
```
