SQL Lab5
SQL Lab5
SQL Lab5
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.
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
FROM CustomerSC
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;
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)
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.
customersc.cus_phone,
customersc.cus_balance,invoicesc.inv_number,invoicesc.cus_code,invoicesc.inv_date
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",
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;
product.p_descript as descript,
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
Provide the following information regarding the line items on invoice number
1006.
Invoice number
Vendor
Line number
Product
Quantity
Price
Discount
Discounted Price
product.p_code,product.p_qoh,product.p_price,product.p_discount,
from invoicesc,line,product,vendor