Advertisement
Vita_Harvey

Week 2 lecture notes

Apr 22nd, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 4.42 KB | None | 0 0
  1. #USE selects the database to be queried or altered.
  2. # This is one way of writing comments, using hashtags the way Python does.
  3. USE ap;
  4.  
  5. -- This is also a comment.  There must be a space after the dashes to work.
  6. DESCRIBE invoices;
  7.  
  8. /*
  9. This is a way of writing
  10. multi-line comments.
  11. */
  12. # Can list several operations, separating them with commas.
  13. SELECT 4 * 53, 11 MOD 2, 20 * 5 + 2;
  14.  
  15. # Will show all the rows and columns associated with invoices table.
  16. # (Select all columns, all rows by default).
  17. SELECT *
  18. FROM invoices;
  19.  
  20. # specify certain columns
  21. SELECT invoice_number, invoice_date, credit_total
  22. FROM invoice_archive;
  23.  
  24. # AS allows visual renaming of columns in output.
  25. SELECT invoice_number AS 'ID', invoice_date AS 'date', invoice_total AS 'total'
  26. FROM invoices;
  27.  
  28. # Can specify format output precision and with commas,
  29. # as shown below, using FORMAT;
  30. # FORMAT returns output as text, however, not numbers.
  31. # (Not required).
  32. SELECT invoice_id AS 'Id', FORMAT(invoice_total * 1.034, 2) AS 'total times some stuff'
  33. FROM invoices;
  34.  
  35. # DISTINCT prevents outputting duplicate values.
  36. # The more lists, the fewer duplicates will be caught,
  37. # since it will be comparing sets of data rather than just multiple columns.
  38. # (Affects every column on the line after it in list, taken as a set).
  39. SELECT DISTINCT vendor_id
  40. FROM invoices;
  41.  
  42. # show all data from ex's active_invoices table.
  43. USE ex;
  44. SELECT * FROM active_invoices;
  45.  
  46. # show all data from active_invoices table of
  47. # ex's invoice numbers, invoice dates, and invoice totals.
  48. SELECT invoice_number, invoice_date, invoice_total
  49. FROM active_invoices;
  50.  
  51. # show all invoice nums plus the difference beetween
  52. # the invoice total and the credit total.
  53. SELECT invoice_number, invoice_total - credit_total
  54. FROM active_invoices;
  55.  
  56. # same as above, but rename columns in displayed output.
  57. SELECT invoice_number AS 'invoice', invoice_total - credit_total AS 'remaining amount'
  58. FROM active_invoices;
  59.  
  60. # To display last name and first name as distinct columns.
  61. SELECT customer_last_name AS 'last name', customer_first_name AS 'first name'
  62. FROM customers;
  63.  
  64. # Concatenate the first and last name columns together in
  65. # last, first format (with comma).
  66. SELECT CONCAT(customer_last_name, ', ', customer_first_name) AS 'name'
  67. FROM customers;
  68.  
  69. # Conditions: used to filter results
  70. SELECT customer_last_name AS 'post-M last names'
  71. FROM customers
  72. WHERE customer_last_name>'M';
  73.  
  74. # Returns nothing because no literal that says "Illinois"
  75. SELECT CONCAT(customer_last_name, ", ", customer_first_name)
  76. FROM customers
  77. WHERE customer_state='Illinois';
  78.  
  79. SELECT CONCAT(customer_last_name, ", ", customer_first_name) AS 'full name'
  80. FROM customers
  81. WHERE customer_state='IL';
  82.  
  83. SELECT customer_last_name AS 'last name', customer_first_name AS 'first name'
  84. FROM customers
  85. WHERE customer_state='WA' OR customer_state='OR';
  86.  
  87. # Displays last names of all customers whose last name is Jones or comes after.
  88. SELECT customer_last_name
  89. FROM customers
  90. WHERE customer_last_name > 'Jones';
  91.  
  92. # Display phone numbers to observe format.
  93. SELECT customer_phone
  94. FROM customers;
  95.  
  96. # Return all customers who have phone numbers
  97. # in the 503 area code.
  98. SELECT customer_last_name, customer_first_name
  99. FROM customers
  100. WHERE customer_phone LIKE '(503)%';
  101.  
  102. SELECT customer_last_name, customer_first_name
  103. FROM customers
  104. WHERE customer_state IN ('WA', 'ID', 'WY');
  105.  
  106. # Sort your output by listing one or more fields in the ORDER BY clause
  107. # e.g. ORDER BY last_name
  108. # or multi-level sort: ORDER BY last_name, first_name
  109. SELECT customer_address AS 'address'
  110. FROM customers
  111. WHERE customer_state > 'M'
  112. ORDER BY customer_last_name, customer_first_name;
  113.  
  114. #
  115. SELECT *
  116. FROM active_invoices
  117. WHERE invoice_date LIKE '2014-04%' AND invoice_total > 1500
  118. ORDER BY invoice_date DESC;
  119.  
  120. # Get a list of vendor IDs for invoices whose terms ID is 4 or 5.
  121. # No duplicates, sort by vendor ID.
  122. SELECT DISTINCT vendor_id as 'Vendor ID'
  123. FROM active_invoices
  124. WHERE terms_id IN (4, 5)
  125. ORDER BY vendor_id;
  126.  
  127. SELECT invoice_number
  128. FROM active_invoices
  129. WHERE invoice_number LIKE '%-%';
  130.  
  131. SELECT invoice_number
  132. FROM active_invoices
  133. WHERE invoice_number NOT LIKE '%-%';
  134.  
  135.     SELECT customer_last_name, customer_first_name, 'Accounting' AS Dept
  136.     FROM customers
  137.     WHERE customer_city LIKE 'S%'
  138. UNION
  139.     SELECT customer_last_name, customer_first_name, 'Marketing' AS Dept
  140.     FROM customers
  141.     WHERE customer_city LIKE 'L%';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement