Browse Source

oef

master
Willy Vervest 2 years ago
parent
commit
9818b2c5d8
97 changed files with 670 additions and 0 deletions
  1. 4
    0
      Exec_3_21-C.SQL
  2. 3
    0
      Exec_3_21.sql
  3. 9
    0
      Exec_3_22-C.SQL
  4. 9
    0
      Exec_3_22.sql
  5. 6
    0
      Exec_3_23-C.SQL
  6. 6
    0
      Exec_3_23.sql
  7. 13
    0
      Exec_3_24-C.SQL
  8. 12
    0
      Exec_3_24.SQL
  9. 6
    0
      Exec_3_25-C.sql
  10. 6
    0
      Exec_3_25.sql
  11. 7
    0
      Exec_3_26.sql
  12. 10
    0
      Exec_3_27.sql
  13. 4
    0
      Exec_4_21.sql
  14. 2
    0
      Exec_4_22.sql
  15. 3
    0
      Exec_4_23.sql
  16. 2
    0
      Exec_4_24.sql
  17. 3
    0
      Exec_4_25.sql
  18. 6
    0
      Exec_4_26.sql
  19. 4
    0
      Exec_4_27.sql
  20. 1
    0
      Exec_4_28.sql
  21. 0
    0
      Exec_4_29.sql
  22. 8
    0
      exec_5C31.sql
  23. 0
    0
      exec_5C32.sql
  24. 7
    0
      exec_5C33.sql
  25. 8
    0
      exec_5C34.sql
  26. 6
    0
      exec_5C35.sql
  27. 8
    0
      exec_5C36.sql
  28. 9
    0
      exec_5C37.sql
  29. 7
    0
      exec_5C38.sql
  30. 8
    0
      exec_5C39.sql
  31. 5
    0
      exec_5C61.sql
  32. 9
    0
      exec_5C62.sql
  33. 8
    0
      exec_5C63.sql
  34. 3
    0
      exec_5C64.sql
  35. 4
    0
      exec_5C65.sql
  36. 9
    0
      exec_5C66.sql
  37. 9
    0
      exec_5C67.sql
  38. 6
    0
      exec_5C68.sql
  39. 11
    0
      exec_5C69.sql
  40. 6
    0
      exec_5C70.sql
  41. 8
    0
      exec_5C71.sql
  42. 6
    0
      exec_5C72.sql
  43. 9
    0
      exec_5C73.sql
  44. 8
    0
      exec_5C74.sql
  45. 8
    0
      exec_5C75.sql
  46. 8
    0
      exec_5C76.sql
  47. 4
    0
      exer_5C1.sql
  48. 7
    0
      exer_5C10.sql
  49. 6
    0
      exer_5C11.sql
  50. 10
    0
      exer_5C12.sql
  51. 5
    0
      exer_5C13.sql
  52. 6
    0
      exer_5C14.sql
  53. 7
    0
      exer_5C15.sql
  54. 6
    0
      exer_5C16.sql
  55. 6
    0
      exer_5C17.sql
  56. 11
    0
      exer_5C18.sql
  57. 10
    0
      exer_5C19.sql
  58. 3
    0
      exer_5C2.sql
  59. 21
    0
      exer_5C20.sql
  60. 9
    0
      exer_5C21.sql
  61. 12
    0
      exer_5C22.sql
  62. 7
    0
      exer_5C23.sql
  63. 5
    0
      exer_5C24.sql
  64. 5
    0
      exer_5C25.sql
  65. 6
    0
      exer_5C26.sql
  66. 30
    0
      exer_5C27.sql
  67. 14
    0
      exer_5C28.sql
  68. 7
    0
      exer_5C29.sql
  69. 4
    0
      exer_5C3.sql
  70. 7
    0
      exer_5C30.sql
  71. 3
    0
      exer_5C4.sql
  72. 8
    0
      exer_5C40.sql
  73. 8
    0
      exer_5C41.sql
  74. 6
    0
      exer_5C42.sql
  75. 8
    0
      exer_5C43.sql
  76. 8
    0
      exer_5C44.sql
  77. 8
    0
      exer_5C45.sql
  78. 9
    0
      exer_5C46.sql
  79. 5
    0
      exer_5C47.sql
  80. 8
    0
      exer_5C48.sql
  81. 0
    0
      exer_5C49.sql
  82. 3
    0
      exer_5C5.sql
  83. 7
    0
      exer_5C50.sql
  84. 9
    0
      exer_5C51.sql
  85. 8
    0
      exer_5C52.sql
  86. 12
    0
      exer_5C53.sql
  87. 10
    0
      exer_5C54.sql
  88. 5
    0
      exer_5C55.sql
  89. 6
    0
      exer_5C56.sql
  90. 5
    0
      exer_5C57.sql
  91. 5
    0
      exer_5C58.sql
  92. 7
    0
      exer_5C59.sql
  93. 3
    0
      exer_5C6.sql
  94. 2
    0
      exer_5C60.sql
  95. 3
    0
      exer_5C7.sql
  96. 5
    0
      exer_5C8.sql
  97. 7
    0
      exer_5C9.sql

+ 4
- 0
Exec_3_21-C.SQL View File

@@ -0,0 +1,4 @@
Select *
FROM vendors
INNER JOIN invoices
ON vendors.vendors_id = invoice.vendors_id;

+ 3
- 0
Exec_3_21.sql View File

@@ -0,0 +1,3 @@
Select *
FROM vendors
INNER JOIN invoices;

+ 9
- 0
Exec_3_22-C.SQL View File

@@ -0,0 +1,9 @@
SELECT
vendor_name,
invoice_number,
invoice_date,
invoice_total - payment_total - credit_total AS balance_due
From vendors JOIN invoices
on vendors.vendors_id = invoice.vendors_id
WHERE invoice_total - payment_total - credit_total <>
ORDER BY vendor_name ASC;

+ 9
- 0
Exec_3_22.sql View File

@@ -0,0 +1,9 @@
SELECT
vendor_name,
invoice_number,
invoice_date,
invoice_total - payment_total - credit_total AS balance_due
From vendors v,
invoices i
WHERE invoice_total IS NULL
ORDER BY vendor_name ASC;

+ 6
- 0
Exec_3_23-C.SQL View File

@@ -0,0 +1,6 @@
SELECT vendor_name,
default_account_number AS default_account,
account_description AS description
FROM vendors v JOIN general_ledger_accounts g
ON vendors.default_account_number = general_ledger_accounts.default_account_number
ORDER BY default_account, vendor_name;

+ 6
- 0
Exec_3_23.sql View File

@@ -0,0 +1,6 @@
SELECT vendor_name,
default_account_number AS default_account,
account_description AS description
FROM vendors v,
general_ledger_accounts g
ORDER BY default_account, vendor_name;

+ 13
- 0
Exec_3_24-C.SQL View File

@@ -0,0 +1,13 @@
SELECT vendor_name,
invoice_date,
invoice_number,
invoice_sequence AS li_sequence,
line_item_amount AS li_amount
FROM vendors v JOIN invoices i,
ON v.vendors_id = i.verdors_id
JOIN Invoice_Line_Items li
ON i.invoice_id = li.invoice.id
ORDER BY vendor_name,
invoice_date,
invoice_number,
invoice_sequence;

+ 12
- 0
Exec_3_24.SQL View File

@@ -0,0 +1,12 @@
SELECT vendor_name,
invoice_date,
invoice_number,
invoice_sequence AS li_sequence,
line_item_amount AS li_amount
FROM vendors v,
invoices i,
Invoice_Line_Items l
ORDER BY vendor_name,
invoice_date,
invoice_number,
invoice_sequence;

+ 6
- 0
Exec_3_25-C.sql View File

@@ -0,0 +1,6 @@
SELECT vendor_id,
concat(vendor_contact_first_name, vendor_contact_last_name) AS contact_name
FROM vendor_contacts v JOIN vendor_contacts v1
ON v.vendor_id <> v1.vendors_id v1
AND v.vendor_contact_last_name = v1.vendor_contact_last_name
ORDER BY v.vendor_contact_last_name ASC;

+ 6
- 0
Exec_3_25.sql View File

@@ -0,0 +1,6 @@
SELECT vendor_id,
vendor_name,
concat(vendor_contact_first_name, vendor_contact_last_name) AS contact_name
FROM vendor v
WHERE vendor_id = vendor_contact_last_name
ORDER BY vendor_contact_last_name;

+ 7
- 0
Exec_3_26.sql View File

@@ -0,0 +1,7 @@
SELECT account_number,
account_description,
invoice_id
FROM invoice_line_items i LEFT OUTER JOIN general_ledger_accounts g
ON i.account_number = g.account_number
WHERE invoice_id = NULL
ORDER BY g.account_number ASC;

+ 10
- 0
Exec_3_27.sql View File

@@ -0,0 +1,10 @@
SELECT vendor_name, vendor_state
FROM vendors v1 JOIN vendors v2
ON v1.vendor_name = v2.vendor_name
WHERE v1.vendor_state = "CA" otherwise v1.vendor_state = "OUTSIDE CA"
UNION
SELECT vendor_name, vendor_state
FROM vendors v1 JOIN vendors v2
ON v1.vendor_name = v2.vendor_name
WHERE v1.vendor_state = "CA" otherwise v1.vendor_state = "OUTSIDE CA"
ORDER BY vendor_name ASC;

+ 4
- 0
Exec_4_21.sql View File

@@ -0,0 +1,4 @@
INSERT INTO terms
(terms_id,terms_description,terms_due_days)
VALUES
(6,'Net due 120 days',120);

+ 2
- 0
Exec_4_22.sql View File

@@ -0,0 +1,2 @@
UPDATE terms
SET terms_description = 'Net due 125 days';

+ 3
- 0
Exec_4_23.sql View File

@@ -0,0 +1,3 @@
use ap;
DELETE FROM terms
WHERE terms_id = 6;

+ 2
- 0
Exec_4_24.sql View File

@@ -0,0 +1,2 @@
INSERT INTO invoices VALUES
(DEFAULT, 32, 'AX-014-027', '2018-01-08', 454.58, 0, 0, 2,'2018-08-31', NULL)

+ 3
- 0
Exec_4_25.sql View File

@@ -0,0 +1,3 @@
INSERT INTO invoices_line_items VALUES
(123,1, 160, 180.23, 'HARD DRIVE'),
(123,2, 527, 254.35, 'Exchange Server update');

+ 6
- 0
Exec_4_26.sql View File

@@ -0,0 +1,6 @@
use ap;
select * from invoices
UPDATE invoices
SET credit_total = invoice_total * 0.1
payement_total = invoice_total - credit_total
WHERE invoice_id = '';

+ 4
- 0
Exec_4_27.sql View File

@@ -0,0 +1,4 @@
use ap;
UPDATE vendors
SET account_number = 403
WHERE vendor_id = 44;

+ 1
- 0
Exec_4_28.sql View File

@@ -0,0 +1 @@
select * from invoices

+ 0
- 0
Exec_4_29.sql View File


+ 8
- 0
exec_5C31.sql View File

@@ -0,0 +1,8 @@
use ap;

SELECT vendor_id, vendor_name, vendor_state
FROM vendors
WHERE NOT EXISTS
(SELECT *
FROM invoices
WHERE vendor_id = vendors.vendor_id);

+ 0
- 0
exec_5C32.sql View File


+ 7
- 0
exec_5C33.sql View File

@@ -0,0 +1,7 @@
SELECT vendor_name, invoice_number, invoice_total
FROM vendors JOIN invoices
ON vendors.vendor_id = invoices.vendor_id
WHERE invoice_total < ANY
(SELECT invoice_total
FROM invoices
WHERE vendor_id = 115);

+ 8
- 0
exec_5C34.sql View File

@@ -0,0 +1,8 @@
USE AP;
SELECT vendor_name, invoice_number, invoice_total
FROM invoices i JOIN vendors v ON i.vendor_id = v.vendor_id
WHERE invoice_total > ALL
(SELECT invoice_total
FROM invoices
WHERE vendor_id = 34)
ORDER BY vendor_name;

+ 6
- 0
exec_5C35.sql View File

@@ -0,0 +1,6 @@
USE AP;
SELECT v.vendor_id, vendor_name, vendor_state
FROM vendors v LEFT JOIN invoices i
ON v.vendor_id = i.vendor_id
WHERE i.vendor_id IS NULL
ORDER BY v.vendor_id;

+ 8
- 0
exec_5C36.sql View File

@@ -0,0 +1,8 @@
USE AP;

SELECT vendor_id, vendor_name, vendor_state
FROM vendors
WHERE vendor_id NOT IN
(SELECT DISTINCT vendor_id
FROM invoices)
ORDER BY vendor_id;

+ 9
- 0
exec_5C37.sql View File

@@ -0,0 +1,9 @@
USE AP;

SELECT invoice_number, invoice_date, invoice_total
FROM invoices
WHERE vendor_id IN
(SELECT vendor_id
FROM vendors
WHERE vendor_state = 'CA')
ORDER BY invoice_date;

+ 7
- 0
exec_5C38.sql View File

@@ -0,0 +1,7 @@
USE AP;

SELECT invoice_number, invoice_date, invoice_total
FROM invoices JOIN vendors
ON invoices.vendor_id = vendors.vendor_id
WHERE vendor_state = 'CA'
ORDER BY invoice_date;

+ 8
- 0
exec_5C39.sql View File

@@ -0,0 +1,8 @@
USE AP;

SELECT invoice_number, invoice_date, invoice_total
FROM invoices
WHERE invoice_total >
(SELECT AVG(invoice_total)
FROM invoices)
ORDER BY invoice_total;

+ 5
- 0
exec_5C61.sql View File

@@ -0,0 +1,5 @@
USE AP;

DELETE FROM general_ledger_accounts
WHERE account_number = 306


+ 9
- 0
exec_5C62.sql View File

@@ -0,0 +1,9 @@
USE AP;

SELECT invoice_number, vendor_name, invoice_due_date,
invoice_total - payment_total - credit_total
AS balance_due
FROM vendors v JOIN invoices i
ON v.vendor_id = i.vendor_id
WHERE invoice_total - payment_total - credit_total > 0
ORDER BY invoice_due_date DESC

+ 8
- 0
exec_5C63.sql View File

@@ -0,0 +1,8 @@
USE AP;

SELECT invoice_number, line_item_amount,
line_item_description
FROM invoices JOIN invoice_line_items line_items
ON invoices.invoice_id = line_items.invoice_id
WHERE account_number = 540
ORDER BY invoice_date

+ 3
- 0
exec_5C64.sql View File

@@ -0,0 +1,3 @@
SELECT vendor_name, customer_last_name, customer_first_name, vendor_state AS state, vendor_city AS city FROM vendors v
JOIN om.customers c
ON v.vendor_zip_code = c.customer_zip ORDER BY state, city

+ 4
- 0
exec_5C65.sql View File

@@ -0,0 +1,4 @@
SELECT customer_first_name, customer_last_name
FROM customers c JOIN employees e
ON c.customer_first_name = e.first_name
AND c.customer_last_name = e.last_name

+ 9
- 0
exec_5C66.sql View File

@@ -0,0 +1,9 @@
USE ap;

SELECT DISTINCT v1.vendor_name, v1.vendor_city,
v1.vendor_state
FROM vendors v1 JOIN vendors v2
ON v1.vendor_city = v2.vendor_city AND
v1.vendor_state = v2.vendor_state AND
v1.vendor_name <> v2.vendor_name
ORDER BY v1.vendor_state, v1.vendor_city;

+ 9
- 0
exec_5C67.sql View File

@@ -0,0 +1,9 @@
use ap;

SELECT vendor_name, invoice_number, invoice_date, line_item_amount, account_description
FROM vendors v
JOIN invoices i
ON v.vendor_id = i.vendor_id JOIN invoice_line_items li
ON i.invoice_id = li.invoice_id JOIN general_ledger_accounts gl
ON li.account_number = gl.account_number WHERE invoice_total - payment_total - credit_total > 0
ORDER BY vendor_name, line_item_amount DESC

+ 6
- 0
exec_5C68.sql View File

@@ -0,0 +1,6 @@
USE ap;

SELECT invoice_number, vendor_name
FROM vendors v, invoices i
WHERE v.vendor_id = i.vendor_id
ORDER BY invoice_number

+ 11
- 0
exec_5C69.sql View File

@@ -0,0 +1,11 @@
USE ap;

SELECT vendor_name, invoice_number, invoice_date,
line_item_amount, account_description
FROM vendors v, invoices i, invoice_line_items li,
general_ledger_accounts gl
WHERE v.vendor_id = i.vendor_id
AND i.invoice_id = li.invoice_id
AND li.account_number = gl.account_number
AND invoice_total - payment_total - credit_total > 0
ORDER BY vendor_name, line_item_amount DESC

+ 6
- 0
exec_5C70.sql View File

@@ -0,0 +1,6 @@
use ap;

SELECT vendor_name, invoice_number, invoice_total
FROM vendors LEFT JOIN invoices
ON vendors.vendor_id = invoices.vendor_id
ORDER BY vendor_name

+ 8
- 0
exec_5C71.sql View File

@@ -0,0 +1,8 @@


use ex;

SELECT department_name, d.department_number, last_name
FROM departments d LEFT JOIN employees e
ON d.department_number = e.department_number
ORDER BY department_name;

+ 6
- 0
exec_5C72.sql View File

@@ -0,0 +1,6 @@
use ex;

SELECT department_name, e.department_number, last_name
FROM departments d RIGHT JOIN employees e
ON d.department_number = e.department_number
ORDER BY department_name

+ 9
- 0
exec_5C73.sql View File

@@ -0,0 +1,9 @@

use ex;


SELECT department_name, last_name, project_number
FROM departments d LEFT JOIN employees e
ON d.department_number = e.department_number LEFT JOIN projects p
ON e.employee_id = p.employee_id
ORDER BY department_name, last_name

+ 8
- 0
exec_5C74.sql View File

@@ -0,0 +1,8 @@
use ex;

SELECT department_name, last_name, project_number
FROM departments d JOIN employees e
ON d.department_number = e.department_number
LEFT JOIN projects p
ON e.employee_id = p.employee_id
ORDER BY department_name, last_name

+ 8
- 0
exec_5C75.sql View File

@@ -0,0 +1,8 @@
USE EX;

SELECT department_name, last_name, project_number
FROM departments
JOIN employees USING (department_number)
LEFT JOIN projects USING (employee_id)
ORDER BY department_nameuse ex;


+ 8
- 0
exec_5C76.sql View File

@@ -0,0 +1,8 @@
USE EX;

SELECT department_name, last_name, project_number
FROM departments
JOIN employees USING (department_number)
LEFT JOIN projects USING (employee_id)
ORDER BY department_nameuse ex;


+ 4
- 0
exer_5C1.sql View File

@@ -0,0 +1,4 @@
SELECT concat(vendor_contact_last_name, ' ',vendor_contact_first_name) AS full_name
FROM vendors
WHERE vendor_contact_last_name REGEXP 'A' OR 'B' OR 'C' OR 'E'
ORDER BY vendor_contact_last_name ASC;

+ 7
- 0
exer_5C10.sql View File

@@ -0,0 +1,7 @@
USE AP;
SELECT invoice_number, vendor_name, invoice_due_date,
invoice_total - payment_total - credit_total AS balance_due
FROM vendors v JOIN invoices i
ON v.vendor_id = i.vendor_id
WHERE invoice_total - payment_total - credit_total > 0
ORDER BY invoice_due_date DESC;

+ 6
- 0
exer_5C11.sql View File

@@ -0,0 +1,6 @@
USE AP;
SELECT vendor_name, customer_last_name, customer_first_name,
vendor_state AS state, vendor_city AS city
FROM vendors v JOIN om.customers c
ON v.vendor_zip_code = c.customer_zip
ORDER BY state, city ASC;

+ 10
- 0
exer_5C12.sql View File

@@ -0,0 +1,10 @@
USE AP;
SELECT vendor_name, invoice_number, invoice_date, account_description, line_item_amount
FROM vendors v JOIN invoices i
ON v.vendor_id = i.vendor_id
JOIN invoice_line_items li
ON i.invoice_id = li.invoice_id
JOIN general_ledger_accounts ge
on li.account_number = ge.account_number
WHERE invoice_total - payment_total - credit_total > 0
ORDER BY vendor_name, line_item_amount DESC;

+ 5
- 0
exer_5C13.sql View File

@@ -0,0 +1,5 @@
USE AP;
SELECT vendor_name, invoice_number, invoice_total
FROM vendors v LEFT JOIN invoices i
ON v.vendor_id = i.vendor_id
ORDER BY vendor_name ASC

+ 6
- 0
exer_5C14.sql View File

@@ -0,0 +1,6 @@
USE ex;
SELECT department_name, d.department_name, last_name
FROM departments d LEFT JOIN employees e
ON d.department_number = e.department_number
ORDER BY department_name ASC;

+ 7
- 0
exer_5C15.sql View File

@@ -0,0 +1,7 @@
USE ex;
SELECT department_name, last_name, project_number
FROM departments d JOIN employees e
ON d.department_number = e.department_number
LEFT JOIN projects p
ON e.employee_id = p.employee_id
ORDER BY department_name, last_name ASC;

+ 6
- 0
exer_5C16.sql View File

@@ -0,0 +1,6 @@
USE ex;
SELECT department_name, last_name, project_number
FROM departments
NATURAL JOIN employees
LEFT JOIN projects USING (employee_id)
ORDER BY department_name ASC;

+ 6
- 0
exer_5C17.sql View File

@@ -0,0 +1,6 @@
USE ex;
SELECT departments.department_number, department_name, employee_id, last_name
FROM departments,employees
ORDER BY departments.department_number ASC;

/* a cross join */

+ 11
- 0
exer_5C18.sql View File

@@ -0,0 +1,11 @@
USE ex;
SELECT 'Active' AS source, invoice_number, invoice_date, invoice_total
FROM active_invoices
WHERE invoice_date >= '2018_06_01'
UNION
SELECT 'Paid' AS source, invoice_number, invoice_date, invoice_total
FROM paid_invoices
WHERE invoice_date >= '2018_06_01'
ORDER BY invoice_total DESC;

