Week 4
Week 4
Lab Manual 4
Learning Objectives
Understand the Cartesian product.
Understand the concepts of joins.
Understanding the different types of joins.
Understanding of combining data across multiple joins.
Query:
SELECT *
FROM customers, orders;
Result:
CustomerId ContactName city OrderId shipAddres ShipPostalCod
s e
1 Ali Lahore 101 Lahore 20022
2 Ahmed Faisalabad 102 Karachi 30011
2 Ahmed Faisalabad 103 Lahore 15022
Joins Syntax
SELECT column_name
FROM table1
INNER JOIN/ CROSS JOIN/ NATURAL JOIN/ LEFT JOIN/ RIGHT
JOIN table2
ON table1.column_name = table2.column_name;
Inner Join
An Inner Join is used to combine two tables based on a specified
condition, usually matching values in a common column. Only rows
that satisfy the condition (i.e., have matching values in both tables) are
included in the result.
Example: Let us consider the tables below first
customers
CustomerId ContactName City
1 Ali Lahore
2 Ahmed Faisalabad
3 Aslam Karachi
Result:
customer_id contactname city orderid shipaddress shippostalcod
e
1 Ali Lahore 101 Lahore 200
2 Ahmed Faisalabad 102 Karachi 150
2 Ahmed Faisalabad 103 Lahore 150
Query:
SELECT *
FROM customers C
LEFT JOIN orders O ON C.customerId = O.customerId
UNION
SELECT *
FROM customers C
RIGHT JOIN orders O ON C.customerId = O.customerId;
Result:
customerI Contactnam city orderi shipaddress ShipPostalCod
e
d e d
1 Ali Lahore 101 Lahore 20022
2 Ahmed Faisalaba 102 Karachi 30011
d
2 Ahmed Faisalaba 103 Lahore 15022
d
3 Aslam Karachi Null Null Null
Null Null Null 104 Islamabad 40012
customers
CustomerId ContactName city
1 Ali Lahore
2 Aslam Karachi
Database Systems-Manual 4 Page | 9
orders
orderId customerId product_id amount
101 1 10 200
102 2 11 150
products
product_id product_name Price
10 Laptop 500
11 Phone 300
Requirement:
Retrieve customer names, cities, order amounts, and product names for
all placed orders.
Query:
SELECT *
FROM customers
INNER JOIN orders ON customers.customerid =
orders.customerid
INNER JOIN products ON orders.product_id =
products.product_id;
Result:
name city amount product_name
Ali Lahore 200 Laptop
Aslam Karachi 150 Phone
Tasks:
Perform all JOIN queries on any table using Northwind
Schema.
Perform self-cross join and see if there is any difference
between cross join and self cross join.
SELECT Customers.custid,
Customers.companyname, Orders.orderid,
Orders.orderdate
FROM Sales.Customers AS C
INNER JOIN Sales.Orders AS O
Database Systems-Manual 4 Page | 10
ON Customers.custid = Orders.custid;
1. List the customers who made purchases during August 1996.
(CustomerID, ContactName, OrderID, OrderDate,
ShipName,ShipAddress)
2. Show the names of the products that were purchased on August
8, 1997. (ProductID, ProductName, OrderID, OrderDate)
3. List all countries where beverages have been delivered.
(Country)
8. Identify customers who have not placed any orders, and return
their IDs only. (CustomerID)
9. Create a query that lists all customers, but only links them to
orders placed on September 4, 1997. (CustomerID,
ContactName, OrderID, OrderDate, TotalAmount)
10. Retrieve the address, city, and country information for all orders
serviced by Anne that were shipped past their due date.
15. Find the customers who have placed orders for products from
the 'Seafood' category and have had those orders shipped by
'United Package'. (CustomerID, ContactName, OrderID,
CategoryName, ShipVia)
16. List all pairs of employees who live in the same city but have
different titles. (EmployeeID1, FirstName1, LastName1, Title1,
EmployeeID2, FirstName2, LastName2, Title2, City)
What to Submit:
Submit the following file in Zip on Eduko:
2024-CS-X.txt