Chapter 8 Advanced SQL
Chapter 8
Advanced SQL
Use the Ch08_LargeCo database shown in Figure P8.19 to work Problems 19-27. For problems
with very large result sets, only the first several rows of output are shown in the following figures.
Figure P8.19 Ch08_SaleCo2 Database Tables
35
Chapter 8 Advanced SQL
1. Write a query to display the products that have a price greater than $50.
SELECT * FROM LGPRODUCT WHERE PROD_PRICE > 50;
2. Write a query to display the current salary for each employee in department 300. Assume that
only current employees are kept in the system, and therefore the most current salary for each
employee is the entry in the salary history with a NULL end date. Sort the output in
descending order by salary amount.
Figure P8.20 Current salary for employees in department 300
SELECT Emp_Num, Emp_LName, Emp_FName, Sal_Amount
FROM LGEMPLOYEE JOIN LGSALARY_HISTORY USING (Emp_Num)
WHERE Sal_End IS NULL
AND Dept_Num = 300
ORDER BY Sal_Amount DESC;
3. Write a query to display the starting salary for each employee. The starting salary would be
the entry in the salary history with the oldest salary start date for each employee. Sort the
output by employee number.
Figure P8.21 Starting salary for each employee
36
Chapter 8 Advanced SQL
SELECT e.Emp_Num, Emp_LName, Emp_FName, Sal_Amount
FROM lgemployee e JOIN lgsalary_history s ON e.Emp_Num = s.Emp_Num
WHERE sal_from = (SELECT Min(sal_from)
FROM lgsalary_history s2 WHERE e.Emp_Num = s2.Emp_Num)
ORDER BY e.Emp_Num;
4. Write a query to display the invoice number, line numbers, product SKUs, product
descriptions, and brand ID for sales of sealer and top coat products of the same brand on the
same invoice.
Figure P8.22 Invoices for sealer and top coat of the same brand
SELECT l.Inv_Num, l.Line_Num, p.Prod_Sku, p.Prod_Descript, l2.Line_Num, p2.Prod_Sku,
p2.Prod_Descript, p.Brand_ID
FROM (lgline l JOIN lgproduct p ON l.prod_sku = p.prod_sku) JOIN
(lgline l2 JOIN lgproduct p2 ON l2.prod_sku = p2.prod_sku)
ON l.inv_num = l2.inv_num
WHERE p.brand_id = p2.brand_id AND p.prod_category = ‘Sealer’ AND p2.prod_category = ‘Top
Coat’
ORDER BY l.inv_num, l.line_num;
5. Write a query to display the customer code, first name, and last name of all customers who
have had at least one invoice completed by employee 83649 and at least one invoice completed
by employee 83677. Sort the output by customer last name and then first name.
Figure P8.24 Customers with invoices filled by employees 83649 and 83677
37
Chapter 8 Advanced SQL
SELECT Cust_Code, Cust_FName, Cust_LName
FROM lgcustomer JOIN lginvoice USING (cust_code)
WHERE employee_id = 83649
INTERSECT
SELECT Cust_Code, Cust_FName, Cust_LName
FROM lgcustomer JOIN lginvoice USING (cust_code)
WHERE employee_id = 83677
ORDER BY cust_name, cust_fname;
6. LargeCo is planning a new promotion in Alabama (AL) and wants to know about the largest
purchases made by customers in that state. Write a query to display the customer code,
customer first name, last name, full address, invoice date, and invoice total of the largest
purchase made by each customer in Alabama. Be certain to include any customers in Alabama
who have never made a purchase (their invoice dates should be NULL and the invoice totals
should display as 0).
Figure P8.25 Largest purchases of customers in Alabama
SELECT c.Cust_Code, Cust_FName, Cust_LName, Cust_Street, Cust_City, Cust_State, Cust_Zip,
Inv_Date, Inv_Total AS “Largest Invoice”
FROM lgcustomer c JOIN lginvoice I ON c.cust_code = i.cust_code
WHERE cust_state = ‘AL’ AND inv_total = (SELECT Max(inv_total) FROM lginvoice i2
WHERE i2.cust_code = c.cust_code)
UNION
SELECT Cust_Code, Cust_FName, Cust_LName, Cust_Street, Cust_City, Cust_State, Cust_Zip,
NULL, 0
FROM lgcustomer
WHERE cust_state = ‘AL’ AND cust_code NOT IN (SELECT cust_code FROM lginvoice)
ORDER BY cust_lname, cust_fname;
7. One of the purchasing managers is interested in the impact of product prices on the sale of
products of each brand. Write a query to display the brand name, brand type, average price of
products of each brand, and total units sold of products of each brand. Even if a product has
been sold more than once, its price should only be included once in the calculation of the
average price. However, you must be careful because multiple products of the same brand can
38
Chapter 8 Advanced SQL
have the same price, and each of those products must be included in the calculation of the
brand’s average price.
Figure P8.26 Average price and total units sold of products by brand
SELECT Brand_Name, Brand_Type, Round(avgprice,2) AS “Average Price”, “Units Sold”
FROM lgbrand b JOIN (SELECT brand_id, Avg(prod_price) AS avgprice
FROM lgproduct
GROUP BY brand_id) s1
ON b.brand_id = s1.brand_id
JOIN (SELECT brand_id, Sum(line_qty) AS “Units Sold”
FROM lgproduct p JOIN lgline l ON p.prod_sku = l.prod_sku
GROUP BY brand_id) s2
ON b.brand_id – s2.brand_id
ORDER BY brand_name;
8. The purchasing manager is still concerned about the impact of price on sales. Write a query to
display the brand name, brand type, product SKU, product description, and price of any
products that are not a premium brand, but that cost more than the most expensive premium
brand products.
Figure P8.27 Nonpremium products that are more expensive than premium products
SELECT Brand_Name, Brand_Type, Prod_Sku, Prod_Descript, Prod_Price
FROM lgproduct p JOIN lgbrand b ON p.brand_id = b.brand_id
WHERE brand_type <> ‘PREMIUM’
AND prod_price > (SELECT Max(prod_price)
FROM lgproduct p JOIN lgbrand b ON p.brand_id = b.brand_id
WHERE brand_type = ‘PREMIUM’);
39