0% found this document useful (0 votes)
23 views12 pages

sm07152 DB LAB8

Uploaded by

sadiqah mushtaq
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)
23 views12 pages

sm07152 DB LAB8

Uploaded by

sadiqah mushtaq
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/ 12

Database Lab 08

Sadiqah Mushtaq

1 Exercises
1. List suppliers in the order of no. of products supplied (Supplier Name, No
of Products) in descending order.

SELECT
Suppliers.CompanyName AS Supplier_Name,
COUNT(Products.ProductID) AS Number_of_Products
FROM
Suppliers
INNER JOIN
Products ON Suppliers.SupplierID = Products.SupplierID
GROUP BY
Suppliers.CompanyName
ORDER BY
Number_of_Products Desc

2. List number of products supplied by different suppliers in different cate-


gories.

1
SELECT
Suppliers.CompanyName,
Categories.CategoryName,
COUNT(Products.ProductID) AS No_of_Products,
AVG(Products.UnitPrice) AS Average_Price,
SUM(Products.UnitsInStock) AS TotalUnitsInStock
FROM
Suppliers
INNER JOIN
Products ON Suppliers.SupplierID = Products.SupplierID
INNER JOIN
Categories ON Products.CategoryID = Categories.CategoryID
GROUP BY
Suppliers.CompanyName,
Categories.CategoryName

3. Select Suppliers supplying more than 4 products.

SELECT
Suppliers.CompanyName AS [Supplier Name]
FROM
Suppliers
INNER JOIN
Products ON Suppliers.SupplierID = Products.SupplierID
GROUP BY
Suppliers.CompanyName
HAVING
COUNT(Products.ProductID) > 4

2
4. Fetch no. of employees from Employees Table who are working in each
region where Region is from Region Table in ascending order i.e. ”South-
ern”, ”Western”, ”Northern”, or ”Eastern”.

SELECT
Region.RegionDescription,
COUNT(DISTINCT Employees.EmployeeID) AS Number_Of_Employees
FROM
Region
INNER JOIN
Territories ON Region.RegionID = Territories.RegionID
INNER JOIN
EmployeeTerritories ON Territories.TerritoryID = EmployeeTerritories.TerritoryI
INNER JOIN
Employees ON EmployeeTerritories.EmployeeID = Employees.EmployeeID
GROUP BY
Region.RegionDescription
ORDER BY
Region.RegionDescription ASC;

5. Select total amount of each order. [Total amount is calculated by summing


up (Unit Price * Qty) - Discount in order details.]

