0% found this document useful (0 votes)
5 views39 pages

Slides 2

The document outlines various operations on relations in SQL, including Cartesian Product, Union, Intersection, and Minus, detailing their definitions and conditions. It also explains different types of joins such as Inner Join, Outer Join, Cross Join, Equi Join, Natural Join, and Non-equi Join, providing examples and syntax for each. Additionally, it discusses the use of qualified names and aliases for fields and tables to enhance query readability.

Uploaded by

domaininitial4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views39 pages

Slides 2

The document outlines various operations on relations in SQL, including Cartesian Product, Union, Intersection, and Minus, detailing their definitions and conditions. It also explains different types of joins such as Inner Join, Outer Join, Cross Join, Equi Join, Natural Join, and Non-equi Join, providing examples and syntax for each. Additionally, it discusses the use of qualified names and aliases for fields and tables to enhance query readability.

Uploaded by

domaininitial4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

CONTENTS

( Learning outcomes )
Operations on Relations (Revised syllabus)
• Cartesian Product (revised syllabus)
• Union (revised syllabus)
• Intersection (revised syllabus)
• Minus (revised syllabus)
Types of Join (Revised syllabus)
• Inner Join
• Outer Join (LEFT Join, RIGHT Join, FULL Join)
• Cross Join / Cartesian Product (revised syllabus)
• Equi Join (revised syllabus)
• Natural Join (revised syllabus)
• Non-equi Join
• Additional search conditions in Joins
**NOTE:- MySQL queries are not case-sensitive
OPERATIONS ON RELATIONS: CARTESIAN PRODUCT
INDEX The Cartesian Product is a binary operation and is denoted by
Operation on relations a cross (x). The Cartesian Product of two relations A and B
• Cartesian can be denoted as A x B . The Cartesian Product of the two
relations produces a relation with all possible combinations
Product of the tuples of the two relations operated upon.
• Union The Cartesian Product yields a new relation which has:
• Intersection Degree of A x B = Degree of A + Degree of B
• Minus Cardinality of A x B =Cardinality of A * Cardinality of B
SUBJECT table with Degree=3, Cardinality=3 TEACHER table with Degree=4, Cardinality=5

NOTE:
Resultant table= SUBJECT x TEACHER with Degree=Total no. of
Degree=3+4=7, attributes/ fields/columns
Cardinality=3*5=15 Cardinality=Total no. of
Tuples/ Records/rows
CROSS Join / CARTESIAN PRODUCT/ UNRESTRICTED Join contd..

For Cartesian product of Table1 and Table2


The resultant table’s cardinality= Cardinality of Table1 * Cardinality of Table2
The resultant table’s degree= Degree of Table1 + Degree of Table2
OPERATIONS ON RELATIONS: UNION
INDEX The Union operation is a binary operation that requires two relations
Operation on as its operand to produce a third resultant relation that contains
relations tuples from both the operand relations. It is denoted by (U). Also
• Cartesian known as Set Union Operation.
Product The Union of two relations A and B can be denoted as A U B .
• Union Union operation must follow two conditions:
i) The operand relations (A and B) must be of the same degree.
• Intersection ii) The domains of the ith attribute of A and the ith attribute of B
• Minus must be the same.
SUBJECT Duplicate tuples in A and B are shown once in the resultant table.
SUBNO SUB_NAME MAX_MRK
1 ENG 100
SUBJECT U PRACTICAL
SUBNO SUB_NAME MAX_MRK
2 MATHS 100
1 ENG 100
3 SCIENCE 70
2 MATHS 100
PRACTICAL 3 SCIENCE 70
SUBNO SUB_NAME MAX_MRK 4 SUPW 50
3 SCIENCE 70
NOTE: SCIENCE is duplicate in both the tables.
4 SUPW 50 Hence, it has come once in the resultant table.
OPERATIONS ON RELATIONS: INTERSECTION
INDEX The Intersection operation is a binary operation that requires
Operation on two relations as its operand to produce a third resultant
relations relation which contains tuples that are common to both the
• Cartesian operand relations. It is denoted by ( ). Also known as Set
Product Intersection Operator. The Intersection of two relations A and B
• Union can be denoted as A B .
• Intersection Intersection operation must follow the same conditions as given
• Minus for Union operation.
Only Common tuples in A and B are shown in the resultant
table.
SUBJECT
SUBNO SUB_NAME MAX_MRK
1 ENG 100
2 MATHS 100 SUBJECT PRACTICAL
3 SCIENCE 70 SUBNO SUB_NAME MAX_MRK
3 SCIENCE 70
PRACTICAL
SUBNO SUB_NAME MAX_MRK NOTE: SCIENCE is duplicate(common) in both the
3 SCIENCE 70 tables. Hence, it has come as output in the
4 SUPW 50 resultant table.
OPERATIONS ON RELATIONS: MINUS
INDEX The Minus operation is a binary operation that requires two relations
Operation on as its operand to produce a third resultant relation which allows us
relations to find tuples that are in one relation but not in another. It is
• Cartesian denoted by ( ). Also known as Set Difference Operator.
Product The Difference of two relations A and B can be denoted as A - B .
• Union Minus operation must follow the same conditions as given for Union.
• Intersection A-B results in a relation which contains tuples present in A but not
• Minus in B.
B-A results in a relation which contains tuples present in B but not
in A
SUBJECT
SUBNO SUB_NAME MAX_MRK
1 ENG 100
-
SUBJECT PRACTICAL
SUBNO SUB_NAME MAX_MRK
2 MATHS 100
1 ENG 100
3 SCIENCE 70
2 MATHS 100
PRACTICAL
SUBNO SUB_NAME MAX_MRK -
PRACTICAL SUBJECT
3 SCIENCE 70 SUBNO SUB_NAME MAX_MRK
4 SUPW 50 4 SUPW 50
**NOTE:- MySQL queries are not case-sensitive
QUALIFIED Names for Fields
• Qualifying a column name uniquely identifies it from similar columns in other
tables.
• Qualified field names are very useful in identifying a field if the two Joining tables
have fields with same name.
• If the tables do not have same name for the columns then using table names as
qualifiers in the SELECT statement is optional.
SYNTAX: <tablename>.<fieldname>
For Ex: Subject.sub_no OR Teacher.sub_no

ALIAS Names for Tables


COLUMN ALIASES are used to make column headings in your result set
easier to read.
TABLE ALIASES are used to shorten your SQL to make it easier to read or
when you are performing a self join (ie: listing the same table more than
once in the FROM clause). NOTE: Assume Employee and Department
Eg. Select * are the two tables having deptno as the
From Employee E, Department D common field. E and D are the table alias
Where E.deptno=D.deptno; names given in the query respectively.
CONCEPT OF JOINS ON RELATIONS
• Concept of Joins A Join is a query that combines rows
from two or more tables. In a Join-query,
on relations
more than one table are written in the
• Types of Joins
FROM clause. So, we can say that Joining
• Inner Join
is the function of combining data from
• Outer Join
(LEFT Join, RIGHT Join,
multiple relations. Join Condition
FULL Join)
expression with WHERE clause is the
• Cross Join / Cartesian
Join Predicate
Product SYNTAX:
• Equi Join SELECT <field list>
• Natural Join FROM <table1>,<table2> [,<table3>…]
• Non-equi Join
[WHERE <Join condition for the tables>];
• Additional search
conditions in Joins
+ =
TYPES OF JOIN

INNER OUTER CROSS NATURAL NON-


EQUI Join
Join Join Join Join EQUI Join

LEFT OUTER FULL OUTER RIGHT OUTER


Join / Join / Join /
LEFT Join FULL Join RIGHT Join
Let us consider Three tables namely, SUBJECT, TEACHER, SALGRADE
INNER JOIN
• CONCEPT OF JOINS
• TYPES OF JOIN • The INNER JOIN creates a new result

•Inner Join
table by combining column values of
two tables (table1 and table2) based
• Outer Join upon the join-predicate. The query
(LEFT Join, RIGHT Join,
compares each row of table1 with
FULL Join)
each row of table2 to find all pairs of
rows which satisfy the join-predicate
• Cross Join
(Join condition)
• Equi Join
• Natural Join
• Non-equi Join
Table 1 Table 2
• Additional search
conditions in Joins
INNER JOIN Example code
OUTER JOIN
• CONCEPT OF JOINS
In the OUTER JOIN all the
• TYPES OF JOIN
• Inner Join
content of the both tables are
integrated together either they
• Outer Join are matched or not.
(LEFT Join, RIGHT
TYPES OF OUTER JOINS
Join, FULL Join)
• Cross Join
LEFT OUTER JOIN / LEFT JOIN
• Equi Join RIGHT OUTER JOIN / RIGHT JOIN
• Natural Join FULL OUTER JOIN / FULL JOIN
• Non-equi Join
• Additional search ON keyword is used in the join
conditions in Joins predicate.
OUTER JOIN contd…
OUTER JOINS Description

LEFT OUTER JOIN returns all the rows from left table
or combined with the matching rows of
LEFT JOIN the right table. If you get no
matching in the right table it returns
Table L Table R
NULL values.

RIGHT OUTER returns all the rows from right table


JOIN combined with the matching rows of
or left table .If you get no column Table L Table R
matching in the left table .it returns
RIGHT JOIN
NULL value.

FULL OUTER JOIN result of combination of both left


or and right outer join and the join
tables have all the records from Table L Table R
FULL JOIN
both tables. It puts NULL on the
place of matches not found.
LEFT OUTER JOIN / LEFT JOIN

LEFT
LEFT OUTER JOIN / LEFT JOIN

LEFT
RIGHT OUTER JOIN / RIGHT JOIN

RIGHT
RIGHT OUTER JOIN / RIGHT JOIN

RIGHT
FULL OUTER JOIN / FULL JOIN
CROSS Join / CARTESIAN PRODUCT/
UNRESTRICTED Join contd..
• CONCEPT OF JOINS
• TYPES OF JOIN • Cross Join is used to return the Cartesian
• Inner Join product of two tables.
• Outer Join
• Unrestricted Join or Cartesian product of
both the tables gives all possible
(LEFT Join, RIGHT Join,
concatenations of all the rows of both the
FULL Join) tables.

•Cross Join • No filter condition is specified in the


query that implies the Join is unrestricted.
• Equi Join
• Natural Join TABLE A TABLE B
• Non-equi Join
1 1
• Additional search
2 2
3 3
conditions in Joins
CROSS Join / CARTESIAN PRODUCT/ UNRESTRICTED Join
contd..
• CONCEPT OF JOINS
• TYPES OF JOIN
• Inner Join
SYNTAX: EXAMPLE:
• Outer Join SELECT <select_list>
(LEFT Join, RIGHT Join, FROM table1,table2;
FULL Join)
SELECT <select_list>
• Cross Join FROM table1 CROSS
Join table2;
• Equi Join
• Natural Join
• Non-equi Join ON Clause is not used only in
• Additional search Cross Join among all other joins.
conditions in Joins
CROSS Join / CARTESIAN PRODUCT/ UNRESTRICTED Join contd..

For Cartesian product of Table1 and Table2


The resultant table’s cardinality= Cardinality of Table1 * Cardinality of Table2
The resultant table’s degree= Degree of Table1 + Degree of Table2
EQUI JOIN
• CONCEPT OF JOINS
The join in which columns are
• TYPES OF JOIN
compared for equality (=), is
• Inner Join
• Outer Join
called Equi Join. This join
(LEFT Join, RIGHT Join,
combines tables based on
FULL Join)
matching values in specified
• Cross Join columns in the join condition

•Equi Join For Eg.


• Natural Join
Select *
From Subject,Teacher
• Non-equi Join
Where Subject.sub_no=Teacher.sub_no;
• Additional search
conditions in Joins
equi Join example

NOTE: Common field with common values are coming twice

NOTE: Common field with common values are coming twice


NATURAL Join
• CONCEPT OF JOINS
• The Join in which only one of the
• TYPES OF JOIN
identical columns (coming from
• Inner Join
Joined tables) exists, is called
• Outer Join NATURAL Join.
(LEFT Join, RIGHT Join,
FULL Join)
• A Natural Join is a type of Equi-Join
• Cross Join
where the Join predicate arises
• Equi Join
implicitly by comparing all columns in
• Natural Join both tables that have the same
column name.
• Non-equi Join
• Additional search
conditions in Joins
NATURAL Join Examples contd…
SYNTAX EXAMPLE
SELECT Table1.<column_list>, SELECT Teacher.*,Sub_name,Medium
Table2.<column_list without the identical FROM Teacher,Subject
column>
FROM table1,table2 WHERE
WHERE Teacher.sub_no=Subject.sub_no;
table1.column_name=table2.column_name;
SELECT * SELECT *
FROM table1 NATURAL Join table2; FROM Teacher NATURAL JOIN
OR Subject;

SELECT *
FROM table1 NATURAL Join table2 SELECT *
WHERE
table1.column_name=table2.column_name;
FROM Teacher NATURAL JOIN Subject
WHERE
Teacher.sub_no=Subject.sub_no;
SELECT <column_list> SELECT *
FROM table1 Join table2 FROM Subject Join Teacher
USING (identical_column_name);
USING(sub_no);
NATURAL JOIN EXAMPLES…

NOTE: Common field with common values are coming once.

NOTE: Common field with common values are coming once.


NATURAL JOIN EXAMPLES contd…

NOTE: Common field with common values are coming once.

NOTE: Common field with common values are coming once.


NON-EQUI Join
• CONCEPT OF JOINS
A Non-equi –join is a
• TYPES OF JOIN
• Inner Join query that specifies
• Outer Join
(LEFT Join, RIGHT Join,
some relationship
FULL Join) other than equality
• Cross Join
• Equi Join
between the columns.
• Natural Join

• Non-equi Join
• Additional search
conditions in Joins
Non-Equi-Join condition
ADDITIONAL SEARCH CONDITIONS in JOINSc

JOIN condition on
Sub_no
ADDITIONAL SEARCH CONDITIONS in JOINS

Table Aliases, S, T
JOIN condition on Sub_no

Nested Order by
Stay safe. Stay aware. Stay healthy. Stay alert.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy