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

SQL Joins

Cartesian products combine every row of one table with every row of another table. Inner joins match and combine rows between tables based on equality conditions, while outer joins also return non-matching rows and fill them with null values. The main join types in SQL are inner, left, right, full, and natural joins.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

SQL Joins

Cartesian products combine every row of one table with every row of another table. Inner joins match and combine rows between tables based on equality conditions, while outer joins also return non-matching rows and fill them with null values. The main join types in SQL are inner, left, right, full, and natural joins.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

SQL

Cartesian product (X)/cross joint


Cartesian Product is denoted by X symbol.
Lets say we have two relations R1 and R2
then the cartesian product of these two
relations (R1 X R2) would combine each tuple
of first relation R1 with the each tuple of
second relation R2.
SQL
Cartesian product (X) example Mysql query –
Table a and Table b as shown Select * from a,b;
below
Select * from a cross join b;

Degree of cartesion product is 3 and cardinality is 4=(2 rows of a X 2 rows of b)


SQL
Join – Join is used to fetch data from two or more
tables, which is joined to appear as single set of data.
It is used for combining column from two or more
tables by using values common to both tables.

Types of JOIN
Following are the types of JOIN that we can use in
SQL:
• Inner
• Outer
• Left
• Right
SQL

INNER Join or EQUI Join⋈


This is a simple JOIN in which the result is
based on matched data as per the equality
condition specified in the SQL query.
SQL
INNER Join or EQUI Join example Mysql query –
Table a and Table b as shown below Select course.student_name from
couse , student where
course.student_name=student.stude
nt_name;

Select a.name from a inner join b


where a.name=b.name;
SQL
Natural JOIN(⋈)
Natural Join is a type of Inner join which is based on column having same
name and same datatype present in both the tables to be joined.E.g.
Select * from a natural join b;
SQL
LEFT Outer Join
The left outer join returns a resultset table with the matched data from the
two tables and then the remaining rows of the left table and null from the
right table's columns. E.g.
Mysql query –
Select * from a left outer join b on
(a.name=b.name);
SQL
RIGHT Outer Join
The right outer join returns a resultset table with the matched data from the
two tables being joined, then the remaining rows of the right table and null
for the remaining left table's columns.E.g.
Mysql query –
Select * from a right outer join b on
(a.name=b.name);
SQL
Full Outer Join
The full outer join returns a resultset table with the matched data of two
table then remaining rows of both left table and then the right table.E.g.

Mysql query –
Select * from a left outer join b on
(a.name=b.name) union Select * from
a right outer join b on
(a.name=b.name) ;

You might also like