Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Practice 5: Maximum Minimum Sum Average

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Practice 5

Determine the validity of the following three statements. Circle either True or False.
1. Group functions work across many rows to produce one result per group.
True/False
2. Group functions include nulls in calculations.
True/False
3. The WHEREclause restricts rows prior to inclusion in a group calculation.
True/False
4. Display the highest, lowest, sum, and average salary of all employees. Label the columns
Maximum, Minimum, Sum, and Average, respectively. Round your results to the nearest whole
number. Place your SQL statement in a text file named lab5_6.sql.

SELECT ROUND(MAX(salary),0) "Maximum",


ROUND(MIN(salary),0) "Minimum",
ROUND(SUM(salary),0) "Sum",
ROUND(AVG(salary),0) "Average"
FROM employees;

Maximum Minimum Sum Average

24000 2100 691400 6462

5. Modify the query in lab5_4.sqlto display the minimum, maximum, sum, and average salary for
each job type. Resave lab5_4.sqlto lab5_5.sql. Run the statement in lab5_5.sql.

Introduction to Oracle9i: SQL 5-28


Practice 5 (continued)
SELECT job_id, ROUND(MAX(salary),0) "Maximum",
ROUND(MIN(salary),0) "Minimum",
ROUND(SUM(salary),0) "Sum",
ROUND(AVG(salary),0) "Average"
FROM employees
GROUP BY job_id

job_id Maximum Minimum Sum Average

SH_CLERK 4200 2500 64300 3215

AD_VP 17000 17000 34000 17000

SA_MAN 14000 10500 61000 12200

PU_MAN 11000 11000 11000 11000

IT_PROG 9000 4200 28800 5760

ST_CLERK 3600 2100 55700 2785

FI_MGR 12000 12000 12000 12000

PU_CLERK 3100 2500 13900 2780

HR_REP 6500 6500 6500 6500

ST_MAN 8200 5800 36400 7280

Introduction to Oracle9i: SQL 5-29


Practice 5 (continued)
MK_MAN 13000 13000 13000 13000

AC_MGR 12000 12000 12000 12000

SA_REP 11500 6100 250500 8350

AD_ASST 4400 4400 4400 4400

PR_REP 10000 10000 10000 10000

MK_REP 6000 6000 6000 6000

AD_PRES 24000 24000 24000 24000

FI_ACCOUNT 9000 6900 39600 7920

AC_ACCOUNT 8300 8300 8300 8300

6. Write a query to display the number of people with the same job.
SELECT job_id, COUNT(*)
FROM employees
GROUP BY job_id;

job_id count

SH_CLERK 20

AD_VP 2

Introduction to Oracle9i: SQL 5-30


Practice 5 (continued)
SA_MAN 5

PU_MAN 1

IT_PROG 5

ST_CLERK 20

FI_MGR 1

PU_CLERK 5

HR_REP 1

ST_MAN 5

MK_MAN 1

AC_MGR 1

SA_REP 30

AD_ASST 1

PR_REP 1

MK_REP 1

Introduction to Oracle9i: SQL 5-31


Practice 5 (continued)
AD_PRES 1

FI_ACCOUNT 5

AC_ACCOUNT 1

7. Determine the number of managers without listing them. Label the column Number of
Managers. Hint:Use the MANAGER_IDcolumn to determine the number of managers.

SELECT COUNT(DISTINCT manager_id) "Number of Managers"


FROM employees;

Number of Managers

19

Introduction to Oracle9i: SQL 5-32


Practice 5 (continued)
8. Write a query that displays the difference between the highest and lowest salaries. Label the column
DIFFERENCE.

If you have time, complete the following exercises:


SELECT MAX(salary) - MIN(salary) DIFFERENCE
FROM employees;

difference

21900.00

9. Display the manager number and the salary of the lowest paid employee for that manager.
Exclude anyone whose manager is not known. Exclude any groups where the minimum
salary is less than $6,000. Sort the output in descending order of salary.

Introduction to Oracle9i: SQL 5-33


Practice 5 (continued)
SELECT manager_id, MIN(salary)
FROM employees
WHERE manager_id IS NOT NULL
GROUP BY manager_id
HAVING MIN(salary) > 6000
ORDER BY MIN(salary) DESC;

manager_id min

0 24000.00

102 9000.00

205 8300.00

146 7000.00

145 7000.00

108 6900.00

149 6200.00

147 6200.00

148 6100.00

Introduction to Oracle9i: SQL 5-34


Practice 5 (continued)
10. Write a query to display each department’s name, location, number of employees, and the
average salary for all employees in that department. Label the columns Name, Location,
Number of People, and Salary, respectively. Round the average salary to two decimal
places.

If you want an extra challenge, complete the following exercises:


SELECT d.department_name "Name", d.location_id "Location ",
COUNT(*) "Number of People",
ROUND(AVG(salary),2) "Salary"
FROM employees e, departments d
WHERE e.department_id = d.department_id
GROUP BY d.department_name, d.location_id;

Name Location Number of People

Human 2400 1
Resources

Purchasing 1700 6

Finance 1700 6

Administration 1700 1

Shipping 1500 45

Accounting 1700 2

Introduction to Oracle9i: SQL 5-35


Practice 5 (continued)
Public Relations 2700 1

Sales 2500 34

Marketing 1800 2

IT 1400 5

Executive 1700 3

11. Create a query that will display the total number of employees and, of that total, the number of
employees hired in 1995, 1996, 1997, and 1998. Create appropriate column headings.

SELECT COUNT(*) total,


SUM(DECODE(TO_CHAR(hire_date, 'YYYY'),1995,1,0))"1 995",
SUM(DECODE(TO_CHAR(hire_date, 'YYYY'),1996,1,0))"1 996",
SUM(DECODE(TO_CHAR(hire_date, 'YYYY'),1997,1,0))"1 997",
SUM(DECODE(TO_CHAR(hire_date, 'YYYY'),1998,1,0))"1 998"
FROM employees;
12. Create a matrix query to display the job, the salary for that job based on department number, and
the total salary for that job, for departments 20, 50, 80, and 90, giving each column an
appropriate heading.

Introduction to Oracle9i: SQL 5-36


Practice 5 (continued)

SELECT job_id "Job",


SUM(DECODE(department_id , 20, salary)) "Dept 20" ,
SUM(DECODE(department_id , 50, salary)) "Dept 50" ,
SUM(DECODE(department_id , 80, salary)) "Dept 80" ,
SUM(DECODE(department_id , 90, salary)) "Dept 90" ,
SUM(salary) "Total"
FROM employees
GROUP BY job_id;

Introduction to Oracle9i: SQL 5-37

You might also like