SQL Home Eercises Practice Solutions

advertisement
KING FAHD UNIVERSITY OF PETROLEUM AND MINERALS
DEPARTMENT OF ACCOUNTING AND MIS
MIS 311 - BUSINESS DATA MANAGEMENT
FALL SEMESTER (042)
SQL EXERCISES
Using the Employee database tables, write SQL statements for the following
problems:
1.
Display the last name, first name, department number, date of
birth, date of hire, and salary of all employees earning $30,000 or
more per annum.
SELECT LASTNAME, FIRSTNME, WORKDEPT, BIRTHDATE,
HIREDATE, SALARY
FROM EMPLOYEE
WHERE SALARY >= 30000;
2.
List all the information for any department whose manager is
unknown.
SELECT *
FROM DEPARTMENT
WHERE MGRNO IS NULL;
3.
Modify the SQL statement for problem 1 such that the results will
be sorted by first name within last name.
SELECT LASTNAME, FIRSTNME, WORKDEPT, BIRTHDATE,
HIREDATE, SALARY
FROM EMPLOYEE
WHERE SALARY >= 30000
ORDER BY LASTNAME, FIRSTNME;
4.
List the department number and name of any department that
includes SERVICE in its name.
SELECT DEPTNO, DEPTNAME
FROM DEPARTMENT
WHERE DEPTNAME LIKE '*SERVICE*';
1
5.
Display the employee number, last name, department number, and
phone number of employees whose department number is between
D11 and D21 (inclusive).
SELECT EMPNO, LASTNAME, WORKDEPT, PHONENO
FROM EMPLOYEE
WHERE WORKDEPT BETWEEN 'D11' AND 'D21';
6.
Display the employee number, first name, and last name (in last
name sequence) for all employees whose department number begins
with the letter E.
SELECT EMPNO, FIRSTNME, LASTNAME
FROM EMPLOYEE
WHERE WORKDEPT LIKE 'E*'
ORDER BY LASTNAME;
7.
Produce a list of employees in departments B01, C01, and D01,
showing last name, department number and their earnings (salary
plus commission). The output must be in descending order of total
earnings within department.
Step 1
UPDATE EMPLOYEE
SET COMM = 0.0
WHERE COMM IS NULL;
Step 2
SELECT LASTNAME, WORKDEPT, (SALARY + COMM) AS TOTEARN
FROM EMPLOYEE
WHERE WORKDEPT IN ('B01','C01','D01')
ORDER BY WORKDEPT, TOTEARN DESC;
Notice Run the above two queries separatly.
8.
Produce a list of men whose monthly salary is less than $1,600,
showing employee number, last name, and monthly salary in
descending order by monthly salary.
SELECT EMPNO, LASTNAME, SALARY/12 AS MONTHLY_SALARY
FROM EMPLOYEE
WHERE SEX = 'M'
AND SALARY/12 < 1600.00
ORDER BY 3 DESC;
Notice the number 3 in the ORDER BY represents the sequence number of
MONTHLY_SALARY in the above SELECT list.
2
9.
List the last name, salary, job title, and education level for any
employee who meets either of the following conditions :
• Salary is greater than $40,000.
• Job is manager and education level is less than 16.
SELECT LASTNAME, SALARY, JOB AS JOB_TITLE, EDLEVEL AS
EDUCATION_LEVEL
FROM EMPLOYEE
WHERE SALARY > 40000
OR (JOB = 'MANAGER' AND EDLEVEL < 16);
10.
Display the sum of all company salaries along with the company
average salary, the minimum salary, and the maximum salary.
SELECT SUM(SALARY) AS TOTAL_SALARY, AVG(SALARY) AS
AVERAGE_SALARY, MIN(SALARY) AS MINIMUM_SALARY,
MAX(SALARY) AS MAXIMUM_SALARY
FROM EMPLOYEE;
11.
Display the last name of the employee whose last name comes first
alphabetically.
SELECT MIN(LASTNAME)
FROM EMPLOYEE;
12.
Display the average salary, and job title for each job where the
average is greater than $35,000.
SELECT JOB, AVG(SALARY) AS AVERAGE_SALARY
FROM EMPLOYEE
GROUP BY JOB
HAVING (AVG(SALARY) > 35000);
3
13.
Produce a list showing the department number, the average salary,
and the number of employees for each department excluding the job
of PRES.
Exclude departments for which fewer than 4 employee salaries have
been averaged. Sequence the list in descending order by number of
employees.
SELECT WORKDEPT, AVG(SALARY) AS AVERAGE_SALARY,
COUNT(EMPNO) AS DEPT_COUNT
FROM EMPLOYEE
WHERE JOB <> “PRES”
GROUP BY WORKDEPT
HAVING (COUNT(EMPNO) > 3)
ORDER BY COUNT(EMPNO) Desc;
14.
Display last name, job title, and department name of employees
whose department name includes PLAN.
SELECT E.LASTNAME, E.JOB, D.DEPTNAME
FROM EMPLOYEE AS E, DEPARTMENT AS D
WHERE E.WORKDEPT=D.DEPTNO
AND D.DEPTNAME LIKE '*PLAN*';
15.
Display last name and first name of all employees who work in the
same department as ADAMSON.
SELECT E.LASTNAME, E.FIRSTNME
FROM EMPLOYEE AS E, EMPLOYEE AS F
WHERE E.WORKDEPT=F.WORKDEPT
AND F.LASTNAME='ADAMSON';
16.
Display the average salary for men and the average salary for women for
each department. Identify the department by both department number
and name. Order the results by descending average salaries
within departments.
SELECT DEPTNO, DEPTNAME, AVG(SALARY) AS AVERAGE_SALARY,
SEX
FROM EMPLOYEE, DEPARTMENT
WHERE WORKDEPT = DEPTNO
GROUP BY DEPTNO, DEPTNAME, SEX
ORDER BY 1, 3 DESC;
Notice the number 1, 3 in the ORDER BY represents the sequence number of DEPTNO
and AVERAGE_SALARY in the above SELECT list.
4
17.
Produce a list of all employees in departments B01, C01, and E21.
Include a literal 'manager' for managers and 'non-manager' for
employees that are not managers. Include employee number, last
name, and first name. (Hint : Think UNION).
SELECT EMPNO, LASTNAME, FIRSTNME, 'NON-MANAGER'
FROM EMPLOYEE
WHERE JOB <> 'MANAGER' AND WORKDEPT IN ('B01',
'C01', 'E21')
UNION ALL
SELECT EMPNO, LASTNAME, FIRSTNME, 'MANAGER'
FROM EMPLOYEE
WHERE JOB = 'MANAGER' AND WORKDEPT IN ('B01',
'C01', 'E21');
18.
Display first name, last name, job title, date of hire for the employee
with the most seniority (or the most senior employee).
SELECT FIRSTNME, LASTNAME, PHONENO, HIREDATE, JOB
FROM EMPLOYEE
WHERE HIREDATE = (SELECT MIN(HIREDATE) FROM
EMPLOYEE);
Notice: change the problem to to find the most junior employee?
SELECT FIRSTNME, LASTNAME, PHONENO, HIREDATE, JOB
FROM EMPLOYEE
WHERE HIREDATE = (SELECT MAX(HIREDATE) FROM
EMPLOYEE);
19.
List each department that has an average salary less than the
company average. Show department number and average salary.
SELECT WORKDEPT, AVG(SALARY)
FROM EMPLOYEE
GROUP BY WORKDEPT
HAVING AVG(SALARY) < (SELECT AVG(SALARY) FROM
EMPLOYEE);
5
20.
Produce a list of all female employees in the 'E' work departments
who earn more than the average salary of the male employees in the
'E' departments. Display the employee number, first name, and last
name.
SELECT EMPNO, FIRSTNME, LASTNAME
FROM EMPLOYEE
WHERE WORKDEPT LIKE 'E*' AND SEX = 'F'
AND SALARY > (SELECT AVG(SALARY)
FROM EMPLOYEE
WHERE WORKDEPT LIKE 'E*' AND SEX='M');
21.
List all employees of department E11 that have a salary that is less
than the average salary of each and every department. Display first
name, last name, and salary.
SELECT FIRSTNME, LASTNAME, SALARY
FROM EMPLOYEE
WHERE WORKDEPT = 'E11'
AND SALARY < ALL
(SELECT AVG(SALARY) FROM EMPLOYEE GROUP BY
WORKDEPT);
6
Download