0% found this document useful (0 votes)
21 views10 pages

Idbslab 10

This document discusses different types of joins in SQL, including inner joins, left joins, right joins, and full joins. It provides the syntax for each type of join and examples to illustrate how each brings together data from two tables based on matching column values. The examples show joining data from tables containing customer and order information to list customers with their orders or list orders with their customers. The document concludes by noting that primary and foreign keys are commonly used to establish joins but other columns can also be used. The result of a join is a single table containing selected columns from the input tables for matching rows.

Uploaded by

Javaria Tabassum
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)
21 views10 pages

Idbslab 10

This document discusses different types of joins in SQL, including inner joins, left joins, right joins, and full joins. It provides the syntax for each type of join and examples to illustrate how each brings together data from two tables based on matching column values. The examples show joining data from tables containing customer and order information to list customers with their orders or list orders with their customers. The document concludes by noting that primary and foreign keys are commonly used to establish joins but other columns can also be used. The result of a join is a single table containing selected columns from the input tables for matching rows.

Uploaded by

Javaria Tabassum
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/ 10

Introduction to Database Systems

Experiment 10
Processing multiple tables using SQL joins.

CLO-3: Use modern tools and languages.


CLO-4: Build the projects of varying complexities.
CLO-5: Demonstrate the implementation of problem solving process.
CLO-6: Work individually as well as in teams.
CLO-7: Propose a unique solution that exhibits originality of idea

Engr. Rabia Arshad


Processing Multiple Tables Using Joins
The most frequently used relational operation, which brings together data from two or more
related tables into one resultant table, is called a join.

SQL JOINS

The JOIN keyword is used to query data from two or more tables, based on a relationship
between certain columns in these tables.

TYPES OF JOINS:

 JOIN/INNER JOIN: Return rows when there is at least one match in both tables
 LEFT JOIN: Return all rows from the left table, even if there are no matches in the right
table
 RIGHT JOIN: Return all rows from the right table, even if there are no matches in the
left table
 FULL JOIN: Return rows when there is a match in one of the tables

SQL INNER JOIN Keyword

The INNER JOIN keyword return rows when there is at least one match in both tables.

SQL INNER JOIN Syntax

SELECT column_name(s)
FROM table_name1
INNER JOIN table_name2
ON table_name1.column_name=table_name2.column_name

SQL INNER JOIN Example

The "Persons" table:

P_Id LastName FirstName Address City


1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger
The "Orders" table:

O_Id OrderNo P_Id


1 77895 3
2 44678 3
3 22456 1
4 24562 1
5 34764 15

Now we want to list all the persons with any orders.

We use the following SELECT statement:

SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo


FROM Persons
INNER JOIN Orders
ON Persons.P_Id=Orders.P_Id
ORDER BY Persons.LastName

The result-set will look like this:

LastName FirstName OrderNo


Hansen Ola 22456
Hansen Ola 24562
Pettersen Kari 77895
Pettersen Kari 44678

The INNER JOIN keyword return rows when there is at least one match in both tables. If there
are rows in "Persons" that do not have matches in "Orders", those rows will NOT be listed

SQL LEFT JOIN Keyword

The LEFT JOIN keyword returns all rows from the left table (table_name1), even if there are no
matches in the right table (table_name2).
Syntax

SELECT column_name(s)
FROM table_name1
LEFT JOIN table_name2
ON table_name1.column_name=table_name2.column_name

PS: In some databases LEFT JOIN is called LEFT OUTER JOIN.

SQL LEFT JOIN Example

The "Persons" table:

P_Id LastName FirstName Address City

1 Hansen Ola Timoteivn 10 Sandnes

2 Svendson Tove Borgvn 23 Sandnes


3 Pettersen Kari Storgt 20 Stavanger

The "Orders" table:

O_Id OrderNo P_Id


1 77895 3
2 44678 3
3 22456 1
4 24562 1
5 34764 15

Now we want to list all the persons and their orders - if any, from the tables above.We use the
following SELECT statement:

SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo


FROM Persons
LEFT JOIN Orders
ON Persons.P_Id=Orders.P_Id
ORDER BY Persons.LastName
The result-set will look like this:

LastName FirstName OrderNo


Hansen Ola 22456
Hansen Ola 24562
Pettersen Kari 77895
Pettersen Kari 44678
Svendson Tove

The LEFT JOIN keyword returns all the rows from the left table (Persons), even if there are no
matches in the right table (Orders).

SQL RIGHT JOIN Keyword

The RIGHT JOIN keyword Return all rows from the right table (table_name2), even if there are
no matches in the left table (table_name1).

SQL RIGHT JOIN Syntax

SELECT column_name(s)
FROM table_name1
RIGHT JOIN table_name2
ON table_name1.column_name=table_name2.column_name

SQL RIGHT JOIN Example

The "Persons" table:

P_Id LastName FirstName Address City


1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger
The "Orders" table:

O_Id OrderNo P_Id


1 77895 3
2 44678 3
3 22456 1
4 24562 1
5 34764 15

Now we want to list all the orders with containing persons - if any, from the tables above.We use
the following SELECT statement:

SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo


FROM Persons
RIGHT JOIN Orders
ON Persons.P_Id=Orders.P_Id
ORDER BY Persons.LastName

The result-set will look like this:

LastName FirstName OrderNo


Hansen Ola 22456
Hansen Ola 24562
Pettersen Kari 77895
Pettersen Kari 44678
34764

The RIGHT JOIN keyword returns all the rows from the right table (Orders), even if there are no
matches in the left table (Persons)

SQL FULL JOIN Keyword

The FULL JOIN keyword return rows when there is a match in one of the tables.

SQL FULL JOIN Syntax

SELECT column_name(s)
FROM table_name1
FULL JOIN table_name2
ON table_name1.column_name=table_name2.column_name
The "Persons" table:

P_Id LastName FirstName Address City


1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger

The "Orders" table:

O_Id OrderNo P_Id


1 77895 3
2 44678 3
3 22456 1
4 24562 1
5 34764 15

Now we want to list all the persons and their orders, and all the orders with their persons.

We use the following SELECT statement:

SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo


FROM Persons
FULL JOIN Orders
ON Persons.P_Id=Orders.P_Id
ORDER BY Persons.LastName

The result-set will look like this:

LastName FirstName OrderNo


Hansen Ola 22456
Hansen Ola 24562
Pettersen Kari 77895
Pettersen Kari 44678
Svendson Tove
34764
The FULL JOIN keyword returns all the rows from the left table (Persons), and all the rows from
the right table (Orders). If there are rows in "Persons" that do not have matches in "Orders", or if
there are rows in "Orders" that do not have matches in "Persons", those rows will be listed as
well.

NOTE:

A primary key from one table and a foreign key that references the table with the primary key
will share a common domain and are frequently used to establish a join.

Occasionally, joins will be established using columns that share a common domain but not the
primary-foreign key relationship, and that will also work.

The result of a join operation is a single table. Selected columns from all the tables are included.
Each row returned contains data from rows in the different input tables where values for the
common columns match.

Task 1:

List customer name, identification number, and order number for all orders listed in the
Order table. Include the order number, even if there is no customer name and
identification number available.

Task 2:

CUSTOMER_T

ORDER_T
What are the names of all customers who have placed orders? List the same in the
ascending order of ORDER_ID.

Task 3:

List customer name, identification number, and order number for all customers listed in
the CUSTOMER_T table. Include the customer identification number and name even if
there is no order available for that customer.
Task 4:

List customer name, identification number, and order number for all orders listed in the
ORDER_T table. Include the order number and name even if there is no customer name
and identification number available.

You might also like