Lab#20 & Lab#22 Asim Raza
Lab#20 & Lab#22 Asim Raza
“Asim Raza”
F21-0415
Section A (SE, 4th)
Lab# 20
Database system
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.
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.
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
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?
Output:
Query (Sub-query): What are the name and address of the customer who placed order number
1008?
FROM Customer_T
WHERE
Customer_T.CustomerID =
(SELECT Order_T.CustomerID
FROM Order_T
Output:
Query: What are the names of customers who have placed orders?
-----------------------------------
**********