Browse Source

added solutions & demo queries

master
Bart De Lepeleer 2 years ago
parent
commit
5ced702347

+ 6
- 0
demo-queries-from slides-01.sql View File

@@ -0,0 +1,6 @@
SELECT invoice_number, invoice_date, invoice_total
FROM invoices
WHERE invoice_total >
(SELECT AVG(invoice_total)
FROM invoices)
ORDER BY invoice_total;

+ 13
- 0
demo-queries-from slides-02.sql View File

@@ -0,0 +1,13 @@
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;

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;

+ 12
- 0
demo-queries-from slides-03.sql View File

@@ -0,0 +1,12 @@
SELECT vendor_id, vendor_name, vendor_state
FROM vendors
WHERE vendor_id NOT IN
(SELECT DISTINCT vendor_id
FROM invoices)
ORDER BY vendor_id;

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;

+ 11
- 0
demo-queries-from slides-04.sql View File

@@ -0,0 +1,11 @@
SELECT invoice_number, invoice_date,
invoice_total - payment_total - credit_total AS balance_due
FROM invoices
WHERE invoice_total - payment_total - credit_total > 0
AND invoice_total - payment_total - credit_total <
(
SELECT AVG(invoice_total - payment_total - credit_total)
FROM invoices
WHERE invoice_total - payment_total - credit_total > 0
)
ORDER BY invoice_total DESC;

+ 7
- 0
demo-queries-from slides-05.sql View File

@@ -0,0 +1,7 @@
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
demo-queries-from slides-06.sql View File

@@ -0,0 +1,6 @@
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);

+ 7
- 0
demo-queries-from slides-07.sql View File

@@ -0,0 +1,7 @@
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;

+ 6
- 0
demo-queries-from slides-08.sql View File

@@ -0,0 +1,6 @@
SELECT vendor_id, vendor_name, vendor_state
FROM vendors
WHERE NOT EXISTS
(SELECT *
FROM invoices
WHERE vendor_id = vendors.vendor_id);

+ 11
- 0
demo-queries-from slides-09.sql View File

@@ -0,0 +1,11 @@
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;

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;

+ 11
- 0
demo-queries-from slides-10.sql View File

@@ -0,0 +1,11 @@
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;

+ 29
- 0
demo-queries-from slides-11.sql View File

@@ -0,0 +1,29 @@
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;

+ 15
- 0
demo-queries-from slides-12.sql View File

@@ -0,0 +1,15 @@
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;

SELECT vendor_state, MAX(sum_of_invoices) AS 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;

+ 5
- 0
exercise-01.sql View File

@@ -0,0 +1,5 @@
SELECT vendor_name
FROM vendors
WHERE vendor_id IN
(SELECT DISTINCT vendor_id FROM invoices)
ORDER BY vendor_name

+ 7
- 0
exercise-02.sql View File

@@ -0,0 +1,7 @@
SELECT invoice_number, invoice_total
FROM invoices
WHERE payment_total >
(SELECT AVG(payment_total)
FROM invoices
WHERE payment_total > 0)
ORDER BY invoice_total DESC

+ 7
- 0
exercise-03.sql View File

@@ -0,0 +1,7 @@
SELECT account_number, account_description
FROM general_ledger_accounts gl
WHERE NOT EXISTS
(SELECT *
FROM invoice_line_items
WHERE account_number = gl.account_number)
ORDER BY account_number

+ 10
- 0
exercise-04.sql View File

@@ -0,0 +1,10 @@
SELECT vendor_name, i.invoice_id, invoice_sequence, 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
WHERE i.invoice_id IN
(SELECT DISTINCT invoice_id
FROM invoice_line_items
WHERE invoice_sequence > 1)
ORDER BY vendor_name, i.invoice_id, invoice_sequence

+ 5
- 0
exercise-05.sql View File

@@ -0,0 +1,5 @@
SELECT SUM(invoice_max) AS sum_of_maximums
FROM (SELECT vendor_id, MAX(invoice_total) AS invoice_max
FROM invoices
WHERE invoice_total - credit_total - payment_total > 0
GROUP BY vendor_id) t

+ 8
- 0
exercise-06.sql View File

@@ -0,0 +1,8 @@
SELECT vendor_name, vendor_city, vendor_state
FROM vendors
WHERE CONCAT(vendor_state, vendor_city) NOT IN
(SELECT CONCAT(vendor_state, vendor_city) as vendor_city_state
FROM vendors
GROUP BY vendor_city_state
HAVING COUNT(*) > 1)
ORDER BY vendor_state, vendor_city

+ 9
- 0
exercise-07.sql View File

@@ -0,0 +1,9 @@
SELECT vendor_name, invoice_number,
invoice_date, invoice_total
FROM invoices i JOIN vendors v
ON i.vendor_id = v.vendor_id
WHERE invoice_date =
(SELECT MIN(invoice_date)
FROM invoices
WHERE vendor_id = i.vendor_id)
ORDER BY vendor_name

+ 14
- 0
exercise-08.sql View File

@@ -0,0 +1,14 @@
SELECT vendor_name, invoice_number,
invoice_date, invoice_total
FROM invoices i
JOIN
(
SELECT vendor_id, MIN(invoice_date) AS oldest_invoice_date
FROM invoices
GROUP BY vendor_id
) oi
ON i.vendor_id = oi.vendor_id AND
i.invoice_date = oi.oldest_invoice_date
JOIN vendors v
ON i.vendor_id = v.vendor_id
ORDER BY vendor_name

Loading…
Cancel
Save