0% found this document useful (0 votes)
2 views19 pages

Lecture 08

This document is a lecture on database systems focusing on joins, which are used to retrieve data from multiple related tables in a relational database. It outlines different types of joins including Inner Join, Left Join, Right Join, Full Join, and Self Join, along with their syntax and examples. The lecture emphasizes the importance of join conditions in defining relationships between tables for effective data retrieval.

Uploaded by

anas
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)
2 views19 pages

Lecture 08

This document is a lecture on database systems focusing on joins, which are used to retrieve data from multiple related tables in a relational database. It outlines different types of joins including Inner Join, Left Join, Right Join, Full Join, and Self Join, along with their syntax and examples. The lecture emphasizes the importance of join conditions in defining relationships between tables for effective data retrieval.

Uploaded by

anas
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/ 19

By: Engr.

Sapna Kumari
DATABASE SYSTEMS- LECTURE 08 Lecturer, CS-SZABIST
Sapna.kumari@hyd.szabist.edu.pk
OUTLINE
• Joins
• Types of Joins
-Inner Join
-Left Join
- Right Join
- Full Join
- Self Join
JOINS
A relational database consists of multiple related tables linking together using
common columns which are known as foreign key columns. Because of this, data in
each table is incomplete from the business perspective.
By using joins, you can retrieve data from two or more tables based on logical
relationships between the tables. Joins indicate how SQL should use data from one
table to select the rows in another table.
A join condition defines the way two tables are related in a query by:
• Specifying the column from each table to be used for the join. A typical join
condition specifies a foreign key from one table and its associated key in the other
table.
• Specifying a logical operator (for example, = or <>,) to be used in comparing
values from the columns.
JOINS-TYPES
-Inner Join
-Left Join
- Right Join
- Full Join
- Self Join
INNER JOIN
The INNER JOIN keyword selects records that have matching values in both tables.
Syntax:
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
If the join condition uses the equal operator (=) and the column
names in both tables used for matching are the same,
you can use the USING clause instead:
INNER JOIN-EXAMPLE
SELECT * FROM TableA
INNER JOIN TableB ON
TableA.PK = TableB.PK
INNER JOIN-EXAMPLE
LEFT JOIN
The LEFT JOIN keyword returns all records from the left table (table1), and the matched records from
the right table (table2). The result is NULL from the right side, if there is no match.

Syntax
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
Note: In some databases LEFT JOIN is called LEFT OUTER JOIN.
LEFT JOIN-EXAMPLE
SELECT * FROM TableA
LEFT JOIN TableB ON
TableA.PK = TableB.PK
LEFT JOIN-EXAMPLE
RIGHT JOIN
The RIGHT JOIN keyword returns all records from the right table (table2), and the
matched records from the left table (table1). The result is NULL from the left side,
when there is no match.
Syntax
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;

Note: In some databases RIGHT JOIN is called RIGHT OUTER JOIN.


RIGHT JOIN -EXAMPLE
SELECT * FROM TableA
RIGHT OUTER JOIN TableB
ON TableA.PK = TableB.PK
RIGHT JOIN -EXAMPLE
FULL JOIN
The FULL OUTER JOIN or OUTER JOIN/ FULL JOIN keyword returns all records when
there is a match in left (table1) or right (table2) table records.
Syntax:
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
FULL OUTER JOIN Orders
ON Customers.CustomerID=Orders.CustomerID
FULL JOIN -EXAMPLE

SELECT * FROM TableA FULL


OUTER JOIN TableB
ON TableA.PK = TableB.PK
FULL JOIN -EXAMPLE
SELF JOIN
A self join is a type of join operation in SQL where a table is joined with itself. In other words,
you use the same table twice in the join operation, with each instance of the table being given
a different alias to distinguish between them.
Self joins are used when you need to compare rows within the same table or retrieve
hierarchical data from a table. They are commonly used in scenarios where you have a
hierarchical relationship within a table, such as with parent-child relationships.
Syntax:
SELECT a.col_name, b.col_name, …
FROM table1 a, table1 b
WHERE a.common_field = b.common_field;
SELF JOIN-EXAMPLE
SELECT
employee.Id,
employee.FullName,
employee.ManagerId,
manager.FullName as ManagerName
FROM Employees employee
JOIN Employees manager
ON employee.ManagerId = manager.Id
FULL JOIN -EXAMPLE

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