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

Table Joins and Indexes in SQL: Class - 12

This document discusses table joins and indexes in SQL. It introduces different types of joins - equi-join, natural join, non-equi join, left join, and right join. It explains how to combine data from multiple tables using joins on common fields. It also discusses using table aliases and indexes to facilitate database processing and information retrieval from joined tables.

Uploaded by

Not so Serious
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Table Joins and Indexes in SQL: Class - 12

This document discusses table joins and indexes in SQL. It introduces different types of joins - equi-join, natural join, non-equi join, left join, and right join. It explains how to combine data from multiple tables using joins on common fields. It also discusses using table aliases and indexes to facilitate database processing and information retrieval from joined tables.

Uploaded by

Not so Serious
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Table Joins and Indexes in SQL

Based on CBSE Curriculum


Class -12
Introduction
• Sometimes we need an information which is
receiving data from multiple tables.
• If the tables have some common field then it
become easier to join these tables and can
retrieve the data.
• In this chapter, we will learn joining of tables
and the process of information retrieval.
• In this chapter we will also learn indexes of SQL
which facilitates database processing.
JOINS
• Join is a query which combine rows of two or more
tables.
• In a join-query, we need to provide a list of tables in
FROM Clause.
• The process of combining multiple tables in order to
retrieve data is called joining. For ex-

SELECT * FROM emp1, dept;

• Unrestricted join or cartesian product of both the tables


gives all possible concatenations of all the rows of both
the tables.
Making Table ready for join

In above given example there are two tables namely emp1 and dept.
In emp1 table, primary key is empcode and in table dept foreign key is
empcode which is referring empcode of emp1 table.
Making Table ready for join
By analyzing
the tables we
will be informed
that how table
are joined here.

Now we will see


that how join
works.
Join query
mysql>
SELECT * from
emp1, dept;

Data from each


column with the
combination of
each record has
show on
execution of
query.

Condition is to
be used to filter
this data.
Join query
To get the details about the departments and their in- charges, query
will be-

mysql> SELECT name, deptname from emp1, dept


where emp1.empcode=dept.deptic;

When both the tables have same field name, then to show a field
from particular table, use the following pattern to access a field-
<Table name>.<Field Name>
Ex- emp1.empcode
This is an example of Equi-
Join, in which columns are
compared for equality and
it is also an example of
Natural- Join, in which only
one of the identical columns
exists.
Additional search with Join Query
If you want to get information that Ankit is in-charge of which
department, following query will be used.-

mysql> SELECT name, deptname from emp1, dept


where emp1.empcode=dept.deptic and
emp1.name=‘Ankit’;
Using Table Aliases
• Sometimes we have very big file names and more than one table have
same filed names.
• In such cases, we need to write file names again and again which
consumes time.
• This can be solved by using alias for tables.
These are table aliases.
See the following example-

mysql> SELECT E.name, E.Salary, D.DeptName


FROM emp1 E, dept D
WHERE
E.empcode=D.deptic;

Neha Tyagi, KV 5 Jaipur II Shift


Joining more than one table
• Sometimes it is required to get the data from more than one table. In such
cases , tables are to be joined on the basis of some matching columns.
• See the following example-
mysql> SELECT E.name, E.Salary, D.DeptName, G.grade
FROM emp1 E, dept D, salarygrade G
WHERE E.empcode=D.deptic
AND E.salary BETWEEN G.lowsal AND
G.hisal;

This is an example of Non-


Equi-Join where condition is
given without using ‘=‘.
Joining table using JOIN Clause
• SQL provides some special clauses for joining of tables- JOIN (and
NATURAL JOIN) clause .
mysql > SELECT * FROM <table1>
[CROSS] [NATURAL] JOIN <Table2>
[ON (<Join Condition>) | USING(<JoinFields>)];
• For creating cartesian product -
SELECT * FROM emp1 JOIN dept;
These will give the
• For creating CROSS JOIN - same output as we
have seen in previous
SELECT * FROM emp1 CROSS JOIN dept;
slides.
• For creating EQUI- JOIN-
SELECT * FROM emp1 e. JOIN dept d
ON (e.empcode=d.deptic);

• For creating NATURAL JOIN-


SELECT * FROM emp1 NATURAL JOIN dept;
LEFT-JOIN
• When we use LEFT-JOIN, it returns all rows from first table whether it has
matching rows in second table or not.
• It shows NULL in columns for the unmatched rows of first table.

mysql>SELECT <Col List> FROM <table1> LEFT JOIN <table2>


ON <joining Condition>

Right-JOIN
• When we use RIGHT-JOIN, it returns all rows from second table whether it has
matching rows in first table or not.
• It shows NULL in columns for the unmatched rows of second table.

mysql>SELECT <Col List> FROM <table1> RIGHT JOIN <table2>


ON <joining Condition>

You might also like