Lab 9 Tasks Subqueries - Solved

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

DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING

MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO


Database Management Systems
Lab Experiments
Note: Write a short description and syntax for the statements used. Specify the DBMS
Software/online resource, which you have used to solve the following queries.

Consider the following tables for queries.


EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
DEPT (DEPTNO, DNAME, LOC)

Lab 9: To study and practice SQL sub-queries.


1. Display the employee’s name and hire date for all employees in the same department
as BLAKE. Exclude Blake.

Query:

SELECT ename, hiredate FROM emp


WHERE deptno =(
select deptno from emp
where ename='BLAKE'
)
and ename<>'BLAKE';

Output:

2. Display the employee’s name and salary for all employees who earn more than the
average salary.

Query:

SELECT ename, sal FROM emp


WHERE sal > (
select avg(sal) from emp
)
Output:

3. Display the employee’s name, job, and hire date for all employees who report to
KING.

Query:

SELECT ename, job, hiredate FROM emp


WHERE mgr= (
select empno from emp where ename='KING'
);
Output:

4. List the employee details whose salary is greater than the lowest salary of an
employee belonging to deptno 20.
Query:

SELECT * FROM emp


WHERE sal> (
select min(sal) from emp where deptno=20
);

Output:
5. Which department has the highest Monthly remuneration bill (Salaries of employees)?

Query:

SELECT deptno, sum(sal) as Remuneration_Bill FROM emp


group by deptno
having sum(sal)= (
select max(sum(sal)) from emp
group by deptno
);

Output:

6. Display the employees that earn a salary that is higher than the salary of all the clerks.
Sort the result on salary from highest to lowest.
Query:

SELECT ename, sal from emp


WHERE sal> all(
SELECT sal from emp where JOB='CLERK'
)
order by 'DESC';
Output:

7. Create a query to display the name, hire date, and salary for all employees who have
both the same salary and commission as employee SCOTT.

Query:

SELECT ename, hiredate, sal


FROM emp
WHERE (sal, NVL(comm,0)) = (
SELECT sal, NVL(comm,0)
FROM emp
WHERE ename='SCOTT'
)
and
ename<>'SCOTT'

Output:

8. Display the names and salaries of those employees who earn the highest salary in their
department.
Query:
SELECT ename, sal
FROM emp
WHERE sal IN (
SELECT max(sal)
FROM emp
GROUP BY deptno
);
Output:

9. displays employee names, salaries, department numbers, and average salaries for all
the employees who make more than the average salary in their department.

Query:
SELECT
emp.ename,
emp.sal,
emp.deptno,
(SELECT AVG(sal) FROM emp emp2 WHERE emp2.deptno = emp.deptno) AS avg_sal
FROM
emp
WHERE
emp.sal > (SELECT AVG(sal) FROM emp emp2 WHERE emp2.deptno = emp.deptno);

Output:
10. Display all employees whose job and salary is same as employee ‘ALLEN’.

Query:

SELECT ename from emp


WHERE (job,sal) IN (
SELECT job, sal from emp where ename='ALLEN'
)
AND ename<>'ALLEN';

Output:
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO
Database Management Systems
Lab Experiments
Note: Write a short description and syntax for the statements used. Specify the DBMS
Software/online resource, which you have used to solve the following queries.

Consider the following tables for queries.


EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
DEPT (DEPTNO, DNAME, LOC)

Lab 10: To Update and Delete the contents of a Table and use of commit and
rollback commands.
1. Create My_Employee table whose structure and data is same as EMP table. Use
My_Employee table for following queries.

Query:

CREATE TABLE My_Employee AS


SELECT * FROM EMP

2. Make the data additions permanent.


Query & Output:

3. Change the name of employee 7722 to Drexler.


Query:

Update My_Employee
SET ENAME='Drexler'
WHERE empno=7722;

Output:
4. Change the salary to 1000 for all employees with a salary less than 900.

Query:
Update My_Employee
SET sal=1000
WHERE sal<900;

Output:

5. Delete employee Allen from the MY_EMPLOYEE table.

Query:

DELETE FROM My_Employee


WHERE ename='ALLEN';

Output:

6. Mark an intermediate point as A in the processing of the transaction.


Query:
SAVEPOINT A;

7. Empty the entire table.


Query:

DELETE FROM My_Employee

Output:

8. Discard the most recent DELETE statement without discarding the earlier statements.
Query:
ROLLBACK TO SAVEPOINT A;
9. Save the changing permanently.
Query:
Commit;
10. Update the salary of employee 100 to 5000.
Query:
update My_Employee
set sal=5000
WHERE empno=100

11. Update the salary of employee 7722 to a salary same as that of employee “KING”.
Query:

update My_Employee
set sal=(
select sal from My_Employee where ename='KING'
)
where empno=7722;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy