@@ -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 +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); |
@@ -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; |
@@ -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; |
@@ -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; |
@@ -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; |
@@ -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; |
@@ -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; |
@@ -0,0 +1,5 @@ | |||
USE AP; | |||
DELETE FROM general_ledger_accounts | |||
WHERE account_number = 306 | |||
@@ -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 |
@@ -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 |
@@ -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 |
@@ -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 |
@@ -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; |
@@ -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 |
@@ -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 |
@@ -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 |
@@ -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 |
@@ -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; |
@@ -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 |
@@ -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 |
@@ -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 |
@@ -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; | |||
@@ -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; | |||
@@ -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; | |||
@@ -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; |
@@ -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; |
@@ -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; |
@@ -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 |
@@ -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 |
@@ -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; |
@@ -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; |
@@ -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'; |
@@ -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'; |
@@ -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; |
@@ -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'; |
@@ -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'; |
@@ -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; |
@@ -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; |
@@ -0,0 +1,5 @@ | |||
USE AP; | |||
SELECT vendor_id, COUNT(*) AS invoice_qty | |||
FROM invoices | |||
GROUP BY vendor_id; |
@@ -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 +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 |
@@ -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; |
@@ -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; |
@@ -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; |
@@ -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 |
@@ -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) |
@@ -0,0 +1,6 @@ | |||
USE AP; | |||
UPDATE invoices | |||
SET payment_date = '2018-09-21', | |||
payment_total = 19351.18 | |||
WHERE invoice_number = '97/522' |
@@ -0,0 +1,5 @@ | |||
USE AP; | |||
UPDATE invoices | |||
SET terms_id = 1 | |||
WHERE vendor_id = 95 |
@@ -0,0 +1,5 @@ | |||
USE AP; | |||
UPDATE invoices | |||
SET credit_total = credit_total + 100 | |||
WHERE invoice_number = '97/522' |
@@ -0,0 +1,7 @@ | |||
USE AP; | |||
DELETE FROM invoice_line_items | |||
WHERE invoice_id IN | |||
(SELECT invoice_id | |||
FROM invoices | |||
WHERE vendor_id = 115) |
@@ -0,0 +1,2 @@ | |||
DELETE FROM invoice_line_items | |||
WHERE invoice_id = 12 |