1: | What is the function of a subquery when used with a SELECT statement? |
A1: | The main function of a subquery when used with a SELECT statement is to return data that the main query can use to resolve the query. |
2: | Can you update more than one column when using the UPDATE statement in conjunction with a subquery? |
A2: | Yes, you can update more than one column using the same UPDATE and subquery statement. |
3: | Are the following syntaxes correct? If not, what is the correct syntax?
|
A3: |
|
4: | What would happen if the following statement were run?DELETE FROM EMPLOYEE_TBL WHERE EMP_ID IN (SELECT EMP_ID FROM EMPLOYEE_PAY_TBL); |
A4: | All rows that were retrieved from the EMPLOYEE_PAY_TBL would be used by the DELETE to remove them from the EMPLOYEE_TBL. A WHERE clause in the subquery is highly advised. |
Use the following tables:
EMPLOYEE_TBL | |||
EMP_ID | VARCHAR2(9) | NOT NULL | PRIMARY KEY |
LAST_NAME | VARCHAR2(15) | NOT NULL | |
FIRST_NAME | VARCHAR2(15) | NOT NULL | |
MIDDLE_NAME | VARCHAR2(15) | ||
ADDRESS | VARCHAR2(30) | NOT NULL | |
CITY | VARCHAR2(15) | NOT NULL | |
STATE | CHAR(2) | NOT NULL | |
ZIP | NUMBER(5) | NOT NULL | |
PHONE | CHAR(10) | ||
PAGER | CHAR(10) |
EMPLOYEE_PAY_TBL | |||
EMP_ID | VARCHAR2(9) | NOT NULL | PRIMARY KEY |
POSITION | VARCHAR2(15) | NOT NULL | |
DATE_HIRE | DATE | ||
PAY_RATE | NYMBER(4,2) | NOT NULL | |
DATE_LAST_RAISE | DATE | ||
SALARY | NUMBER (6,2) | ||
BONUS | NUMBER (4,2) |
CONSTRAINT EMP_FK FOREIGN KEY (EMP_ID_ REFERENCES EMPLOYEE_TBL (EMP_ID)
CUSTOMER_TBL | |||
---|---|---|---|
CUST_ID | VARCHAR2(10) | NOT NULL | PRIMARY KEY |
CUST_NAME | VARCHAR2(30) | NOT NULL | |
CUST_ADDRESS | VARCHAR2(20) | NOT NULL | |
CUST_CITY | VARCHAR2(15) | NOT NULL | |
CUST_STATE | CHAR(2) | NOT NULL | |
CUST_ZIP | NUMBER(5) | NOT NULL | |
CUST_PHONE | NUMBER(10) | ||
CUST_FAX | NUMBER(10) |
ORDERS_TBL | |||
---|---|---|---|
ORD_NUM | VARCHAR2(10) | NOT NULL | PRIMARY KEY |
CUST_ID | VARCHAR2(10) | NOT NULL | |
PROD_ID | VARCHAR2(10) | NOT NULL | |
QTY | NUMBER(6) | NOT NULL | |
ORD_DATE | DATE |
PRODUCTS_TBL | |||
---|---|---|---|
PROD_ID | VARCHAR2(10) | NOT NULL | PRIMARY KEY |
PROD_DESC | VARCHAR2(40) | NOT NULL | |
COST | NUMBER(6,2) | NOT NULL |
1: | Using a subquery, write an SQL statement to update the CUSTOMER_TBL table, changing the customer name to DAVIDS MARKET, who has an order with order number 23E934. |
A1: | The immediate supervisor should instigate the request process by completing a user ID request form, which contains all the information necessary to add the user to the database. The form should then be forwarded to the security officer. The user request is then routed to either the database administrator or the individual designated to assist the database administrator with security, so that the user can be added to the database. This is a general process that should be followed and modified accordingly for each company. |
2: | Using a subquery, write a query that returns all the names of all employees who have a pay rate greater than JOHN DOE, whose employee identification number is 343559876. |
A2: | SELECT E.LAST_NAME, E.FIRST_NAME, E.MIDDLE_NAME FROM EMPLOYEE_TBL E, EMPLOYEE_PAY_TBL P WHERE P.PAY_RATE > (SELECT PAY_RATE FROM EMPLOYEE_PAY_TBL WHERE EMP_ID = '343559876'), |
3: | Using a subquery, write a query that lists all products that cost more than the average cost of all products. |
A3: | SELECT PROD_DESC FROM PRODUCTS_TBL WHERE COST > (SELECT AVG(COST) FROM PRODUCTS_TBL); |