Lab # 05
Lab # 05
Lab # 05
1. Northwind Database
The database is about a company named "Northwind Traders". The database captures all the sales
transactions that occurs between the company i.e. Northwind traders and its customers as well as the
purchase transactions between Northwind and its suppliers.
1
Lab No 5
2. INNER JOINS
Now, how can we find out
Which products are provided by which suppliers?
Which customers placed which orders?
Which customers are buying which products?
Creating a report that returns the employee id and order id from the Orders table is not difficult.
But this is not very useful as we cannot tell who the employee is that got this order. The next sample
shows how we can use a join to make the report more useful.
Products table in Northwind database only stores SupplierID which is a foreign key pointing back to
SupplierID column in suppliers table. If we want to know the supplier's name for a product, we need
to write a query to join with suppliers table to get this information. In this practice, a single result set
is returned which displays product name and the supplier's name for each product.
/*
This query returns supplier's name for each product.
Note that the result is ordered by column alias SupplierName.
*/
SELECT p.ProductName,
s.CompanyName AS SupplierName
FROM products p
INNER JOIN suppliers s ON p.SupplierID=s.SupplierID
ORDER BY SupplierName;
3.
4. With where clause:
/*
The following two queries return the same result set.
-- Query 1
SELECT DISTINCT c.CompanyName, o.OrderDate
FROM orders AS o
INNER JOIN Customers AS c ON o.CustomerID=c.CustomerID
WHERE o.OrderDate BETWEEN '1998-05-04' AND '1998-05-06'
ORDER BY o.OrderDate;
-- Query 2
SELECT DISTINCT c.CompanyName, o.OrderDate
FROM orders AS o
INNER JOIN Customers AS c ON o.CustomerID=c.CustomerID
WHERE o.OrderDate > '1998-05-03'
ORDER BY o.OrderDate;
3
Lab No 5
5. Multi-table Joins
Syntax
Note that, to join with a table, that table must be in the FROM clause or must already be joined with
the table in theFROM clause. Consider the following.
SELECT table1.column, table2.column, table3.column
FROM table1
JOIN table3 ON (table2.column=table3.column)
JOIN table2 ON (table1.column=table2.column)
WHERE conditions
The above code would break because it attempts to join table3 with table2 before table2 has been
joined withtable1.
Create a report showing the Order ID, the name of the company that placed the order, and
the first and last name of the associated employee. Only show orders placed after January 1,
1998 that shipped after they were required. Sort by Company Name.
1. Create a report that shows the order ids and the associated employee names for orders that
shipped after the required date. It should return the following. (37)
2. Create a report that shows the total quantity of products (from the Order_Details table) ordered.
Only show records for products for which the quantity ordered is fewer than 200. (5)
3. Create a report that shows the total number of orders by Customer since December 31, 1996. The
report should only return rows for which the NumOrders is greater than 15.(5)
4
Lab No 5
4. Create a report that shows the company name, order id, and total price of all products of which
Northwind has sold more than $10,000 worth. There is no need for a GROUP BY clause in this
report.
6. OUTTER JOINS
So far, all the joins we have worked with are inner joins, meaning that rows are only returned that
have matches in both tables. For example, when doing an inner join between the Employees table and
the Orders table, only employees that have matching orders and orders that have matching employees
will be returned.
Create a report that shows the number of employees and customers from each city that has
employees in it.
Left Joins
A LEFT JOIN (also called a LEFT OUTER JOIN) returns all the records from the first table even if
there are no matches in the second table.
SELECT table1.column, table2.column
FROM table1
LEFT [OUTER] JOIN table2 ON (table1.column=table2.column)
WHERE conditions
All rows in table1 will be returned even if they do not have matches in table2.
5
Lab No 5
All records in the Employees table will be counted whether or not there are matching cities in
the Customers table.
Right Joins
A RIGHT JOIN (also called a RIGHT OUTER JOIN) returns all the records from the second table
even if there are no matches in the first table.
All rows in table2 will be returned even if they do not have matches in table1.
/*
Create a report that shows the number of
employees and customers from each city that has customers in it.
*/
All records in the Customers table will be counted whether or not there are matching cities in the
Employees table.
A FULL JOIN (also called a FULL OUTER JOIN) returns all the records from each table even if
there are no matches in the joined table.
Create a report that shows the number of employees and customers from each city.
6
Lab No 5
(e.City = c.City)
GROUP BY e.City, c.City
ORDER BY numEmployees DESC;
In employees table, EmployeeID is primary key and ReportsTo is foreign key which relates back to
EmployeeID in the same table. So we can use ReportsTo and EmployeeID to join the employees
table to itself and find out the manager for each employee.
/*
This query displays manager and staff relationship.
Joined columns:
7
Lab No 5
Understand and Run sample queries and then write SQL for queries given in exercise.
8
Lab No 5
8. Fetch no. of employees in each region. If there is no employee in any region, even then
region name should appear in the list with employee count of 0.
(RegionName, No. of employees)
10. Select Top 3 employees of company. Employees are ranked on the basis of no. of orders
they have processed.
(ROWNUM <= 3, ORDER BY number of orders processed)
11. Select orders in which products of neither ‘Meat/Poultry’ nor ‘Dairy Products’ categories
exist. (Order ID)