sql joins- reena p v
Post on 22-Jan-2018
129 Views
Preview:
TRANSCRIPT
Objectives:
To understand the requirement of
SQL Joins
Identify and demonstrate the proper
use of inner join , outer join clauses.
INTRODUCTION - SQL JOINS
SQL Joins are essential to display
data from more than one table.
SQL JOIN clause is used to
combine rows from two or more
tables, based on a common field
between them..
C artesian Product (Cross Join)
Cartesian product of two tables is
obtained by pairing up each row of
one table with each row of the other
table.
SELECT * FROM order_table, product_table;
Order_No P_code Sup_Code Code Name
1 P001 S002 P001 Toothpaste
2 P002 S002 P001 Toothpaste
1 P001 S002 P002 Shampoo
2 P002 S002 P002 Shampoo
1 P001 S002 P003 Conditioner
2 P002 S002 P003 Conditioner
Each row of the first table (Order_table) is paired with each
row of the second table (Product).
C artesian Product (Cross Join)
The number of columns in the Cartesian
product is the sum of the number of columns
in both the tables
The number of rows in the Cartesian
product is the product of rows of the tables
C artesian Product (Cross Join)
E qui-Join of Tables
• A join which is obtained by putting a
condition of equality on cross join is
called an 'equi join'.
• It is a simple join condition that uses ‘= ‘
sign
as a comparison operation .
• we can extract meaningful information
from
the Cartesian product by placing some
E qui-Join of Tables
Qualifying the column name
SELECT Order_No, Order_table.Sup_Code, Name,
Address FROM order_table, supplier
WHERE order_table.sup_code = supplier.sup_code;
Extracting values by putting condition (filtering)
SELECT * FROM order, product
WHERE order.O_code = product.P_code and qty>100;
Non Equi Join
A JOIN that uses comparison
operator instead of the equal sign
like >, <, >=, <= along with
conditions.
Inner Join
An inner join is a type of join
where we use = and <> in the
where condition joining the
tables.
ROLLNO NAME
1 ROHAN
2 JAYA
3 TEENA
4 DIKSHA
5 SUMA
Table : A
ROLL NO FEES
3 4500
4 3500
5 5000
6 6000
Table : B
SELECT A. ROLLNO, NAME ,FEES FROM A,B
WHERE A. ROLLNO= B.ROLLNO;
ROLLNO NAME FEES
3 TEENA 4500
4 DIKSHA 3500
5 SUMA 5000
SELECT A. ROLLNO, NAME ,FEES FROM A INNER JOIN B
ON A. ROLLNO= B.ROLLNO;
A left outer join, or left join, results in
a set where all of the rows from the
first table are preserved.
The rows from the second, or right
hand side table only show up if they
have a match with the rows from the
first table.
Where there are values from the left
table but not from the right, the table
will read null.
ROLLNO NAME
1 ROHAN
2 JAYA
3 TEENA
4 DIKSHA
5 SUMA
Table : A
ROLL NO FEES
3 4500
4 3500
5 5000
6 6000
Table : B
SELECT A. ROLLNO, NAME ,FEES FROM A
LEFT OUTER JOIN B ON A. ROLLNO= B.ROLLNO;
ROLLNO NAME FEES
1 ROHAN NULL
2 JAYA NULL
3 TEENA 4500
4 DIKSHA 3500
5 SUMA 5000
ROLLNO NAME
1 ROHAN
2 JAYA
3 TEENA
4 DIKSHA
5 SUMA
Table : A
ROLL NO FEES
3 4500
4 3500
5 5000
6 6000
Table : B
SELECT A. ROLLNO, NAME ,FEES FROM A
RIGHT OUTER JOIN B ON A. ROLLNO=
B.ROLLNO;
ROLLNO NAME FEES
3 TEENA 4500
4 DIKSHA 3500
5 SUMA 5000
NULL NULL 6000
FULL OUTER JOINThe full outer join returns a result table with
the matched data of two table then remaining
rows of both left table and then the right table.
ROLLNO NAME FEES
3 TEENA 4500
4 DIKSHA 3500
5 SUMA 5000
1 ROHAN NULL
2 JAYA NULL
NULL NULL 6000
NATURAL JOIN
The JOIN in which only one of the
identical columns coming from joined
tables exists called natural join.
i.e In the result common column will
appear only once.
LOAN NO BRANCH AMOUNT
L1 B1 1000
L2 B2 2000
L3 B3 1500
Table : LOAN
CUST NO LOAN NO
C1 L1
C2 L2
C3 L4
Table : BORROWER
SELECT * FROM LOAN NATURAL JOIN
BORROWER;
LOAN NO BRANCH AMOUNT CUST_NO
L1 B1 1000 C1
L2 B2 2000 C2
SELF JOIN
A self-join is a query in which a table
is joined (compared) to itself. Self-
joins are used to compare values in a
column with other values in the same
column in the same table
EMP_ID NAME MGR_ID
1 Rakesh 3
2 Sudarsh 1
3 Rohit Null
4 Mukesh 1
5 Sushil 1
Table : Employee
SELECT E. NAME AS EMPLOYEE , M.NAME
AS MANAGER FROM Employee E JOIN
Employee M ON E. MGR_ID= M.EMP_ID
EMPLOYEE MANAGER
Rakesh Rohit
Sudarsh Rakesh
Rohit NULL
Mukesh Rakesh
Sushil Rakesh
What is the difference between
EQUI JOIN and NATURAL JOIN ?
A…........Join is made possible by
aliasing the original table.
top related