3
SELECT
[Order Details].OrderID,
SUM([Order Details].UnitPrice * [Order Details].Quantity - [Order Details].Disc
FROM
[Order Details]
GROUP BY
[Order Details].OrderID

6. Find total number of products in each category.

SELECT
COUNT(Products.ProductID) AS [No of Products],
Categories.CategoryName
FROM
Products
INNER JOIN
Categories ON Products.CategoryID = Categories.CategoryID
GROUP BY
Categories.CategoryName

4
7. Find number of orders placed by different customers for different suppliers
where ContactName is from Customers Table and CompanyName is from
Suppliers Table.

SELECT
Suppliers.CompanyName,
Customers.ContactName,
COUNT(DISTINCT Orders.OrderID) AS [No of Orders]
FROM
[Order Details]
INNER JOIN
Products ON [Order Details].ProductID = Products.ProductID
INNER JOIN
Suppliers ON Products.SupplierID = Suppliers.SupplierID
INNER JOIN
Orders ON Orders.OrderID = [Order Details].OrderID
INNER JOIN
Customers ON Orders.CustomerID = Customers.CustomerID
GROUP BY
Customers.ContactName,
Suppliers.CompanyName

5
8. Find number of orders handled by different employees in different years.

SELECT
CONCAT(Employees.FirstName, ’ ’, Employees.LastName) AS [Employee Name],
YEAR(OrderDate) AS OrderYear,
COUNT(Orders.OrderID) AS [Number of Orders]
FROM
Orders
INNER JOIN
Employees ON Employees.EmployeeID = Orders.EmployeeID
GROUP BY
YEAR(OrderDate),
CONCAT(Employees.FirstName, ’ ’, Employees.LastName)

9. Find number of orders, in descending order, handled by different employees


under different managers.

SELECT
CONCAT(M.FirstName, ’ ’, M.LastName) AS [Manager Name],

6
CONCAT(E.FirstName, ’ ’, E.LastName) AS [Employee Name],
COUNT(DISTINCT Orders.OrderID) AS [No of Orders]
FROM
Orders
INNER JOIN
Employees E ON E.EmployeeID = Orders.EmployeeID
INNER JOIN
Employees M ON M.ReportsTo = Orders.EmployeeID
GROUP BY
CONCAT(M.FirstName, ’ ’, M.LastName),
CONCAT(E.FirstName, ’ ’, E.LastName)
ORDER BY
CONCAT(M.FirstName, ’ ’, M.LastName),
CONCAT(E.FirstName, ’ ’, E.LastName)

10. Fetch no. of employees in each region from Region Table. If there is no
employee in any region, even then the region name should appear in the
list with employee count of 0.

SELECT
Region.RegionDescription,
COALESCE(COUNT(DISTINCT Employees.EmployeeID), 0) AS NumberOfEmployees
FROM
Region
LEFT JOIN
Territories ON Region.RegionID = Territories.RegionID
LEFT JOIN
EmployeeTerritories ON Territories.TerritoryID = EmployeeTerritories.TerritoryI
LEFT JOIN
Employees ON EmployeeTerritories.EmployeeID = Employees.EmployeeID
GROUP BY
Region.RegionDescription

7
11. Find all combinations of employees and customer ordered by employee ID.

SELECT
CONCAT(Employees.FirstName, ’ ’, Employees.LastName) AS [Employee Full Name],
Customers.ContactName AS CustomerName
FROM
Employees
CROSS JOIN
Customers
ORDER BY
Employees.EmployeeID

12. Get CustomerID and ContactName from Customers alphabetically, by


Country and name.

SELECT
Customers.CustomerID,

8
Customers.ContactName
FROM
Customers
ORDER BY
Customers.ContactName,
Customers.Country ASC

13. Display the number of employees and customers from each city that has
employees in it ordered by City.

SELECT
COUNT(DISTINCT Employees.EmployeeID) AS [Number of Employees],
COUNT(DISTINCT Customers.CustomerID) AS [Number of Customers]
FROM
Customers
RIGHT JOIN
Employees ON Employees.City = Customers.City
GROUP BY
Employees.City
ORDER BY
Employees.City

9
14. Display the number of employees and customers from each city.

SELECT
COUNT(DISTINCT Employees.EmployeeID) AS [Number of Employees],
COUNT(DISTINCT Customers.CustomerID) AS [Number of Customers],
COALESCE(Employees.City, Customers.City) AS City
FROM
Customers
FULL OUTER JOIN
Employees ON Employees.City = Customers.City
GROUP BY
Employees.City,
Customers.City

15. Display the order ids and the associated employee names for orders that
shipped after the required date.

SELECT
Orders.OrderID,

10
CONCAT(Employees.FirstName, ’ ’, Employees.LastName) AS [Employee Full Name]
FROM
Employees
INNER JOIN
Orders ON Orders.EmployeeID = Employees.EmployeeID
WHERE
Orders.ShippedDate > Orders.RequiredDate

16. Display the total quantity of products (from the Order Details table) or-
dered. Only show records for products for which the quantity ordered is
fewer than 200.

SELECT
[Order Details].ProductID,
SUM([Order Details].Quantity) AS [Total Ordered Quantity]
FROM
[Order Details]
GROUP BY
[Order Details].ProductID
HAVING
SUM([Order Details].Quantity) < 200

11
17. Display the total number of orders by Customer since December 31, 1996.
The report should only return rows for which the total number of orders
is greater than 15.

SELECT
Orders.CustomerID,
COUNT(DISTINCT Orders.OrderID) AS [No of Orders]
FROM
Orders
WHERE
Orders.OrderDate > ’1996-12-31’
GROUP BY
Orders.CustomerID
HAVING
COUNT(DISTINCT Orders.OrderID) > 15

12

You might also like