/* union

+ 10
- 0
exer_5C19.sql View File

@@ -0,0 +1,10 @@

USE AP;
SELECT 'Active' AS source, invoice_number, invoice_date, invoice_total
FROM invoices
WHERE invoice_date >= '2018_06_01'
UNION
SELECT 'Paid' AS source, invoice_number, invoice_date, invoice_total
FROM invoices
WHERE invoice_date >= '2018_06_01'
ORDER BY invoice_total DESC;

+ 3
- 0
exer_5C2.sql View File

@@ -0,0 +1,3 @@
SELECT vendor_name, vendor_contact_last_name, vendor_contact_first_name
FROM vendors
ORDER BY vendor_contact_last_name, vendor_contact_first_name ASC;

+ 21
- 0
exer_5C20.sql View File

@@ -0,0 +1,21 @@
USE AP;
SELECT invoice_number, vendor_name, '33% PAYMENT' AS payment_type,
invoice_total AS total, invoice_total * 0.333 AS payment
FROM invoices JOIN vendors
ON invoices.vendor_id = vendors.vendor_id
WHERE invoice_total > 10000
UNION
SELECT invoice_number, vendor_name, '50% PAYMENT' AS payment_type,
invoice_total AS total, invoice_total * 0.500 AS payment
FROM invoices JOIN vendors
ON invoices.vendor_id = vendors.vendor_id
WHERE invoice_total BETWEEN 500 AND 10000
UNION
SELECT invoice_number, vendor_name, 'FULL PAYMENT' AS payment_type,
invoice_total AS total, invoice_total AS payment
FROM invoices JOIN vendors
ON invoices.vendor_id = vendors.vendor_id
WHERE invoice_total < 500
ORDER BY payment_type, vendor_name, invoice_number;

/* 3 unions van 4 tabellen

+ 9
- 0
exer_5C21.sql View File

@@ -0,0 +1,9 @@
11
32020.42


USE AP;
SELECT COUNT(*) AS number_of_invoices,
SUM(invoice_total - payment_total - credit_total) AS total_due
FROM invoices
WHERE invoice_total - payment_total - credit_total > 0;

+ 12
- 0
exer_5C22.sql View File

@@ -0,0 +1,12 @@
After 1/1/2018
114
37966.19
6.00

use ap;
SELECT 'After 1/1/2018' AS selection_date,
COUNT(*) AS number_of_invoices,
MAX(invoice_total) AS highest_invoice_total,
MIN(invoice_total) AS lowest_invoice_total
FROM invoices
WHERE invoice_date > '01/01/2018';

+ 7
- 0
exer_5C23.sql View File

@@ -0,0 +1,7 @@
USE ap;
SELECT COUNT(*) AS number_of_invoices,
sum(invoice_total - payment_total - credit_total)AS total_due
FROM invoices
WHERE invoice_total - payment_total - credit_total > 0;


+ 5
- 0
exer_5C24.sql View File

@@ -0,0 +1,5 @@
USE ap;
SELECT COUNT(*) AS number_of_invoices,
sum(invoice_total - payment_total - credit_total)AS total_due
FROM invoices
WHERE invoice_total - payment_total - credit_total > 0;

+ 5
- 0
exer_5C25.sql View File

@@ -0,0 +1,5 @@
USE ap;
SELECT COUNT(*) AS number_of_invoices,
sum(invoice_total - payment_total - credit_total)AS total_due
FROM invoices
WHERE invoice_total - payment_total - credit_total > 0;

+ 6
- 0
exer_5C26.sql View File

@@ -0,0 +1,6 @@
USE ap;
SELECT vendor_id,ROUND(AVG(invoice_total), 2) AS average_invoice_amount
FROM invoices
GROUP BY vendor_id
HAVING AVG(invoice_total) > 2000
ORDER BY average_invoice_amount DESC;

+ 30
- 0
exer_5C27.sql View File

@@ -0,0 +1,30 @@
use ap;
SELECT t1.vendor_state, vendor_name, t1.sum_of_invoices
FROM
(---- invoice totals by vendor ----
SELECT vendor_state, vendor_name,
SUM(invoice_total) AS sum_of_invoices
FROM vendors v JOIN invoices i
ON v.vendor_id = i.vendor_id
GROUP BY vendor_state, vendor_name
) t1
JOIN
( ---- top invoice totals by state -----
SELECT vendor_state, MAX(sum_of_invoices)
AS sum_of_invoices
FROM
( ---- invoice totals by vendor ----
SELECT vendor_state,
vendor_name,
SUM(invoice_total)
AS sum_of_invoices FROM vendors v JOIN invoices i
ON v.vendor_id =
i.vendor_id
GROUP BY vendor_state,
vendor_name ) t2

GROUP BY vendor_state ) t3
ON t1.vendor_state = t3.vendor_state AND t1.sum_of_invoices =
t3.sum_of_invoices

ORDER BY vendor_state

+ 14
- 0
exer_5C28.sql View File

@@ -0,0 +1,14 @@
use ap;

SELECT vendor_state,
MAX(sum_of_invoices) AS max_sum_of_invoices
FROM
(
SELECT vendor_state, vendor_name,
SUM(invoice_total) AS sum_of_invoices
FROM vendors v JOIN invoices i
ON v.vendor_id = i.vendor_id
GROUP BY vendor_state, vendor_name
) t
GROUP BY vendor_state
ORDER BY vendor_state

+ 7
- 0
exer_5C29.sql View File

@@ -0,0 +1,7 @@
use ap;

SELECT vendor_name, MAX(invoice_date) AS latest_inv
FROM vendors v
LEFT JOIN invoices i ON v.vendor_id = i.vendor_id
GROUP BY vendor_name
ORDER BY latest_inv DESC;

+ 4
- 0
exer_5C3.sql View File

@@ -0,0 +1,4 @@
SELECT invoice_number, invoice_date, invoice_total
FROM invoices
WHERE invoice_date BETWEEN '2018-06-01' AND '2018-06-30'
ORDER BY invoice_date DESC;

+ 7
- 0
exer_5C30.sql View File

@@ -0,0 +1,7 @@
use ap;

SELECT vendor_name,
(SELECT MAX(invoice_date) FROM invoices
WHERE vendor_id = vendors.vendor_id) AS latest_inv
FROM vendors
ORDER BY latest_inv DESC;

+ 3
- 0
exer_5C4.sql View File

@@ -0,0 +1,3 @@
SELECT invoice_number,invoice_date,invoice_total
FROM invoices
ORDER BY invoice_total desc;

+ 8
- 0
exer_5C40.sql View File

@@ -0,0 +1,8 @@
USE AP;

SELECT 'After 1/1/2018' AS selection_date,
COUNT(*) AS number_of_invoices,
ROUND(AVG(invoice_total), 2) AS avg_invoice_amt,
SUM(invoice_total) AS total_invoice_amt
FROM invoices
WHERE invoice_date > '2018-01-01';

+ 8
- 0
exer_5C41.sql View File

@@ -0,0 +1,8 @@
USE AP;

SELECT 'After 1/1/2018' AS selection_date,
COUNT(*) AS number_of_invoices,
MAX(invoice_total) AS highest_invoice_total,
MIN(invoice_total) AS lowest_invoice_total
FROM invoices
WHERE invoice_date > '2018-01-01';

+ 6
- 0
exer_5C42.sql View File

@@ -0,0 +1,6 @@
USE AP;

SELECT MIN(vendor_name) AS first_vendor,
MAX(vendor_name) AS last_vendor,
COUNT(vendor_name) AS number_of_vendors
FROM vendors;

+ 8
- 0
exer_5C43.sql View File

@@ -0,0 +1,8 @@
USE AP;

SELECT COUNT(DISTINCT vendor_id) AS number_of_vendors,
COUNT(vendor_id) AS number_of_invoices,
ROUND(AVG(invoice_total), 2) AS avg_invoice_amt,
SUM(invoice_total) AS total_invoice_amt
FROM invoices
WHERE invoice_date > '2018-01-01';

+ 8
- 0
exer_5C44.sql View File

@@ -0,0 +1,8 @@
USE AP;

SELECT COUNT(DISTINCT vendor_id) AS number_of_vendors,
COUNT(vendor_id) AS number_of_invoices,
ROUND(AVG(invoice_total), 2) AS avg_invoice_amt,
SUM(invoice_total) AS total_invoice_amt
FROM invoices
WHERE invoice_date > '2018-01-01';

+ 8
- 0
exer_5C45.sql View File

@@ -0,0 +1,8 @@
USE AP;

SELECT vendor_id, ROUND(AVG(invoice_total), 2)
AS average_invoice_amount
FROM invoices
GROUP BY vendor_id
HAVING AVG(invoice_total) > 2000
ORDER BY average_invoice_amount DESC;

+ 9
- 0
exer_5C46.sql View File

@@ -0,0 +1,9 @@
USE AP;

SELECT vendor_name, vendor_state,
ROUND(AVG(invoice_total), 2) AS average_invoice_amount
FROM vendors JOIN invoices
ON vendors.vendor_id = invoices.vendor_id
GROUP BY vendor_name
HAVING AVG(invoice_total) > 2000
ORDER BY average_invoice_amount DESC;

+ 5
- 0
exer_5C47.sql View File

@@ -0,0 +1,5 @@
USE AP;

SELECT vendor_id, COUNT(*) AS invoice_qty
FROM invoices
GROUP BY vendor_id;

+ 8
- 0
exer_5C48.sql View File

@@ -0,0 +1,8 @@
use ap;

SELECT vendor_id, invoice_number, invoice_total
FROM invoices i
WHERE invoice_total > (SELECT AVG(invoice_total)
FROM invoices
WHERE vendor_id = i.vendor_id)
ORDER BY vendor_id, invoice_total;

+ 0
- 0
exer_5C49.sql View File


+ 3
- 0
exer_5C5.sql View File

@@ -0,0 +1,3 @@
SELECT invoice_id,invoice_total, credit_total + payment_total AS total
FROM invoices
WHERE invoice_id = 18;

+ 7
- 0
exer_5C50.sql View File

@@ -0,0 +1,7 @@
SELECT vendor_state, vendor_city, COUNT(*) AS invoice_qty,
ROUND(AVG(invoice_total), 2) AS invoice_avg
FROM invoices JOIN vendors
ON invoices.vendor_id = vendors.vendor_id
GROUP BY vendor_state, vendor_city
HAVING COUNT(*) >= 2
ORDER BY vendor_state, vendor_city

+ 9
- 0
exer_5C51.sql View File

@@ -0,0 +1,9 @@
USE AP;

SELECT vendor_name,
COUNT(*) AS invoice_qty,
ROUND(AVG(invoice_total), 2) AS invoice_avg
FROM vendors JOIN invoices
ON vendors.vendor_id = invoices.vendor_id
GROUP BY vendor_name HAVING AVG(invoice_total) > 500
ORDER BY invoice_qty DESC;

+ 8
- 0
exer_5C52.sql View File

@@ -0,0 +1,8 @@
USE AP;

SELECT vendor_name, COUNT(*) AS invoice_qty,
ROUND(AVG(invoice_total), 2) AS invoice_avg
FROM vendors JOIN invoices
ON vendors.vendor_id = invoices.vendor_id WHERE invoice_total > 500
GROUP BY vendor_name
ORDER BY invoice_qty DESC;

+ 12
- 0
exer_5C53.sql View File

@@ -0,0 +1,12 @@
USE AP;

SELECT
invoice_date,
COUNT(*) AS invoice_qty,
SUM(invoice_total) AS invoice_sum
FROM invoices
GROUP BY invoice_date
HAVING invoice_date BETWEEN '2018-05-01' AND '2018-05-31'
AND COUNT(*) > 1
AND SUM(invoice_total) > 100
ORDER BY invoice_date DESC;

+ 10
- 0
exer_5C54.sql View File

@@ -0,0 +1,10 @@
SELECT
invoice_date,
COUNT(*) AS invoice_qty,
SUM(invoice_total) AS invoice_sum
FROM invoices
WHERE invoice_date BETWEEN '2018-05-01' AND '2018-05-31'
GROUP BY invoice_date
HAVING COUNT(*) > 1
AND SUM(invoice_total) > 100
ORDER BY invoice_date DESC

+ 5
- 0
exer_5C55.sql View File

@@ -0,0 +1,5 @@
USE AP;

INSERT INTO invoices VALUES
(115, 97, '456789', '2018-08-01',
8344.50, 0, 0, 1, '2018-08-31', NULL)

+ 6
- 0
exer_5C56.sql View File

@@ -0,0 +1,6 @@
USE AP;

UPDATE invoices
SET payment_date = '2018-09-21',
payment_total = 19351.18
WHERE invoice_number = '97/522'

+ 5
- 0
exer_5C57.sql View File

@@ -0,0 +1,5 @@
USE AP;

UPDATE invoices
SET terms_id = 1
WHERE vendor_id = 95

+ 5
- 0
exer_5C58.sql View File

@@ -0,0 +1,5 @@
USE AP;

UPDATE invoices
SET credit_total = credit_total + 100
WHERE invoice_number = '97/522'

+ 7
- 0
exer_5C59.sql View File

@@ -0,0 +1,7 @@
USE AP;

DELETE FROM invoice_line_items
WHERE invoice_id IN
(SELECT invoice_id
FROM invoices
WHERE vendor_id = 115)

+ 3
- 0
exer_5C6.sql View File

@@ -0,0 +1,3 @@
SELECT invoice_number, invoice_date, invoice_total
FROM invoices
WHERE invoice_total > 50000;

+ 2
- 0
exer_5C60.sql View File

@@ -0,0 +1,2 @@
DELETE FROM invoice_line_items
WHERE invoice_id = 12

+ 3
- 0
exer_5C7.sql View File

@@ -0,0 +1,3 @@
SELECT DISTINCT vendor_state, vendor_city
FROM vendors
ORDER BY vendor_state DESC;

+ 5
- 0
exer_5C8.sql View File

@@ -0,0 +1,5 @@
use ap;
SELECT invoice_number, vendor_name
FROM vendors INNER JOIN invoices
ON vendors.vendor_id = invoices.vendor_id
ORDER BY invoice_number;

+ 7
- 0
exer_5C9.sql View File

@@ -0,0 +1,7 @@
USE AP;
SELECT invoice_number, vendor_name, invoice_due_date,
invoice_total - payment_total - credit_total AS balance_due
FROM vendors v JOIN invoices i
ON v.vendor_id = i.vendor_id
WHERE invoice_total - payment_total - credit_total > 0
ORDER BY invoice_due_date DESC;

Loading…
Cancel
Save