Assignment A (solutions)

advertisement
MIS 4443/5603: Advanced Database Management, Spring 2007
Assignment A (Solutions)
1. List the names of employees who have worked at the firm in the past, the names of
departments they used to work for and the location they worked in.
SELECT eh.empid, eh.empname, d.dname, d.loc
FROM empHistory_tbl eh, dept_tbl d
WHERE eh.empid NOT IN
(SELECT e.empno
FROM employee_tbl e)
AND eh.deptid=d.deptno
2. Which employees who quit, were rehired? List their names and the names of the department
they are currently working for.
SELECT e.empno, e.ename, d.dname
FROM employee_tbl e, empHistory_tbl eh, dept_tbl d
WHERE e.empno=eh.empid AND
e.deptno=d.deptno
3. Which department has never had an employee quit? (even thought it may not have any
employees to quit!)
SELECT eh.empid, d.dname, d.loc
FROM dept_tbl d LEFT JOIN empHistory_tbl eh
ON d.deptno=eh.deptid
WHERE eh.empid IS NULL
4. What is the average salary paid by the firm?
SELECT AVG(sal) 'Average Salary'
FROM employee_tbl
5. Who else has the same manager as EMPNO 7499, Allen? Do not repeat Allen’s name in the
result.
SELECT ename
FROM employee_tbl
WHERE mgr=(SELECT mgr
FROM employee_tbl
WHERE empno=7499)
AND empno<>7499
6. Name the employee that makes the lowest salary.
SELECT ename, sal
FROM employee_tbl
WHERE sal=(SELECT MIN(sal)
FROM employee_tbl)
7. How many people get commissions?
SELECT COUNT(comm)
FROM employee_tbl
8. How many people are not eligible for any commissions (as seen by the null values against
their names)?
SELECT COUNT(*)
FROM employee_tbl
WHERE comm IS NULL
9. List the department numbers, job titles and the total salaries paid to each job within a
department. In the output also include summary data – at the job level, the department level
and for the overall company.
SELECT deptno, job, SUM(sal)
FROM employee_tbl
GROUP BY deptno, job
WITH CUBE
SELECT deptno, job, SUM(sal)
FROM employee_tbl
GROUP BY deptno, job
WITH ROLLUP
10. List the names and salaries of employees whose salaries are in the top 10% of salaries paid by
the company.
SELECT TOP 10 PERCENT WITH TIES
ename, sal
FROM employee_tbl
ORDER BY sal DESC
11. List the names and salaries of employees (grouped by their job titles) and provide salary subtotals for each job position.
SELECT ename, job, sal
FROM employee_tbl
ORDER BY job
COMPUTE SUM(sal) BY job
12. List the MGRs (manager numbers) of those managers whose total monthly employee payroll
exceeds $6,000. Order the list from highest to lowest.
SELECT mgr, SUM(sal)
FROM employee_tbl
GROUP BY mgr
HAVING SUM(sal) > 6000
ORDER BY 2 DESC
2
13. Display the employee name and salary of all employees who report to King.
SELECT ename, sal
FROM employee_tbl
WHERE mgr IN (SELECT empno
FROM employee_tbl
WHERE ename = 'KING')
14. Write a query to display the employee name and hire date for all employees in the same
department as Blake. Exclude Blake in the result.
SELECT ename, hiredate
FROM employee_tbl
WHERE deptno IN (SELECT deptno
FROM employee_tbl
WHERE ename = 'BLAKE')
AND ename <> 'BLAKE'
15. Display the employee name, department number, and job title for all employees whose
department location is Dallas.
SELECT ename, deptno, job
FROM employee_tbl
WHERE deptno IN (SELECT deptno
FROM dept_tbl
WHERE loc = 'DALLAS')
16. Create a query to display the employee number and name for all employees who earn more
than the average salary. Sort the results in descending order of salary.
SELECT empno, ename
FROM employee_tbl
WHERE sal > (SELECT AVG(sal)
FROM employee_tbl)
ORDER BY sal DESC
17. Display the department number, name, and job for all employees in the Sales department.
SELECT deptno, ename, job
FROM employee_tbl
WHERE deptno IN (SELECT deptno
FROM dept_tbl
WHERE dname = 'SALES')
18. Create a query to display the employees that earn a salary that is higher than the salary of
all of the clerks. Sort the results on salary from highest to lowest.
SELECT ename, job, sal
FROM employee_tbl
WHERE sal > ALL (SELECT sal
FROM employee_tbl
WHERE job = 'CLERK')
ORDER BY sal DESC
3
19. List employees (and their departments) where their total compensation is between $2,500 and
$3,500. Order from highest to lowest.
SELECT ename, deptno, sal + ISNULL(comm,0) compensation
FROM employee_tbl
WHERE sal + ISNULL(comm,0) > 2500
AND sal + ISNULL(comm,0) < 3500
ORDER BY 3 DESC, 1 ASC
20. How many positions are there in each department?
SELECT e.deptno, COUNT(*) [Number of Positions]
FROM employee_tbl e
GROUP BY e.deptno
4
Download