Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Homework 5

Part 1 : SUBQUERIES

1. list the department names having more than 6 employees in it.

SELECT dname
FROM dept
WHERE deptno IN (
SELECT deptno
FROM emp
GROUP BY deptno
HAVING COUNT(*) > 6
);
DNAME
Sales

2. Display the employees whose location which has at least one o in it.

SELECT *
FROM emp
WHERE LOWER (loc) LIKE '%o%';

EMPNO ENAME JOB DEPTNO LOC


7839 King11 President 10 New York
7698 Blake11 Manager 30 Chicago
7782 Clark11 Manager 10 New York
7499 Allen11 Salesman 30 Chicago
7521 Ward11 Salesman 30 Chicago
7654 Martin11 Salesman 30 Chicago
7844 Turner11 Salesman 30 Chicago
7900 James11 Salesman 30 Chicago
7934 Miller11 Clerk 10 New York
7700 Omar Manager 10 New York
7702 Khanfar Salesman 30 Chicago
Mahmoo
7703 Clerk 10 New York
d

3. Select the department name and location of all employees working for Clark
SELECT dname, loc
FROM dept
WHERE deptno = (
SELECT deptno
FROM emp
WHERE ename = 'Clark'
);

4. Display empno,job and salary of all the Analyst who are earning more than any of
the manager

SELECT empno, job, salary


FROM employees
WHERE job = 'Analyst'
AND salary > ALL (
SELECT MAX(salary)
FROM employees
WHERE job = 'Manager'
);

5. Select 2nd maximum salary.

SELECT MAX(SAL) AS SecondHighestSalary


FROM EMP
WHERE SAL < (
SELECT MAX(SAL)
FROM EMP
);

SECONDHIGHESTSALARY
4000

6. Display all the employees in 'Operations and Accounting' Dept. list the employees
who has salary greater than Miller

SELECT *
FROM emp
WHERE deptno IN (
SELECT deptno
FROM dept
WHERE deptno IN ('Operations', 'Accounting')
) AND sal > (
SELECT sal
FROM emp
WHERE ename = 'Miller'
);
7. List employees who work for SALES department and their salary greater than
average salary of their department.

SELECT *
FROM emp
WHERE deptno = (
SELECT deptno
FROM dept
WHERE dname = 'SALES'
) AND sal > (
SELECT AVG(sal)
FROM emp
WHERE deptno = (
SELECT deptno
FROM dept
WHERE dname = 'SALES'
)
);

EMPNO ENAME JOB SAL


7698 Blake11 Manager 3250
Salesma
7702 Khanfar 2100
n

Part 2 Set Operators


Q1: Display the department numbers for departments that do not contain the job
SALESMAN. (Use set operators)
SELECT deptno
FROM emp
WHERE job <> 'SALESMAN'
MINUS
SELECT deptno
FROM emp
WHERE job = 'SALESMAN';

DEPTNO
30
20
10

Q2: Display the employee name, salary and department no for all the employees who
earn more than 3000 and who work in a department with name contains the character
C. (Use set operators).
SELECT ename, salary, deptno
FROM emp
WHERE sal > 3000
INTERSECT
SELECT ename, sal, deptno
FROM emp
WHERE deptno IN (
SELECT deptno
FROM dept
WHERE LOWER(dname) LIKE '%c%'
);
Q3: Display the employee name and salary for employees who earn the same salary
as the maximum salary. Without using group functions.

SELECT E.ENAME, E.SAL


FROM EMP.E
WHERE E.SAL = (
SELECT MAX( SAL)
FROM EMP
);

ENAME SAL
King11 6500

You might also like