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

Lab#20 & Lab#22 Asim Raza

This document summarizes two lab assignments on different types of joins and subqueries in database systems. The first lab covers left outer joins and right outer joins, giving examples of how to write queries using each join to retrieve customer and order data. The second lab introduces subqueries, explaining how an inner query can provide values to a where clause in an outer query. It provides examples of subquery and non-subquery syntax to retrieve a customer's information based on their order number.

Uploaded by

AWAN
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)
11 views

Lab#20 & Lab#22 Asim Raza

This document summarizes two lab assignments on different types of joins and subqueries in database systems. The first lab covers left outer joins and right outer joins, giving examples of how to write queries using each join to retrieve customer and order data. The second lab introduces subqueries, explaining how an inner query can provide values to a where clause in an outer query. It provides examples of subquery and non-subquery syntax to retrieve a customer's information based on their order number.

Uploaded by

AWAN
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/ 8

IT DEPARTMENT

“Asim Raza”
F21-0415
Section A (SE, 4th)
Lab# 20
Database system

Submission data: 23/5/2023


LAB 20—DIFFERENT TYPES OF JOINS

Objectives:
Students will learn how to use different types of joins, left outer join,
right outer join
Outer Join:
A join in which rows that do not have matching value in common columns are nevertheless
included in the results table. Null values appear in columns where there is no match between tables.
Example use: In Order_T table come customers’ orders are appearing. There are some customers
in Customer_T who have not placed any order. Due to that qui-join shown previously do not
include all the customer shown in Customer_T. The organization may be very interested in
identifying those customers who have not placed orders. It might want to contact them to
encourage new orders, or it might be interested in analyzing the customers to know why they are
not ordering.

Using an outer join produces this information.

Left OUTER Join


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

SELECT Customer_t.Customerid, Customername,

Orderid FROM Customer_t LEFT OUTER JOIN

Order_t ON Customer_t.CustomerID =

Order_t.customerid;

Output:
Query: 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.

SELECT customer_t.CustomerID, CustomerName, OrderID FROM

Customer_T RIGHT OUTER JOIN Order_t ON

Customer_t.CustomerID = Order_t.CustomerID;

This query will also return the rows in which the foreign key column does not exist i.e. NULL, for
any row

Output:

------------------------------------------------------------------
****************
IT DEPARTMENT
“Asim Raza”
F21-0415
Section A (SE, 4th)
Lab# 22
Database system

Submission data: 23/5/2023


LAB 22—SUBQUERIES

Objectives:
The purpose of this lab is to familiarize students with how to call a
query within other query to make a sub-query, and how to write
efficient sub-queries.
Subqueries
The preceding SQL examples illustrate one of the two basic approaches for joining two tables: the
joining technique. SQL also provides the subquery technique, which involves placing an inner
query (SELECT . . . FROM . . . WHERE) within a WHERE or HAVING clause of another (outer)
query. The inner query provides a set of one or more values for the search condition of the outer
query. Such queries are referred to as subqueries or nested subqueries. Subqueries can be nested
multiple times. Subqueries are prime examples of why SQL is a set-oriented language.
Query: (Not Subquery) What are the name and address of the customer who placed order
number 1008?

SELECT CustomerName, CustomerAddress, CustomerCity,


CustomerState, CustomerPostalCode

FROM Customer_T, Order_T

WHERE Customer_T.CustomerID = Order_T.

CustomerID AND OrderID = 1008;

Output:
Query (Sub-query): What are the name and address of the customer who placed order number
1008?

SELECT CustomerName, CustomerAddress,


CustomerCity,CustomerState, CustomerPostalCode

FROM Customer_T

WHERE

Customer_T.CustomerID =

(SELECT Order_T.CustomerID

FROM Order_T

WHERE OrderID = 1008);

Output:

Query: What are the names of customers who have placed orders?

-----------------------------------
**********

You might also like