saad saeed 076 dblab manual
saad saeed 076 dblab manual
saad saeed 076 dblab manual
WEEK 2, 3
CREATE DATABASE Lab2;
use Lab2;
UPDATE Student
SET Name = 'Rehan 2 ', CGPA = 3.85
WHERE RegistrationNumber = '1';
SELECT * FROM Student;
WEEK 4, 5
CREATE DATABASE Lab2_Home;
USE Lab2_Home;
UPDATE Student
SET Department = 'Software Engineering', Address = '4321 Yellow Street, Lahore'
WHERE RegistrationNumber = '1';
UPDATE Course
SET Name = 'Advanced Database Systems'
WHERE Code = 'CS202';
WEEK 6, 7
use Northwind;
SELECT
COUNT(OrderID) AS TotalOrders,
AVG(Freight) AS AverageFreight,
SUM(Freight) AS TotalFreight,
MAX(Freight) AS MaxFreight,
MIN(Freight) AS MinFreight
FROM Orders;
SELECT
ShipCountry,
COUNT(OrderID) AS TotalOrders,
AVG(Freight) AS AverageFreight
FROM Orders
GROUP BY ShipCountry
HAVING COUNT(OrderID) > 5;
SELECT
FirstName AS EmployeeFirstName,
LastName AS EmployeeLastName,
HireDate AS EmployeeHireDate
FROM Employees;
SELECT
E.FirstName,
E.LastName,
O.OrderDate
FROM Employees AS E
JOIN Orders AS O ON E.EmployeeID = O.EmployeeID;
SELECT ProductName
FROM Products
WHERE Price > (SELECT AVG(Price) FROM Products);
SELECT Supplier.Country
FROM Suppliers AS Supplier
GROUP BY Supplier.Country
HAVING COUNT(Supplier.SupplierID) >= 2;
use Northwind;
-- wrong
SELECT * FROM Orders
WHERE ShipCountry = 'USA' AND OrderDate > '1997-01-01' OR ShipCountry = 'Canada';
-- correct
SELECT * FROM Orders
WHERE (ShipCountry = 'USA' AND OrderDate > '1997-01-01') OR ShipCountry = 'Canada';
-- null
SELECT GPA * 2 AS GPA_Doubled FROM Student;
--12. Use the DISTINCT operator to eliminate the duplicates in your SQL statement:
SELECT DISTINCT ShipCountry FROM Orders;
-- Home Task
--1. Write a query to report orders that were delayed shipped:
--2. Our employees belong to how many countries? List the names.
SELECT DISTINCT Country FROM Employees;
--5. List the IDs of the orders on which discount was not provided.
SELECT OrderID FROM [Order Details]
WHERE [Order Details].Discount = 0;
--6. Enlist the names of customers who have not specified their region.
SELECT ContactName FROM Customers
WHERE Region IS NULL;
--7. Enlist the names of customers along with contact number who either belong to UK or
USA.
SELECT ContactName, Phone FROM Customers
WHERE Country IN ('UK', 'USA');
--10. List the IDs of customers whose orders were never shipped.
SELECT CustomerID FROM Orders
WHERE ShippedDate IS NULL;
--11. Write a query to report suppliers with their ID, company name, and city.
SELECT SupplierID, CompanyName, City FROM Suppliers;
--12. Our employees belong to how many countries? List them who are used to live in
London.
SELECT COUNT(DISTINCT Country) FROM Employees;
SELECT * FROM Employees
WHERE City = 'London';
--13. List the names of products which have not been discontinued
SELECT ProductName FROM Products
WHERE Discontinued = 0;
---14. List the IDs of the orders on which discount was 0.1 or less.
SELECT OrderID FROM [Order Details]
WHERE Discount <= 0.1;
--15. Enlist the IDs, names of employees and their contact number with extensions who have
not specified their region.
WEEK 8, 9,10
Query:
SELECT Customers.CustomerID, Customers.ContactName, Orders.OrderID,
Orders.OrderDate
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
Reason: A LEFT JOIN is used to include all customers and any orders they
might have. Customers who have not placed any orders will still appear in the
result set, with NULL values for order-related columns.
Query:
SELECT Customers.CustomerID, Customers.ContactName, Orders.OrderID,
Orders.OrderDate
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID
WHERE Orders.OrderID IS NULL;
Reason: This query uses a LEFT JOIN to find customers who have not placed
any orders. The WHERE Orders.OrderID IS NULL condition filters out
customers who do not have corresponding entries in the Orders table.
Query:
SELECT Customers.CustomerID, COUNT(OrderID) AS TotalOrders
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID
GROUP BY Customers.CustomerID;
Reason: The LEFT JOIN here ensures that all customers are included in the
count, even those who have not placed any orders. For customers without
orders, the count will be 0. And then group them by id so we have one row for
each customer
Query:
SELECT Customers.CustomerID, COUNT(Orders.OrderID) AS TotalOrders, SUM([Order
Details].Quantity) AS TotalQuantity
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID
LEFT JOIN [Order Details] ON Orders.OrderID = [Order Details].OrderID
WHERE Customers.Country = 'USA'
GROUP BY Customers.CustomerID;
Reason: This LEFT JOIN approach ensures that even customers who do not have any orders
are included in the results, with their TotalOrders and TotalQuantity as 0 if they have no
orders.
Double LEFT JOIN: The first LEFT JOIN includes all customers even if they have no
orders. The second LEFT JOIN ensures that order details are included for customers with
orders. This allows for calculating total orders and quantities while still including customers
who have not placed any orders.
Query:
SELECT Customers.CustomerID, Customers.CompanyName, Orders.OrderID,
Orders.OrderDate
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID
WHERE Orders.OrderDate = '1997-04-07';
Reason: The LEFT JOIN includes all customers in the results, but the WHERE clause filters
to only show orders placed on a specific date. Customers without orders on this date will
appear with NULL values for order-related columns.
Query:
SELECT Customers.CustomerID, Orders.OrderID, Orders.OrderDate
FROM Customers
INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID
WHERE OrderDate BETWEEN '1997-07-01' AND '1997-07-31';
Reason: An INNER JOIN is used to find only those customers who have orders within the
specified date range. This join ensures that only customers with matching entries in the
Orders table are included.
Query:
SELECT Products.ProductName, Orders.OrderDate
FROM Products
INNER JOIN [Order Details] ON [Order Details].ProductID = Products.ProductID
INNER JOIN Orders ON Orders.OrderID = [Order Details].OrderID
WHERE OrderDate = '1997-08-08';
Reason: This query uses INNER JOIN to link products to their orders and then filter the
results to show only those orders placed on a specific date. Only products associated with
orders on that date will appear.
Query:
SELECT o.ShipAddress AS Address, o.ShipCity AS City, o.ShipCountry AS Country
FROM Orders o
INNER JOIN Employees e ON o.EmployeeID = e.EmployeeID
WHERE e.FirstName = 'Anne' AND o.ShippedDate > o.RequiredDate;
Reason: An INNER JOIN is used to link orders to employees, specifically focusing on orders
handled by a particular employee. The query then filters to find orders that were shipped
late.
Query:
SELECT DISTINCT Orders.ShipCountry AS Country
FROM Orders
INNER JOIN [Order Details] ON Orders.OrderID = [Order Details].OrderID
INNER JOIN Products ON [Order Details].ProductID = Products.ProductID
INNER JOIN Categories ON Products.CategoryID = Categories.CategoryID
WHERE Categories.CategoryName = 'Beverages';
Reason: INNER JOIN is used here to connect orders with products and their categories. The
query then filters to get the unique list of countries where
WEEK 11, 12
use lastdb;