IS 4420 SQL lab 3 - Demo Objective: A. Query multiple tables B. Use subqueris C. Understand PL/SQL In order to see the difference between a natural join and an outer join, insert a record first: INSERT INTO CUSTOMER_T (CUSTOMER_ID, CUSTOMER_NAME) VALUES (8, 'Leon Chen'); 1. Natural Join: For each customer who placed an order, what is the customer’s name and order number? SELECT CUSTOMER_T.CUSTOMER_ID, CUSTOMER_NAME, ORDER_ID FROM CUSTOMER_T, ORDER_T WHERE CUSTOMER_T.CUSTOMER_ID = ORDER_T.CUSTOMER_ID; 2. Outer Join: List the customer name, ID number, and order number for all customers. Include customer information even for customers that do NOT have an order SELECT CUSTOMER_T.CUSTOMER_ID, CUSTOMER_NAME, ORDER_ID FROM CUSTOMER_T, ORDER_T WHERE CUSTOMER_T.CUSTOMER_ID = ORDER_T.CUSTOMER_ID(+); 3. Assemble all information necessary to create an invoice for order number 106 SELECT CUSTOMER_T.CUSTOMER_ID, CUSTOMER_NAME, CUSTOMER_ADDRESS, CUSTOMER_CITY, CUSTOMER_STATE, ZIP, ORDER_T.ORDER_ID, ORDER_DATE, ORDERED_QUANTITY, PRODUCT_DESCRIPTION, STANDARD_PRICE, (ORDERED_QUANTITY * STANDARD_PRICE) FROM CUSTOMER_T, ORDER_T, ORDER_LINE_T, PRODUCT_T WHERE CUSTOMER_T.CUSTOMER_ID = ORDER_T.CUSTOMER_ID AND ORDER_T.ORDER_ID = ORDER_LINE_T.ORDER_ID AND ORDER_LINE_T.PRODUCT_ID = PRODUCT_T.PRODUCT_ID AND ORDER_T.ORDER_ID = 106; 4. Uncorrelated subquery: Show all customers who have placed an order SELECT CUSTOMER_NAME FROM CUSTOMER_T WHERE CUSTOMER_ID IN (SELECT DISTINCT CUSTOMER_ID FROM ORDER_T); 1 5. Show all orders that include furniture finished in natural ash Correlated subquery: SELECT DISTINCT ORDER_ID FROM ORDER_LINE_T WHERE EXISTS (SELECT * FROM PRODUCT_T WHERE PRODUCT_ID = ORDER_LINE_T.PRODUCT_ID AND PRODUCT_FINISH = ‘Natural Ash’); Natural join: SELECT DISTINCT ORDER_ID FROM ORDER_LINE_T, PRODUCT_T WHERE PRODUCT_T. PRODUCT_ID = ORDER_LINE_T.PRODUCT_ID AND PRODUCT_FINISH = ‘Natural Ash’; 6. Show the order ID and product descriptions for each order line SELECT o.Order_ID, p.Product_Description FROM Product_T p, Order_Line_T o WHERE p.Product_ID=o.Product_ID; 7. Show customer name and order date when the customer placed an order, sorted by order date SELECT customer_name, order_date FROM customer_t c, order_t o, order_line_t ol, product_t p WHERE c.customer_id=o.customer_id AND o.order_id=ol.order_id AND ol.product_id=p.product_id GROUP BY customer_name, order_date ORDER BY order_date; 8. Show customer name, order date, and the order total amount in dollars in each order, sorted by descending order date SELECT customer_name, order_date, sum(p.standard_price * ol.ordered_quantity) as Total FROM customer_t c, order_t o, order_line_t ol, product_t p WHERE c.customer_id=o.customer_id AND o.order_id=ol.order_id AND ol.product_id=p.product_id GROUP BY customer_name, order_date ORDER BY order_date DESC; 9. Show names of customer who placed more than one orders, and also show the total number of orders placed by each of the customers. SELECT customer_name, count(customer_name) FROM customer_t c, order_t o WHERE c.customer_id=o.customer_id GROUP BY customer_name HAVING count(customer_name) > 1; 2 10. PL/SQL: Compare the total ordered quantity of office chairs (product ID 2000) and office desks (product ID 1000), and calculate the percentage difference You have two ways to run the program below: 1. Type the program in the SQL Plus command window (or type in other text editor like Notebook and then copy / paste to SQL Plus command window) and run it; 2. Type in other text editor like Notebook, save it as ‘c:\plsqldemo.sql’, and run it in SQL Plus using: @ c:\plsqldemo.sql; PL/SQL program: set serveroutput on; declare chair_qty integer; desk_qty integer; pct integer; begin SELECT SUM(ORDERED_QUANTITY) into chair_qty FROM ORDER_LINE_T WHERE PRODUCT_ID=2000; SELECT SUM(ORDERED_QUANTITY) into desk_qty FROM ORDER_LINE_T WHERE PRODUCT_ID=1000; dbms_output.put_line('The ordered quantity of office chair is '||to_char(chair_qty)); dbms_output.put_line('The ordered quantity of office desk is '||to_char(desk_qty)); if (chair_qty > desk_qty) then pct:= round((chair_qty - desk_qty)/desk_qty*100); dbms_output.put_line('The ordered quantity of office chair is '||to_char(pct)|| '% higher than the ordered quantity of office desk.'); else pct:= round((desk_qty - chair_qty)/chair_qty*100); dbms_output.put_line('The ordered quantity of office desk is '||to_char(pct)|| '% higher than the ordered quantity of office chair.'); end if; end; 3