Browse Source

oef

master
Willy Vervest 2 years ago
parent
commit
3d1710176c
54 changed files with 403 additions and 0 deletions
  1. 8
    0
      exec_5C31.sql
  2. 0
    0
      exec_5C32.sql
  3. 7
    0
      exec_5C33.sql
  4. 8
    0
      exec_5C34.sql
  5. 6
    0
      exec_5C35.sql
  6. 8
    0
      exec_5C36.sql
  7. 9
    0
      exec_5C37.sql
  8. 7
    0
      exec_5C38.sql
  9. 8
    0
      exec_5C39.sql
  10. 5
    0
      exec_5C61.sql
  11. 9
    0
      exec_5C62.sql
  12. 8
    0
      exec_5C63.sql
  13. 3
    0
      exec_5C64.sql
  14. 4
    0
      exec_5C65.sql
  15. 9
    0
      exec_5C66.sql
  16. 9
    0
      exec_5C67.sql
  17. 6
    0
      exec_5C68.sql
  18. 11
    0
      exec_5C69.sql
  19. 6
    0
      exec_5C70.sql
  20. 8
    0
      exec_5C71.sql
  21. 6
    0
      exec_5C72.sql
  22. 9
    0
      exec_5C73.sql
  23. 8
    0
      exec_5C74.sql
  24. 8
    0
      exec_5C75.sql
  25. 8
    0
      exec_5C76.sql
  26. 7
    0
      exer_5C23.sql
  27. 5
    0
      exer_5C24.sql
  28. 5
    0
      exer_5C25.sql
  29. 6
    0
      exer_5C26.sql
  30. 30
    0
      exer_5C27.sql
  31. 14
    0
      exer_5C28.sql
  32. 7
    0
      exer_5C29.sql
  33. 7
    0
      exer_5C30.sql
  34. 8
    0
      exer_5C40.sql
  35. 8
    0
      exer_5C41.sql
  36. 6
    0
      exer_5C42.sql
  37. 8
    0
      exer_5C43.sql
  38. 8
    0
      exer_5C44.sql
  39. 8
    0
      exer_5C45.sql
  40. 9
    0
      exer_5C46.sql
  41. 5
    0
      exer_5C47.sql
  42. 8
    0
      exer_5C48.sql
  43. 0
    0
      exer_5C49.sql
  44. 7
    0
      exer_5C50.sql
  45. 9
    0
      exer_5C51.sql
  46. 8
    0
      exer_5C52.sql
  47. 12
    0
      exer_5C53.sql
  48. 10
    0
      exer_5C54.sql
  49. 5
    0
      exer_5C55.sql
  50. 6
    0
      exer_5C56.sql
  51. 5
    0
      exer_5C57.sql
  52. 5
    0
      exer_5C58.sql
  53. 7
    0
      exer_5C59.sql
  54. 2
    0
      exer_5C60.sql

+ 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;


+ 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;

+ 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;

+ 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


+ 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)

+ 2
- 0
exer_5C60.sql View File

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

Loading…
Cancel
Save