Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 3.14 KB | None | 0 0
  1. USE classicmodels;
  2.  
  3. -- 1
  4. SELECT city
  5.     FROM customers
  6.     WHERE country = 'Australia'
  7.     ORDER BY city asc;
  8.  
  9. -- 2
  10. SELECT EmployeeNumber, LastName, FirstName, Extension
  11.     FROM employees
  12.     WHERE officeCode = 6;
  13.  
  14. -- 3
  15. SELECT ProductCode, ProductName, ProductVendor, QuantityInStock, ProductLine
  16.     FROM products
  17.     WHERE (quantityInStock > 4000 AND quantityInStock < 5000);
  18.  
  19. -- 4
  20. SELECT ProductCode, ProductName, ProductVendor, BuyPrice, max(MSRP) as MSRP
  21.     FROM products;
  22.  
  23. -- 5
  24. SELECT ProductName, MSRP, BuyPrice, (MSRP - BuyPrice) as Margin
  25.     FROM products
  26.     ORDER BY Margin ASC
  27.     LIMIT 1;
  28.  
  29. -- 6
  30. SELECT country, count(customerNumber) as Customers
  31.     FROM customers
  32.     GROUP BY country
  33.     HAVING count(country) > 5
  34.     ORDER BY Customers ASC;
  35.  
  36. -- 7
  37. SELECT O.productCode, P.productName, SUM(O.quantityOrdered) AS OrderCount
  38.     FROM OrderDetails O, Products P
  39.     GROUP BY P.productCode, O.productCode
  40.     ORDER BY OrderCount DESC
  41.     LIMIT 1;
  42.  
  43. -- 8
  44. SELECT CONCAT(managers.FirstName, ' ', managers.LastName) AS `Managers`, CONCAT(employees.FirstName, ' ', employees.LastName) AS `Workers`
  45.     FROM employees
  46.         LEFT JOIN employees AS managers
  47.         ON employees.ReportsTo = managers.employeeNumber
  48.     ORDER BY managers.employeeNumber;
  49.  
  50. -- 9
  51. SELECT EmployeeNumber, LastName, FirstName
  52.     FROM Employees
  53.     WHERE ReportsTo IS NULL;
  54.    
  55. -- 10
  56. SELECT ProductName
  57.     FROM Products
  58.     WHERE ProductName LIKE '196%';
  59.    
  60. -- 11
  61. SELECT monthname(orderDate) as Month, year(orderDate) as Year, count(*) as orderCount
  62.     FROM Orders
  63.     GROUP BY monthname(orderDate), year(orderDate)
  64.     ORDER BY count(*) DESC
  65.     LIMIT 2;
  66.    
  67. -- 12
  68. SELECT DISTINCT E.firstName, E.lastName
  69.     FROM Employees E LEFT OUTER JOIN Customers C
  70.     ON E.employeeNumber = C.salesRepEmployeeNumber
  71.     WHERE (C.salesRepEmployeeNumber IS NULL
  72.         AND E.jobTitle = 'Sales Rep');
  73.  
  74. -- 13
  75. SELECT customerName
  76.     FROM customers
  77.     WHERE creditLimit = 0 AND country = 'Switzerland';
  78.  
  79. -- 14
  80. SELECT customerName, sum(quantityOrdered)
  81.     FROM OrderDetails, Customers, Orders
  82.     WHERE (OrderDetails.orderNumber = Orders.orderNumber
  83.         AND Orders.customerNumber = Customers.customerNumber)
  84.     GROUP BY customerName
  85.     HAVING sum(quantityOrdered) < 500;
  86.  
  87. -- 15
  88. CREATE TABLE LowCustomers(
  89.     CustomerNumber int(45) NOT NULL,
  90.     ContactDate date NOT NULL,
  91.     OrderTotal decimal(7, 2) NOT NULL,
  92.     CONSTRAINT LowCustomer_PK PRIMARY KEY (CustomerNumber));
  93.  
  94. -- 16  
  95. INSERT INTO LowCustomers (CustomerNumber, ContactDate, OrderTotal)
  96.     SELECT o.customerNumber, CURDATE(), SUM(od.priceEach * od.quantityOrdered) as TotalValue
  97.         FROM Orders o, OrderDetails od
  98.         WHERE o.orderNumber = od.orderNumber
  99.         GROUP BY o.customerNumber
  100.         HAVING SUM(od.priceEach * od.quantityOrdered) < 50000;
  101.    
  102. -- 17
  103. SELECT *
  104.     FROM LowCustomers
  105.     ORDER BY orderTotal DESC;
  106.  
  107. -- 18
  108. ALTER TABLE LowCustomers
  109.     ADD orderCount int(45)
  110.     AFTER orderTotal;
  111.  
  112. -- 19
  113. UPDATE LowCustomers
  114.     SET orderCount = FLOOR(RAND()*(18-1+1)+1)
  115.     WHERE orderCount IS NULL;
  116.    
  117. -- 20
  118. SELECT *
  119.     FROM LowCustomers
  120.     ORDER BY orderCount DESC;
  121.  
  122. -- 21  
  123. DROP TABLE LowCustomers;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement