1. A join is a special type of cross product that combines rows from two tables based on a related column between them.
2. There are several types of joins including inner joins, outer joins, self joins, and natural joins. Inner joins return rows where the join condition is met, while outer joins return all rows of one or both tables even if the condition is not met.
3. Specific types of joins include equijoins/inner joins which return rows with matching values between tables, left and right outer joins which return all rows of the left or right table respectively along with matched rows, and full outer joins which return all rows from both tables.
Presiding Officer Training module 2024 lok sabha elections
Database Joins Explained
1. Topic: Joins in Database
Under the guidance of
Dr. Sheelesh kumar Sharma
Submitted to:- Submitted by:-
MCA Department Shashank Pathak
IMS Ghaziabad MCA 3rd Year
2. Joins
1. Join is a special form of cross product of two tables.
Join=(Cross Product + Select Statement(Conditions)
2. In Cartesian product we join a tuple of one table with the tuples of the
second table. But in join there is a special requirement of relationship
between tuples.
For example if there is a relation STUDENT and a relation BOOK then it
may be required to know that how many books have been issued to any
particular student. Now in this case the primary key of STUDENT that is
stId is a foreign key in BOOK table through which the join can be made.
3. Types
1. Cross Join.
2. Natural Join.
3. Equi(Inner Join).
4. Self Join.
5. Outer Join-
• Left Outer Join.
• Right Outer Join.
• Full Outer Join.
4. Eno Ename Address
1 Kiran Delhi
2 Sita Banaras
3 Amrit Mohali
4 Veeru Delhi
Dno Dname Eno
D1 HR 1
D2 IT 2
D3 Sales 4
D4 Finance 3
Tables
Department TableEmployee Table
5. Cross Join
1. Cross join where there is no clause specified.
2. In mathematics, it is a set of all pairs of elements (x, y) that can be
constructed from given sets, X and Y, such that x belongs to X and y to Y.
3. It defines a relation that is the concatenation of every tuple of relation R
with every tuple of relation S.
Query:
SQL>select * from employee,department;
OR
SQL>select * from employee cross join department;
6. Natural Join
• Natural Join joins two tables based on same attribute.
• The resulting table will contains all the attribute of both table but keep
only one each common fields.
• Natural join is denoted by binary operator( ) , written as (A B)
where A and B are relations.
• In particular, natural join allows the combination of relations that are
associated by a foreign key.
Normal Query:-
SQL>select * from employee where employee, department where emp.eno=dept.eno;
Joins:
SQL>select * from employee natural join department;
7. Natural Join(Example)
Eno Ename Address Deptno Dname
1 Kiran Delhi D1 HR
2 Sita Banaras D2 IT
4 Veeru Delhi D3 Sales
3 Amrit Mohali D4 Finance
8. Equi Join/Inner Join
1. Inner join is used to select all records from table A, B where the join
condition is meet.
2. An inner join is the most common join operation used in applications and
can be regarded as the default join-type.
3. Inner join create a new result table by combinning column values of two
table A,B based upon the join-preducate.
4. The query compares each row of A with each row of B to find all pairs of
rows which satisfy the join-predicate. When the join-statement is
satisfied, column values for each matched pair of rows of A and B are
combined into a result row.
10. Self Join
1. A self-join is joining a table to itself. This is best illustrated by an
example.
2. A query to find all pairings of two employees in the same country is
desired.
3. If there were two separate tables for employees and a query which
requested employees in the first table having the same country as
employees in the second table, a normal join operation could be used to
find the answer table. However, all the employee information is contained
within a single large table.
12. Outer Join
1. If there are any values in one table and that is not present in another table,
in an inner join/equi join that row will not be selected. Such row can be
forcefully selected by using the outer join symbol(+).
2. Example:
SQL> select * from employee e, department d where e.eno(+)=d.eno;
3. Outer join further divided into three parts:
• Left Outer join.
• Right Outer join.
• Full Outer join.
13. Left Outer Join
1. The result of Left outer join(or simple left join) for table A and B always
contains all records of the “left table(A)”, even if the join-condition does
not find any matching record in the “right table(B)”
2. This means that if the ON clause matches 0(zero) records in B, the join will
still return a row in the result but with NULL in each column from B.
3. This means that a left outer join returns all the values from the left table,
plus matched values from the right table (or NULL in case of no matching
join predicate).
14. 4. If the right table returns one row and the left table returns more than one
matching row for it, the values in the right table will be repeated for each
distinct row on the left table.
Query:-
SQL>select e.eno,e.ename from employee e left outer join department d on
e.eno=d.eno;
Eno Ename Address Dno Dname Eno
1 Kiran Delhi D1 HR 1
2 Sita Banaras D2 IT 2
3 Amrit Mohali D4 Finance 3
4 Veeru Delhi D3 Sales 4
15. Right Outer Join
1. A right outer join closely resembles a left outer join except with the
treatment of the tables reserved. Every row from the “right table”.
2. B will appear in the joined table at least once. If no matching row from
the ''left” table(A) exists, NULL will appear in cloumns from table A
for those recprds that have no match in B. A right outer join returns all
the values from the right table and matched values from the left
table(NULL in case of no matching join predicate).
16. Query:-
SQL>select * from employee e right outer join department d on e.eno=d.eno;
Eno Ename Address Dno Dname Eno
1 Kiran Delhi D1 HR 1
2 Sita Banaras D2 IT 2
4 Veeru Delhi D3 Sales 4
3 Amrit Mohali D4 Finance 3
17. Full Outer Join
1. A full outer join combines the effect of applying both left and right outer
joins. Where records in the full outer joined table that do not matched, the
result set will have NULL values for every column of the table that have
lacks matching row. For those records that do match, a single row will be
produced in the result set(containing fields populated from both tables).
2. Select all records from table(A) and table(B) regardless of whether the
join condition is meet or not.
18. Query:
SQL> select * from employee e full outer join department d on e.eno=d.eno;
Eno Ename Address Dno Dname Eno
1 Kiran Delhi D1 HR 1
2 Sita Banaras D2 IT 2
4 Veeru Delhi D3 Sales 4
3 Amrit Mohali D4 Finance 3