Set-Operators-in-SQL
Set-Operators-in-SQL
in SQL
SQL Join
A JOIN clause is used to combine
rows from two or more tables,
based on a related column
between them.
Table 1 − Orders Table
10308 2 1996-09-18
10309 37 1996-09-19
10310 77 1996-09-20
Table 2 − Customers Table
Example:
Base on the two tables, what would be the result of this SQL command?
The result will be:
UNION
Base on the two tables above, What would be the output or result of this command?
This would produce the following result.
+------+----------+--------+---------------------+
| ID | NAME | AMOUNT | DATE |
+------+----------+--------+---------------------+
| 1 | Ramesh | NULL | NULL |
| 2 | Khilan | 1560 | 2009-11-20 00:00:00 |
| 3 | kaushik | 3000 | 2009-10-08 00:00:00 |
| 3 | kaushik | 1500 | 2009-10-08 00:00:00 |
| 4 | Chaitali | 2060 | 2008-05-20 00:00:00 |
| 5 | Hardik | NULL | NULL |
| 6 | Komal | NULL | NULL |
| 7 | Muffy | NULL | NULL |
+------+----------+--------+---------------------+
Union All
UNION ALL operator is used
to combine the results of two
SELECT statements including
duplicate rows.
UNION ALL
Base on the two tables above, What would be the output or result of this command?
This would produce the following result.
+------+----------+--------+---------------------+
| ID | NAME | AMOUNT | DATE |
+------+----------+--------+---------------------+
| 1 | Ramesh | NULL | NULL |
| 2 | Khilan | 1560 | 2009-11-20 00:00:00 |
| 3 | kaushik | 1500 | 2009-10-08 00:00:00 |
| 4 | Chaitali | 2060 | 2008-05-20 00:00:00 |
| 5 | Hardik | NULL | NULL |
| 6 | Komal | NULL | NULL |
| 7 | Muffy | NULL | NULL |
| 3 | kaushik | 1500 | 2009-10-08 00:00:00 |
| 2 | Khilan | 1560 | 2009-11-20 00:00:00 |
| 4 | Chaitali | 2060 | 2008-05-20 00:00:00 |
+------+----------+--------+---------------------+
There are two other clauses or
operators, which are like the
UNION clause.
Intersect Clause
The SQL INTERSECT clause/operator is
used to combine two SELECT statements, but
returns rows only from the first SELECT statement
that are identical to a row in the second SELECT
statement. This means INTERSECT returns only
common rows returned by the two SELECT
statements.
Here, the given condition could be any given expression based on your requirement.
For the same dataset from the aforementioned example, the intersect operator
output is given below
Table 1 − CUSTOMERS Table is as follows
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
Table 2 − ORDERS Table is as follows.
+-----+---------------------+-------------+--------+
|OID | DATE | CUSTOMER_ID | AMOUNT |
+-----+---------------------+-------------+--------+
| 102 | 2009-10-08 00:00:00 | 3 | 3000 |
| 100 | 2009-10-08 00:00:00 | 3 | 1500 |
| 101 | 2009-11-20 00:00:00 | 2 | 1560 |
| 103 | 2008-05-20 00:00:00 | 4 | 2060 |
+-----+---------------------+-------------+--------+
Considering the following two tables.
Now, let us join these two tables in our SELECT statement as follows.
EXCEPT
Here, the given condition could be any given expression based on your requirement.
For the same dataset from the aforementioned example, the Except operator output is
given below
Considering the two tables above.
Now, let us join these two tables in our SELECT statement as follows.