0% found this document useful (0 votes)
28 views

XII L Notes 4 SQL Joins& Set 17-04-2021 Printing

Uploaded by

shwetha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

XII L Notes 4 SQL Joins& Set 17-04-2021 Printing

Uploaded by

shwetha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

BIRLA PUBLIC SCHOOL, DOHA-QATAR

INFORMATICS PRACTICES

JOINS & SET OPERATIONS( Printing)

GRADE: XII 17-04-2021


SQL JOINS

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.

The types of SQL joins are as follows:

1. Cartesian Product (Cross Product) 2. Equi Join 3. Inner Join 4. Outer Join

5. Self Join 6. Non-Equi Join 7. Natural Join

Cartesian Product (Cross Product)


The Cartesian product is also termed as cross product or cross-join. The
Cartesian product is a binary operation and is denoted by (X). The degree of
the new relation formed is the sum of the degrees of two relations on which
Cartesian product is performed. The number of tuples in the new relation is
equal to the product of the number of tuples of the two tables on which
Cartesian product is performed.

Table : emp

Table: Dept
BIRLA PUBLIC SCHOOL, DOHA-QATAR

INFORMATICS PRACTICES

JOINS & SET OPERATIONS( Printing)

GRADE: XII 17-04-2021

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

JOINS & SET OPERATIONS( Printing)

GRADE: XII 17-04-2021


Equi Join
The join in which columns are compared for equality is called Equijoin.
An Equi join is a simple SQL join condition that uses the equal to sign (=) as a
comparison operator for defining a relationship between two tables on the
basis of a common field.
Syntax for Equi join: select <column1>, <column2>,....
from <table1>, <table2>
where <table1.column1> = <table2.column2>;
Equijoin

SET operations on Relations

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

JOINS & SET OPERATIONS( Printing)

GRADE: XII 17-04-2021

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

JOINS & SET OPERATIONS( Printing)

GRADE: XII 17-04-2021


MINUS
SELECT select_list2
FROM table_name2;

SELECT id FROM t1
MINUS
SELECT id FROM t2;

MySQL doesnot support minus.


Simulation can be done usng the query below

How to work on left join


BIRLA PUBLIC SCHOOL, DOHA-QATAR

INFORMATICS PRACTICES

JOINS & SET OPERATIONS( Printing)

GRADE: XII 17-04-2021

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.

The following diagram illustrates the INTERSECT operator.

MySQL doesnot support Intersect


BIRLA PUBLIC SCHOOL, DOHA-QATAR

INFORMATICS PRACTICES

JOINS & SET OPERATIONS( Printing)

GRADE: XII 17-04-2021


Simulation can be done usng the query below

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);

********************************************************

You might also like