0% found this document useful (0 votes)
200 views

Homework 3

This document contains solutions to exercises involving SQL queries on tables containing vendor, invoice, and general ledger data. The exercises demonstrate skills in aggregation, filtering, subqueries, joins, and other SQL concepts. Sample queries and solutions are provided for calculating totals by vendor, filtering for average payments, finding unmatched accounts, and more advanced concepts like correlated subqueries and common table expressions.

Uploaded by

api-559045701
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
200 views

Homework 3

This document contains solutions to exercises involving SQL queries on tables containing vendor, invoice, and general ledger data. The exercises demonstrate skills in aggregation, filtering, subqueries, joins, and other SQL concepts. Sample queries and solutions are provided for calculating totals by vendor, filtering for average payments, finding unmatched accounts, and more advanced concepts like correlated subqueries and common table expressions.

Uploaded by

api-559045701
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Chapter 6

Exercise 1:

Query

SELECT vendor_id, SUM(invoice_total)


AS invoice_total_sum
FROM invoices
GROUP BY vendor_id;

Solution:

Exercise 2:

SELECT vendor_name, SUM(payment_total) AS payment_total_sum


FROM vendors v JOIN invoices i ON v.vendor_id = i.vendor_id
GROUP BY vendor_name ORDER BY payment_total_sum DESC;

Solution:

Exercise 3:

SELECT vendor_name, COUNT(*) AS invoice_count,


SUM(invoice_total) AS invoice_total_sum
FROM vendors v JOIN invoices i ON v.vendor_id = i.vendor_id
GROUP BY vendor_name ORDER BY invoice_count DESC;
Solution:

Exercise 4:

Query

SELECT account_description, COUNT(*) AS line_item_count,


SUM(line_item_amount) AS line_item_amount_sum
FROM general_ledger_accounts gl JOIN invoice_line_items li
ON gl.account_number = li.account_number GROUP BY account_description
HAVING line_item_count > 1 ORDER BY line_item_amount_sum DESC;

Solution:

Exercise 5:

Query

SELECT account_description, COUNT(*) AS line_item_count,


SUM(line_item_amount) AS line_item_amount_sum
FROM general_ledger_accounts gl JOIN invoice_line_items li
ON gl.account_number = li.account_number JOIN invoices i
ON li.invoice_id = i.invoice_id
WHERE invoice_date BETWEEN '2018-04-01' AND '2018-06-30'
GROUP BY account_description HAVING line_item_count > 1
ORDER BY line_item_amount_sum DESC;
Solution:

Exercise 6:
Query

SELECT account_number, SUM(line_item_amount) AS line_item_sum


FROM invoice_line_items
GROUP BY account_number WITH ROLLUP;

Solution:

Exercise 7:
Query
SELECT vendor_name,
COUNT(DISTINCT li.account_number) AS number_of_gl_accounts
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
GROUP BY vendor_name HAVING number_of_gl_accounts > 1
ORDER BY vendor_name;

Solution:
Chapter 7:

Exercise 1:
Query

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

Solution

Exercise 2:
Query
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;

Solution :
Exercise 3:
Query
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;

Solution:

Exercise 4:
Query
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;

Solution:
Exercise 5:
Query
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) md;

Solution

Exercise 6:

Query:

SELECT vendor_name, vendor_city, vendor_state


FROM vendors
WHERE CONCAT(vendor_state, vendor_city) NOT IN (SELECT distinct
CONCAT(vendor_state, vendor_city)
FROM vendors
GROUP BY vendor_city, vendor_state having count(*)>1)
ORDER BY vendor_state , vendor_city;

Solution
Exercise 7:
Query

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;

Solution

Exercise 8:

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;

Solution

You might also like