XII L Notes 4 SQL Joins& Set 17-04-2021 Printing
XII L Notes 4 SQL Joins& Set 17-04-2021 Printing
INFORMATICS PRACTICES
An SQL JOIN clauseis used to combine rows from two or more tables based on
a common field between them. While querying for a join, more than one table
is considered in FROM clause. The process/function of combining data from
multiple tables is called a JOIN.
SQL can extract data from two or even more than two related tables by
performing either a physical or virtual join on the tables using WHERE clause.
1. Cartesian Product (Cross Product) 2. Equi Join 3. Inner Join 4. Outer Join
Table : emp
Table: Dept
BIRLA PUBLIC SCHOOL, DOHA-QATAR
INFORMATICS PRACTICES
Natural Join
The join in which only one of the identical columns ( coming from joined
tables) exists is called Natural Join.
BIRLA PUBLIC SCHOOL, DOHA-QATAR
INFORMATICS PRACTICES
UNION OPERATOR
The UNION operator is used to combine the result-set of two or more SELECT
statements.
The Union operation is used to return all the distinct rows selected by either
query.
For executing Union between two tables, the number of columns selected
from each table should be the same. Also, the datatypes of the corresponding
columns selected from each table should be the same.
OR
Combined unique entries of two tables.
Example:
BIRLA PUBLIC SCHOOL, DOHA-QATAR
INFORMATICS PRACTICES
UNION ALL
The Union operation is used to return all the rows selected by either query.
MINUS OPERATOR
The MINUS compares the results of two queries and returns distinct rows from
the result set of the first query that does not appear in the result set of the
second query.
syntax of the MINUS operator:
SELECT select_list1
FROM table_name1
BIRLA PUBLIC SCHOOL, DOHA-QATAR
INFORMATICS PRACTICES
SELECT id FROM t1
MINUS
SELECT id FROM t2;
INFORMATICS PRACTICES
INTERSECT OPERATOR
The INTERSECT operator is a set operator that returns only distinct rows of two
queries or more queries.
Syntax of the INTERSECT operator.
(SELECT column_list
FROM table_1)
INTERSECT
(SELECT column_list
FROM table_2);
The INTERSECT operator compares the result sets of two queries and returns
the distinct rows that are output by both queries.
To use the INTERSECT operator for two queries, you follow these rules:
1. The order and the number of columns in the select list of the queries
must be the same.
2. The data types of the corresponding columns must be compatible.
INFORMATICS PRACTICES
Questions
1. If there are two tables emp1 and emp2, and both have common records,
how can we fetch all the records but common records only once?
Ans. (select * from emp) union (select * from emp1);
2. How to fetch only common records from two tables emp and emp1?
Ans. (select * from emp) intersect (select * from emp1);
3. How can we retrieve all records of emp1 that are not present in emp2?
Ans. (select * from emp) minus (select * from emp1);
********************************************************