Hour 15, "Combining Multiple Queries into One"

Quiz Answers

1: Is the syntax correct for the following compound queries? If not, what would correct the syntax? Use the EMPLOYEE_TBL and the EMPLOYEE_PAY_TBL shown as follows:
EMPLOYEE_TBL  
EMP_IDVARCHAR2(9)NOT NULL,
LAST_NAMEVARCHAR2(15)NOT NULL,
FIRST_NAMEVARCHAR2(15)NOT NULL,
MIDDLE_NAMEVARCHAR2(15), 
ADDRESSVARCHAR2(30)NOT NULL,
CITYVARCHAR2(15)NOT NULL,
STATECHAR(2)NOT NULL,
ZIPNUMBER(5)NOT NULL,
PHONECHAR(10), 
PAGERCHAR(10), 

CONSTRAINT EMP_PK PRIMARY KEY (EMP_ID)

EMPLOYEE_PAY_TBL   
EMP_IDVARCHAR2(9)NOT NULL,PRIMARY KEY
POSITIONVARCHAR2(15)NOT NULL, 
DATE_HIREDATE,  
PAY_RATENUMBER(4,2)NOT NULL, 
DATE_LAST_RAISEDATE,  
SALARYNUMBER(8,2),  
BONUSNUMBER(6,2),  

CONSTRAINT EMP_FK FOREIGN KEY (EMP_ID)
REFERENCES EMPLOYEE_TBL (EMP_ID)

  1. SELECT EMP_ID, LAST_NAME, FIRST_NAME
    FROM EMPLOYEE_TBL
    UNION
    SELECT EMP_ID, POSITION, DATE_HIRE
    FROM EMPLOYEE_PAY_TBL;
    										
  2. SELECT EMP_ID FROM EMPLOYEE_TBL
    UNION ALL
    SELECT EMP_ID FROM EMPLOYEE_PAY_TBL
    ORDER BY EMP_ID
    										
  3. SELECT EMP_ID FROM EMPLOYEE_PAY_TBL
    INTERSECT
    SELECT EMP_ID FROM EMPLOYEE_TBL
    ORDER BY 1;
    										
A1:
  1. This compound query does not work because the data types do not match. The EMP_ID columns match, but the LAST_NAME and FIRST_NAME data types do not match the POSITION and DATE_HIRE data types.

  2. Yes, the statement is correct.

  3. Yes, this compound query works.

2: Match the correct operator to the following statements.
 STATEMENTOPERATOR
a.Show duplicatesUNION
b.Return only rows from the first query that match those in the second queryINTERSECT

UNION ALL

EXCEPT
c.Return no duplicates 
d.Return only rows from the first query not returned by the second 

A2:
STATEMENTOPERATOR
a.Show duplicates.UNION ALL
b.Return only rows from the first query that match those in the second query.INTERSECT
c.Return no duplicates.UNION
d.Return only rows from the first query not returned by the second.EXCEPT

Exercise Answers

Using the CUSTOMER_TBL and the ORDERS_TBL as listed:

CUSTOMER_TBL   
CUST_INVARCHAR2(10)NOT NULLPRIMARY KEY
CUST_NAMEVARCHAR2(30)NOT NULL 
CUST_ADDRESSVARCHAR2(20)NOT NULL 
CUST_CITYVARCHAR2(15)NOT NULL 
CUST_STATECHAR(2)NOT NULL 
CUST_ZIPNUMBER(5)NOT NULL 
CUST_PHONENUMBER(10)  
CUST_FAXNUMBER(10)  

ORDERS_TBL   
ORD_NUMVARCHAR2(10)NOT NULLPRIMARY KEY
CUST_IDVARCHAR2(10)NOT NULL 
PROD_IDVARCHAR2(10)NOT NULL 
QTYNUMBER(6)NOT NULL 
ORD_DATEDATE  

1: Write a compound query to find the customers that have placed an order.
A1:
SELECT CUST_ID FROM CUSTOMER_TBL
INTERSECT
SELECT CUST_ID FROM ORDERS_TBL;
									

2: Write a compound query to find the customers that have not placed an order.
A2:
SELECT CUST_ID FROM CUSTOMER_TBL
EXCEPT
SELECT CUST_ID FROM ORDERS_TBL;

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset