|
| 1 | +/**********************************************************************/ |
| 2 | +/* SQL Queries: Practice your SQL Knowledge! */ |
| 3 | +/**********************************************************************/ |
| 4 | + |
| 5 | +/**********************************************************************/ |
| 6 | +/* Credit to Schema : https://github.com/AndrejPHP/w3schools-database */ |
| 7 | +/**********************************************************************/ |
| 8 | +/* Run w3schools.sql to set up database, tables and data*/ |
| 9 | + |
| 10 | +/* |
| 11 | +----Schema---- |
| 12 | +Customers (CustomerID, CustomerName, ContactName, Address, City, PostalCode, Country) |
| 13 | +Categories (CategoryID,CategoryName, Description) |
| 14 | +Employees (EmployeeID, LastName, FirstName, BirthDate, Photo, Notes) |
| 15 | +OrderDetails(OrderDetailID, OrderID, ProductID, Quantity) |
| 16 | +Orders (OrderID, CustomerID, EmployeeID, OrderDate, ShipperID) |
| 17 | +Products(ProductID, ProductName, SupplierID, CategoryID, Unit, Price) |
| 18 | +Shippers (ShipperID, ShipperName, Phone) |
| 19 | +*/ |
| 20 | + |
| 21 | +/**** Advanced Level *****/ |
| 22 | + |
| 23 | +/*1. Select customer name together with each order the customer made*/ |
| 24 | +SELECT CustomerName, OrderID |
| 25 | +FROM customers c |
| 26 | +JOIN orders o |
| 27 | +ON c.CustomerID = o.CustomerID; |
| 28 | + |
| 29 | +/*2. Select order id together with name of employee who handled the order*/ |
| 30 | +SELECT o.OrderID, e.EmployeeID, e.FirstName, e.LastName |
| 31 | +FROM orders o |
| 32 | +JOIN employees e |
| 33 | +ON o.EmployeeID = e.EmployeeID; |
| 34 | + |
| 35 | +/*3. Select customers who did not placed any order yet*/ |
| 36 | +SELECT c.CustomerID, c.CustomerName, o.OrderID |
| 37 | +FROM customers c |
| 38 | +LEFT JOIN orders o |
| 39 | +ON c.CustomerID = o.CustomerID |
| 40 | +WHERE o.CustomerID IS NULL; |
| 41 | + |
| 42 | +/*4. Select order id together with the name of products*/ |
| 43 | +SELECT o.OrderID, p.ProductID, p.ProductName |
| 44 | +FROM orders o |
| 45 | +JOIN order_details od ON o.OrderID = od.OrderID |
| 46 | +JOIN products p ON p.ProductID = od.ProductID |
| 47 | +ORDER BY o.OrderID; |
| 48 | + |
| 49 | +/*5. Select products that no one bought*/ |
| 50 | +SELECT p.ProductID, p.ProductName, od.OrderID |
| 51 | +FROM products p |
| 52 | +LEFT JOIN order_details od ON p.ProductID = od.ProductID |
| 53 | +WHERE od.OrderID IS NULL; |
| 54 | + |
| 55 | +/*6. Select customer together with the products that he bought*/ |
| 56 | +SELECT c.CustomerID, c.CustomerName, p.ProductName |
| 57 | +FROM customers c |
| 58 | +JOIN orders o ON o.CustomerID = c.CustomerID |
| 59 | +JOIN order_details od ON od.OrderID = o.OrderID |
| 60 | +JOIN products p ON p.ProductID = od.ProductID |
| 61 | +ORDER BY c.CustomerID, p.ProductName ASC; |
| 62 | + |
| 63 | +/*7. Select product names together with the name of corresponding category*/ |
| 64 | +SELECT p.ProductID, p.ProductName, c.CategoryName |
| 65 | +FROM products p |
| 66 | +JOIN categories c |
| 67 | +ON p.CategoryID = c.CategoryID; |
| 68 | + |
| 69 | +/*8. Select orders together with the name of the shipping company*/ |
| 70 | +SELECT o.OrderID, o.CustomerID, o.EmployeeID, o.OrderDate, shp.ShipperName |
| 71 | +FROM orders o |
| 72 | +JOIN shippers shp |
| 73 | +ON o.ShipperID = shp.ShipperID |
| 74 | +ORDER BY o.OrderID; |
| 75 | + |
| 76 | +/*9. Select customers with id greater than 50 together with each order they made*/ |
| 77 | +SELECT c.CustomerID, c.CustomerName, o.OrderID |
| 78 | +FROM customers c |
| 79 | +JOIN orders o |
| 80 | +ON c.CustomerID = o.CustomerID |
| 81 | +WHERE c.CustomerID > 50; |
| 82 | + |
| 83 | +/*10. Select employees together with orders with order id greater than 10400*/ |
| 84 | +SELECT o.OrderID, e.EmployeeID, e.FirstName, e.LastName |
| 85 | +FROM orders o |
| 86 | +JOIN employees e |
| 87 | +ON o.EmployeeID = e.EmployeeID |
| 88 | +WHERE o.OrderID > 10400; |
0 commit comments