-- 50 Northwind SQL Queries.pdf uitgewerkt in SQL queries

-- 1. Show CategoryName and Description from categories sorted by CategoryName
SELECT CategoryName, Description
FROM
nwcategory
ORDER BY CategoryName;

-- 2. Show ContactName, CompanyName, ContactTitle and Phone from customers sorted by Phone
SELECT ContactName, CompanyName, ContactTitle, Phone
FROM nwcustomer
ORDER BY Phone;

-- 3. Show capitalized FirstName, LastName and HireDate from employees sorted by HireDate
SELECT UPPER(FirstName) AS FirstName, UPPER(LastName) AS LastName, HireDate
FROM nwemployee
ORDER BY HireDate;

-- 4. Show top 10 orders by Freight (OrderID, OrderDate, ShippedDate, CustomerID, Freight)
SELECT OrderID, OrderDate, ShippedDate, CustomerID, Freight
FROM nworder
ORDER BY Freight DESC
LIMIT 10;

-- 5. Show all CustomerIDs in lowercase renamed as ID
SELECT LOWER(CustomerID) AS ID  
FROM nwcustomer;

-- 6. Show CompanyName, Fax, Phone, Country, HomePage from suppliers sorted by Country desc and CompanyName
SELECT CompanyName, Fax, Phone, Country, HomePage
FROM nwsupplier
ORDER BY Country DESC, CompanyName;

-- 7. Show CompanyName, ContactName of customers from Buenos Aires
SELECT CompanyName, ContactName
FROM nwcustomer
WHERE City = 'Buenos Aires';

-- 8. Show ProductName, UnitPrice, QuantityPerUnit of products that are out of stock
SELECT ProductName, UnitPrice, QuantityPerUnit  
FROM nwproduct
WHERE UnitsInStock = 0;

-- 9. Show ContactName, Address, City of customers not from Germany, Mexico, Spain
SELECT ContactName, Address, City
FROM nwcustomer
WHERE Country NOT IN ('Germany','Mexico','Spain');

-- 10. Show OrderDate, ShippedDate, CustomerID, Freight of orders placed on 21 May 1996
SELECT OrderDate, ShippedDate, CustomerID, Freight
FROM nworder
WHERE OrderDate = '1996-05-21';

-- 11. Show FirstName, LastName, Country of employees not from USA
SELECT FirstName, LastName, Country
FROM nwemployee
WHERE Country <> 'USA';

-- 12. Show EmployeeID, OrderID, CustomerID, RequiredDate, ShippedDate of orders shipped later than required
SELECT EmployeeID, OrderID, CustomerID, RequiredDate, ShippedDate
FROM nworder
WHERE ShippedDate > RequiredDate;

-- 13. Show City, CompanyName, ContactName of customers from cities starting with A or B
SELECT City, CompanyName, ContactName
FROM nwcustomer
WHERE City LIKE 'A%' OR City LIKE 'B%';

-- 14. Show all even OrderIDs
SELECT OrderID
FROM nworder
WHERE MOD(OrderID,2)=0;

-- 15. Show all orders where Freight > 500
SELECT *
FROM nworder
WHERE Freight > 500;

-- 16. Show ProductName, UnitsInStock, UnitsOnOrder, ReorderLevel of products up for reorder
SELECT ProductName, UnitsInStock, UnitsOnOrder, ReorderLevel
FROM nwproduct
WHERE ReorderLevel = 0;

-- 17. Show CompanyName, ContactName, Fax of customers with no Fax
SELECT CompanyName, ContactName, Fax
FROM nwcustomer
WHERE Fax IS NULL;

-- 18. Show FirstName, LastName of employees that do not report to anybody
SELECT FirstName, LastName
FROM nwemployee
WHERE ReportsTo IS NULL;

-- 19. Show all odd OrderIDs
SELECT OrderID
FROM nworder
WHERE MOD(OrderID,2)=1;

-- 20. Show CompanyName, ContactName, Fax of customers with no Fax sorted by ContactName
SELECT CompanyName, ContactName, Fax
FROM nwcustomer
WHERE Fax IS NULL
ORDER BY ContactName;

-- 21. Show City, CompanyName, ContactName of customers from cities containing letter L
SELECT City, CompanyName, ContactName, City
FROM nwcustomer
WHERE City LIKE '%L%'
ORDER BY ContactName;

-- 22. Show FirstName, LastName, BirthDate of employees born in the 1950s
SELECT FirstName, LastName, BirthDate
FROM nwemployee
WHERE BirthDate BETWEEN '1950-01-01' AND '1959-12-31';

-- 23. Show FirstName, LastName, year of Birthdate as BirthYear
SELECT LastName, FirstName, EXTRACT(YEAR FROM BirthDate) AS BirthYear
FROM nwemployee;

-- 24. Show OrderID, total number of OrderID as NumberOfOrders grouped and sorted
SELECT OrderID, COUNT(OrderID) AS NumberOfOrders
FROM nworderdetail
GROUP BY OrderID
ORDER BY NumberOfOrders DESC;
-- 25. Show SupplierID, ProductName, CompanyName from Exotic Liquids, Specialty Biscuits, Ltd., Escargots Nouveaux
SELECT s.SupplierID, p.ProductName, s.CompanyName
FROM nwsupplier s
JOIN nwproduct p ON s.SupplierID = p.SupplierID
WHERE s.CompanyName IN ('Exotic Liquids','Specialty Biscuits, Ltd.','Escargots Nouveaux')
ORDER BY s.SupplierID;

-- 26. Show ShipPostalCode, OrderID, OrderDate, RequiredDate, ShippedDate, ShipAddress where ShipPostalCode = 98124
SELECT ShipPostalCode, OrderID, OrderDate, RequiredDate, ShippedDate, ShipAddress
FROM nworder
WHERE ShipPostalCode = '98124';

-- 27. Show ContactName, ContactTitle, CompanyName of customers without 'Sales' in ContactTitle
SELECT ContactName, ContactTitle, CompanyName
FROM nwcustomer
WHERE ContactTitle NOT LIKE '%Sales%';

-- 28. Show LastName, FirstName, City of employees not in Seattle
SELECT LastName, FirstName, City
FROM nwemployee
WHERE City != 'Seattle';

-- 29. Show CompanyName, ContactTitle, City, Country of customers in Mexico or Spain except Madrid
SELECT CompanyName, ContactTitle, City, Country
FROM nwcustomer
WHERE Country IN ('Mexico','Spain')
AND City <> 'Madrid';

-- 30. Show employee contact info concatenated with extension
SELECT CONCAT(FirstName,' ',LastName,' can be reached at x',Extension) AS ContactInfo
FROM nwemployee;

-- 31. Show ContactName of customers where 2nd letter is not A
SELECT ContactName
FROM nwcustomer WHERE ContactName NOT LIKE '_A%';


-- 32. Show average UnitPrice, total UnitsInStock, max UnitsOnOrder
SELECT ROUND(AVG(UnitPrice),0) AS AveragePrice, SUM(UnitsInStock) AS TotalStock, MAX(UnitsOnOrder) AS MaxOrder
FROM nwproduct;

-- 33. Show SupplierID, CompanyName, CategoryName, ProductName, UnitPrice
SELECT s.SupplierID, s.CompanyName, c.CategoryName, p.ProductName, p.UnitPrice
FROM nwproduct p
JOIN nwsupplier s ON s.SupplierID = p.SupplierID
JOIN nwcategory c ON c.CategoryID = p.CategoryID;

-- 34. Show CustomerID, sum of Freight grouped and having >200
SELECT CustomerID, SUM(Freight)
FROM nworder
GROUP BY CustomerID
HAVING SUM(Freight) > 200;

-- 35. Show OrderID, ContactName, UnitPrice, Quantity, Discount where discount applied
SELECT od.OrderID, c.ContactName, od.UnitPrice, od.Quantity, od.Discount
FROM nworderdetail od
JOIN nworder o ON od.OrderID = o.OrderID
JOIN nwcustomer c ON c.CustomerID = o.CustomerID
WHERE od.Discount != 0;

-- 36. Show Employee and their Manager using self join
SELECT a.EmployeeID, CONCAT(a.LastName,' ',a.FirstName) AS Employee, CONCAT(b.LastName,' ',b.FirstName) AS Manager
FROM nwemployee a
LEFT JOIN nwemployee b ON b.EmployeeID = a.ReportsTo
ORDER BY a.EmployeeID;

-- 37. Show average, minimum and maximum UnitPrice of all products
SELECT AVG(UnitPrice) AS AveragePrice, MIN(UnitPrice) AS MinimumPrice, MAX(UnitPrice) AS MaximumPrice
FROM nwproduct;

-- 38. Create view CustomerInfo with customer and order details
CREATE VIEW CustomerInfo AS
SELECT
c.CustomerID, c.CompanyName, c.ContactName, c.ContactTitle, c.Address, c.City, c.Country, c.Phone, o.OrderDate, o.RequiredDate, o.ShippedDate
FROM nwcustomer c
JOIN nworder o ON c.CustomerID = o.CustomerID;

-- 39. Rename view CustomerInfo to CustomerDetails
RENAME TABLE CustomerInfo TO CustomerDetails;

-- 40. Create view ProductDetails with product, supplier, category info
CREATE VIEW ProductDetails AS
SELECT
p.ProductID, s.CompanyName, p.ProductName, c.CategoryName, c.Description, p.QuantityPerUnit, p.UnitPrice, p.UnitsInStock, p.UnitsOnOrder, p.ReorderLevel,
p.Discontinued
FROM nwsupplier s
JOIN nwproduct p ON s.SupplierID = p.SupplierID
JOIN nwcategory c ON c.CategoryID = p.CategoryID;

-- 41. Drop CustomerDetails view
DROP VIEW IF EXISTS CustomerDetails;

-- 42. Show first 5 characters of CategoryName as Short_info
SELECT SUBSTRING(CategoryName,1,5) AS Short_info
FROM nwcategory;

-- 43. Duplicate shippers table as nwshipper_dup
DROP TABLE IF EXISTS nwshipper_dup;
CREATE TABLE nwshipper_dup LIKE nwshipper;
INSERT INTO nwshipper_dup SELECT * FROM nwshipper;

-- 44. Add Email column to nwshipper_dup and update with values
ALTER TABLE nwshipper_dup ADD COLUMN Email VARCHAR(50);
UPDATE nwshipper_dup SET Email ='[email protected]' WHERE ShipperID = 1;
UPDATE nwshipper_dup SET Email ='[email protected]' WHERE ShipperID = 2;
UPDATE nwshipper_dup SET Email ='[email protected]' WHERE ShipperID = 3;

-- 45. Show CompanyName, ProductName of products in Seafood category
SELECT s.CompanyName, p.ProductName
FROM nwcategory c
JOIN nwproduct p ON c.CategoryID = p.CategoryID
JOIN nwsupplier s ON s.SupplierID = p.SupplierID
WHERE c.CategoryName = 'Seafood';

-- 46. Show CategoryID, CompanyName, ProductName of products in CategoryID 5
SELECT c.CategoryID, s.CompanyName, p.ProductName
FROM nwcategory c
JOIN nwproduct p ON c.CategoryID = p.CategoryID
JOIN nwsupplier s ON s.SupplierID = p.SupplierID
WHERE c.CategoryID = 5;

-- 47. Drop nwshipper_dup table
DROP TABLE IF EXISTS nwshipper_dup;

-- 48. Show employees with calculated Age
SELECT LastName, FirstName, Title, DATE_FORMAT(FROM_DAYS(DATEDIFF(CURRENT_DATE, BirthDate)),'%y Years') AS Age
FROM nwemployee;

-- 49. Show CompanyName and number of orders >10 since Dec 31, 1994
SELECT c.CompanyName, COUNT(c.CustomerID) AS NumberOfOrders
FROM nwcustomer c
JOIN nworder o ON o.CustomerID = c.CustomerID
WHERE o.OrderDate >= '1994-12-31'
GROUP BY c.CustomerID
HAVING COUNT(c.CustomerID) > 10;

-- 50. Show Product info concatenated with unit and price
SELECT CONCAT(ProductName,' weighs/is ',QuantityPerUnit,' and cost $',ROUND(UnitPrice)) AS ProductInfo
FROM nwproduct;