You are on page 1of 13

SQL Lab #5

Complete the following SQL commands to create the following queries/views. You will
be using your Saleco tables to group data and join tables. Cut/Paste your SQL statement
and results from SQLDeveloper under each instruction.

Using Aggregate Functions for all rows in table:

1. Create and run the SQL statement that include customer fname, lname and
balance from the customer tables for all records where balance is > 0.
Ans: select cus_fname, cus_lname, cus_balance from customersc where
cus_balance>0;

2. Use the aggregate function Sum to find the total of all customer balances. Use an
alias Total Balance.
Ans: select SUM(cus_balance) as Total_Balance from customersc;
3. Use the aggregate function AVG to find the average of all customer balances. Use
an alias Average Balance.
Ans: select AVG(CUS_BALANCE) as Average_Balance from customersc;

4. Find the first and last names of both the customers with smallest and largest
balance. Use a proper alias for the column heading.
Ans: SELECT CUS_FNAME, CUS_LNAME
FROM CustomerSC

WHERE CUS_BALANCE = (SELECT min(CUS_BALANCE) as

smallest_balance FROM CustomerSC);

SELECT CUS_FNAME, CUS_LNAME

FROM CustomerSC

WHERE CUS_BALANCE = (SELECT max(CUS_BALANCE) as largest_balance

FROM CustomerSC);
5. Use the Count function to find the number of products that have a price greater
than fifty.
Ans: select count (distinct P_code) from product where p_price > 50;

6. NEW - Do #5 again except find the number of products that have a


price less than the average price.
Ans: select COUNT (distinct p_code) from product where p_price <
(select avg (p_price) from product);
Using Aggregate Functions for GROUPS of rows in table:

7. Create the SQL that will query the Product table showing the SUM of all products
for a specific vendor. To do this, use the sum function on the price field and
group by vendor number. Your output should show the vendor number and the
data will be the sum of all prices for each row (vendor).
Ans: select v_code, sum(p_price) from product group by v_code;
8. Do #7 again but instead of getting the sum of the price, calculate the total price of
each product then use the AVG function on this amount. Group by vendor again.

AVG(price * qoh)

Ans: select v_code, avg(p_price*P_qoh) from product group by v_code;


9. CREATE YOUR OWN. Build your own SQL using aggregate functions and
grouping to answer a business question.
Ans: select p_code, avg(p_price) from product where p_indate >15-JAN-

10group by p_code having min(p_qoh)>=11;

How many invoices does each customer have?

JOINING TABLES

Review your SALECO ERD to see all Primary and Foreign KEYs. Tables are joined by
setting these fields equal to each other when they have a relationship

10. Select all the fields from the customer and the invoice table joining these tables
where the customersc.cus_code = invoicesc.cus_code.

Ans: select customersc.cus_code, customersc.cus_lname,

customersc.cus_fname, customersc.cus_initial, customersc.cus_areacode,

customersc.cus_phone,

customersc.cus_balance,invoicesc.inv_number,invoicesc.cus_code,invoicesc.inv_date

from customersc inner join invoicesc on

customersc.cus_code = invoicesc.cus_code order by customersc.cus_code;


11. Select the vendor name and contact from the vendor table and the p_descript and
price from the product table and join the tables where the v_codes equal.
Ans: select vendor.v_code,vendor.v_contact,product.p_descript, product.p_price
from vendor,product where vendor.v_code = product.v_code;
12. Repeat # 11 display only those rows where the price < = 50. Use column alias
names and sort from highest price to lowest.
Ans: select vendor.v_code, vendor.v_contact, product.p_descript,

product.p_price as NEW_PRICE from vendor,product where vendor. V_code =

product.v_code and p_price<=50

order by NEW_PRICE desc;

13. Join the Customer, Invoice, Line and Product table showing the customer fname,
lname, invoice number (from invoice table), product descript, line units and line
price using nice column alias names. Add a derived field called Line Total by
taking line units * line price.
Ans: select customersc.cus_lname as "Last Name",customersc.cus_fname as
"First

Name",

invoicesc.inv_number as "Invoice Number", p_descript as product,

line_price as "Line Price", line_units as "Line Units",

line_units*line_price as "Line Total"

from customersc,invoicesc,product,line
where CustomerSC.cus_code=InvoiceSC.cus_code and
InvoiceSC.inv_number=Line.inv_number and

Line.p_code=Product.p_code;

14. Create #13 as a View called Receipts.


Ans: create view Receipts as select customersc.cus_lname as lname,

customersc.cus_fname as fname, invoicesc.inv_number as numberr,

product.p_descript as descript,

line.line_price as price, line.line_units as units , line.line_price*line.line_units

as Line_Total from customersc,invoicesc,product,line ;


15. Business Question: What is the total amount for each invoice?. Using the
Receipts view, create a query that groups the records by invoice number showing
the invoice number and Sum of all the Line Totals. Call this Receipt Total.
Ans: select numberr, sum(Line_Total) as Receipt_Total from RECEIPTS

group by numberr;
16. Using your own creativity, create your own Grouped Query. It must be derived
from joining at least 2 tables. State in your own words the question your query
answers.
Ans: select line.inv_number, line.line_number, line.p_code, line.line_units,

line.line_price, product.p_code,

product.p_descript,product.p_indate,product.p_qoh,product.p_min,product.p_
price

from line inner join product on

product.p_code = line.p_code order by product.p_code;

Provide the following information regarding the line items on invoice number
1006.

Invoice number
Vendor
Line number
Product
Quantity
Price
Discount
Discounted Price

Ans: select invoicesc.inv_number,vendor.v_name, line.line_number,

product.p_code,product.p_qoh,product.p_price,product.p_discount,

product.p_price*(1-product.p_discount) as "discounted price"

from invoicesc,line,product,vendor

where invoicesc.inv_number=line.inv_number and


line.p_code=product.p_code and

product.v_code=vendor.v_code and invoicesc.inv_number=1006;

You might also like