Solved SQL Querries For Revision
Solved SQL Querries For Revision
Solved SQL Querries For Revision
This table is a
subset of the EMPLOYEE table. The basic EMP_1 table structure is summarized in the table below.
(Note that the JOB_CODE is the FK to JOB.)
ATTRIBUTE (FIELD) NAME DATA DECLARATION
EMP_NUM CHAR(3)
EMP_LNAME VARCHAR(15)
EMP_FNAME VARCHAR(15)
EMP_INITIAL CHAR(1)
EMP_HIREDATE DATE
JOB_CODE CHAR(3)
2. Having created the table structure in Problem 1, write the SQL code to enter the first two rows for the
table shown in Figure P7.2.
INSERT INTO EMP_1 VALUES (‘101’, ‘News’, ‘John’, ‘G’, ’08-Nov-00’, ‘502’);
INSERT INTO EMP_1 VALUES (‘102’, ‘Senior’, ‘David’, ‘H’, ’12-Jul-89’, ‘501’);
5. Write the SQL code to change the job code to 501 for the person whose employee number
(EMP_NUM) is 107. After you have completed the task, examine the results, and then reset the job
code to its original value.
UPDATE EMP_1
SET JOB_CODE = ‘501’
WHERE EMP_NUM = ‘107’;
6. Write the SQL code to delete the row for the person named William Smithfield, who was hired on
June 22, 2004, and whose job code classification is 500. (Hint: Use logical operators to include all of
the information given in this problem.)
DELETE FROM EMP_1
WHERE EMP_LNAME = 'Smithfield'
AND EMP_FNAME = 'William'
AND EMP_HIREDATE = '22-June-04'
AND JOB_CODE = '500';
8. Write the SQL code to create a copy of EMP_1, naming the copy EMP_2. Then write the SQL
code that will add the attributes EMP_PCT and PROJ_NUM to its structure. The EMP_PCT is
the bonus percentage to be paid to each employee. The new attribute characteristics are:
EMP_PCTNUMBER(4,2)
PROJ_NUMCHAR(3)
(Note: If your SQL implementation allows it, you may use DECIMAL(4,2) rather than
NUMBER(4,2).)
There are two ways to get this job done. The two possible solutions are shown next.
Solution A:
Solution B:
UPDATE EMP_2
SET PROJ_NUM = '25'
WHERE JOB_CODE > = '502'
12. Write the SQL code that will change the PROJ_NUM to 14 for those employees who were hired
before January 1, 1994 and whose job code is at least 501. (You may assume that the table will be
restored to its condition preceding this question.)
UPDATE EMP_2
SET PROJ_NUM = '14'
WHERE EMP_HIREDATE <= ' 01-Jan-94'
AND JOB_CODE >= '501';
13. Write the two SQL command sequences required to:
There are many ways to accomplish both tasks. We are illustrating the shortest way to do the job next.
a. Create a temporary table named TEMP_1 whose structure is composed of the EMP_2 attributes
EMP_NUM and EMP_PCT.
The SQL code shown in problem 13b contains the solution for problem 13a.
21. Write the SQL code to calculate the ASSIGN_CHARGE values in the ASSIGNMENT table in the
Ch07_ConstructCo database. (See Figure P7.1.) Note that ASSIGN_CHARGE is a derived
attribute that is calculated by multiplying ASSIGN_CHG_HR by ASSIGN_HOURS.
UPDATE ASSIGNMENT
SET ASSIGN_CHARGE = ASSIGN_CHG_HR * ASSIGN_HOURS;
22. Using the data in the ASSIGNMENT table, write the SQL code that will yield the total number of
hours worked for each employee and the total charges stemming from those hours worked. The
results of running that query are shown in Figure P7.22.
SELECT ASSIGNMENT.PROJ_NUM,
Sum(ASSIGNMENT.ASSIGN_HOURS) AS SumOfASSIGN_HOURS,
Sum(ASSIGNMENT.ASSIGN_CHARGE) AS SumOfASSIGN_CHARGE
FROM ASSIGNMENT
GROUP BY ASSIGNMENT.PROJ_NUM
24. Write the SQL code to generate the total hours worked and the total charges made by all employees.
The results are shown in Figure P7.24. (Hint: This is a nested query. If you use Microsoft Access, you
can generate the result by using the query output shown in Figure P7.22 as the basis for the query
that will produce the output shown in Figure P7.24.)
Solution A:
SELECT Sum(SumOfASSIGN_HOURS) AS SumOfASSIGN_HOURS,
Sum(SumOfASSIGN_CHARGE) AS SumOfASSIGN_CHARGE
FROM Q23;
or
SELECT Sum(SumOfASSIGN_HOURS) AS SumOfASSIGN_HOURS,
Sum(SumOfASSIGN_CHARGE as SumOfASSIGN_CHARGE
FROM (SELECT ASSIGNMENT.PROJ_NUM,
Sum(ASSIGNMENT.ASSIGN_HOURS) AS SumOfASSIGN_HOURS,
Sum(ASSIGNMENT.ASSIGN_CHARGE) AS
SumOfASSIGN_CHARGE
FROM ASSIGNMENT
GROUP BY ASSIGNMENT.PROJ_NUM
);
Solution B:
SELECT Sum(SumOfASSIGN_HOURS) AS SumOfASSIGN_HOURS,
Sum(SumOfASSIGN_CHARGE) AS SumOfASSIGN_CHARGE
FROM Q22;
or
27. Write a query to count the number of customers with a customer balance over $500.
SELECT COUNT(*)
FROM CUSTOMER
WHERE CUS_BALANCE >500;
29. Using the output shown in Figure P7.29 as your guide, generate the listing of customer purchases,
including the subtotals for each of the invoice line numbers. (Hint: Modify the query format used
to produce the listing of customer purchases in Problem 18, delete the INV_DATE column, and
add the derived (computed) attribute LINE_UNITS * LINE_PRICE to calculate the subtotals.)
30. Modify the query used in Problem 29 to produce the summary shown in Figure P7.30.
31. Modify the query in Problem 30 to include the number of individual product purchases made by each
customer. (In other words, if the customer’s invoice is based on three products, one per
LINE_NUMBER, you would count three product purchases. If you examine the original invoice data,
you will note that customer 10011 generated three invoices, which contained a total of six lines, each
representing a product purchase.) Your output values must match those shown in Figure P7.31.