0% found this document useful (0 votes)
17 views7 pages

Lab 5

1. This document contains a student's homework assignment on database queries using subqueries and set operators. 2. The homework has two parts - the first part contains 7 questions involving subqueries to retrieve data from multiple tables. The second part contains 3 questions using set operators like UNION and INTERSECT. 3. The student provides the SQL queries for each question and the output returned by the queries.

Uploaded by

omarkhanfar2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views7 pages

Lab 5

1. This document contains a student's homework assignment on database queries using subqueries and set operators. 2. The homework has two parts - the first part contains 7 questions involving subqueries to retrieve data from multiple tables. The second part contains 3 questions using set operators like UNION and INTERSECT. 3. The student provides the SQL queries for each question and the output returned by the queries.

Uploaded by

omarkhanfar2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Name: Mays Mohammed Ali Alkhalil

ID: 32115333015
DR. JIHAD
SUB: DATABASE
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(Empno) > 6
);

DNAME
Sales

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

SELECT E.EMPNO, E.ENAME, E.JOB, E.DEPTNO, D.LOC


FROM EMP E
JOIN DEPT D ON E.DEPTNO = D.DEPTNO
WHERE D.LOC LIKE '%o%';

EMPNO ENAME JOB DEPTNO LOC


7839 King-27 President 10 New York
7698 Blake-27 Manager 30 Chicago
7782 Clark-27 Manager 10 New York
Salesma
7499 Allen-27 30 Chicago
n
Salesma
7521 Ward-27 30 Chicago
n
Salesma
7654 Martin-27 30 Chicago
n
Salesma
7844 Turner-27 30 Chicago
n
Salesma
7900 James-27 30 Chicago
n
7934 Miller-27 Clerk 10 New York
7700 MAYS Manager 10 New York
Salesma
7702 ALKHALIL 30 Chicago
n
7703 ALI Clerk 10 New York
3. Select the department name and location of all employees working for Clark
SELECT DISTINCT D.DNAME, D.LOC
FROM EMP E
JOIN DEPT D ON E.DEPTNO = D.DEPTNO
WHERE E.SUPERENO IN (SELECT EMPNO 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 E.EMPNO, E.JOB, E.SAL


FROM EMP E
WHERE E.JOB = 'Analyst' AND E.SAL > (
SELECT MAX(SAL)
FROM EMP
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 E.EMPNO, E.ENAME, E.JOB, E.SAL, D.DNAME


FROM EMP E
JOIN DEPT D ON E.DEPTNO = D.DEPTNO
WHERE D.DNAME IN ('Operations', 'Accounting')
AND E.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 E.EMPNO, E.ENAME, E.JOB, E.SAL


FROM EMP E
JOIN DEPT D ON E.DEPTNO = D.DEPTNO
WHERE D.DNAME = 'Sales'
AND E.SAL > (
SELECT AVG(SAL)
FROM EMP
WHERE DEPTNO = (
SELECT DEPTNO
FROM DEPT
WHERE DNAME = 'Sales'
)
);

EMPNO ENAME JOB SAL


7698 Blake-16 Manager 3250
7702 GHANNAM Salesman 2100

Part 2 Set Operators


Q1: Display the department numbers for departments that do not contain the job
SALESMAN. (Use set operators)

SELECT DISTINCT DEPTNO


FROM EMP
WHERE DEPTNO NOT IN (
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 E.EMPNO, E.ENAME, E.SAL, E.DEPTNO


FROM EMP E
WHERE E.SAL > 3000
INTERSECT
SELECT E.EMPNO, E.ENAME, E.SAL, E.DEPTNO
FROM EMP E
JOIN DEPT D ON E.DEPTNO = D.DEPTNO
WHERE D.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 SAL
FROM (
SELECT SAL
FROM EMP
ORDER BY SAL DESC )
WHERE ROWNUM = 1
);

ENAME SAL
King-16 6500

You might also like