0% found this document useful (0 votes)
3 views

DBMS LAB EXPERIMENT WEEK6

The document outlines various SQL queries using group by, having, order by clauses, and subqueries related to employee data management. It also includes examples of transaction control language commands such as commit, rollback, and savepoint. The queries demonstrate how to retrieve specific employee information based on salary and job roles, as well as manage database transactions.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

DBMS LAB EXPERIMENT WEEK6

The document outlines various SQL queries using group by, having, order by clauses, and subqueries related to employee data management. It also includes examples of transaction control language commands such as commit, rollback, and savepoint. The queries demonstrate how to retrieve specific employee information based on salary and job roles, as well as manage database transactions.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1 Aditya College of Engineering & Technology

DATABASE MANAGEMENT SYSTEMS LAB


EXPERIMENT NO: 6 DATE:
Queries using group by, having, order by classes, sub queries and
co-related sub queries. Transaction control language commands:
Commit, Rollback, and Savepoint.
Group by clause:
Using group by, we can create groups of related information. Columns used in select clause must be
used with group by clause, otherwise it was not a group by expression.
Having clause:
This will work as where clause which can be used only with group by clause because of absence of
where clause in group by.
Order by clause:
This will be used to ordering the columns data (ascending or descending).
ASC (default) and DESC – specify the ordering of values, either ascending or descending

Sub queries and co-related sub queries


1. Display the name of the employee who earns highest salary.
select ename from emp where sal=(select max(sal) from emp);
Result:

2. Display the employee number and name for employee working as clerk and earning highest
salary among clerks.
select empno, ename from emp where job='CLERK' and sal=(select max(sal) from emp where
job='CLERK');
Result:

3. Display the names of salesman who earns salary more than the highest salary of any clerk.
select ename, sal from emp where job='SALESMAN' and sal>(select max(sal) from emp
where job='CLERK');
Result:

Regd. No:
Database Management Systems Lab
2 Aditya College of Engineering & Technology

4. Display the names of clerks who earn a salary more than the lowest salary of any salesman.
select ename from emp where job='CLERK' and sal>(select min(sal) from emp where
job='SALESMAN');
Result:

5. Display the names of the employees who earn highest salary in their respective
departments.
select ename, sal, deptno from emp where sal in(select max(sal) from emp group by
deptno);
Result:

6. Display the names of the employees who earn highest salaries in their respective job groups.
select ename, sal, job from emp where sal in(select max(sal) from emp group by job);
Result:

7. Display the employee names who are working in accounting department.


select ename from emp where deptno=(select deptno from dept where
dname='ACCOUNTING');
Result:

Regd. No:
Database Management Systems Lab
3 Aditya College of Engineering & Technology

8. Display the employee names who are working in Kakinada.


select ename from emp where deptno=(select deptno from dept where LOC='Kakinada');
Result:

9. Display the Job groups having total salary greater than the maximum salary for managers.
SELECT JOB,SUM(SAL) FROM EMP GROUP BY JOB HAVING SUM(SAL)>(SELECTMAX(SAL)
FROM EMP WHERE JOB='MANAGER');
Result:

10. Display the names of employees from department number 10 with salary greater than that
of any employee working in other department.
select ename from emp where deptno=10 and sal>any(select sal from emp where deptno
not in 10)
Result:

11. Display the names of the employees from department number 10 with salary greater than
that of all employee working in other departments.
select ename from emp where deptno=10 and sal>all(select sal from emp where deptno not
in 10);
Result:

Regd. No:
Database Management Systems Lab
4 Aditya College of Engineering & Technology

Transaction control language commands: Commit, Rollback, and Savepoint.


1. select * from emp;

Result:
2. delete from emp where empno=7369;
Result: 1 row deleted.
3. select * from emp;

Result:
4. rollback;
Result: rollback completed.
5. select * from emp;

Result:
6. delete from emp where empno<=7550;
Result: 3 rows deleted.

Regd. No:
Database Management Systems Lab
5 Aditya College of Engineering & Technology

7. commit;
Result: commit completed.
8. rollback;
Result: rollback completed.
9. select * from emp;

Result:
10. insert into emp values(7369,'smith','clear',7902,'17-DEC-1980',800,null,20);
Result: 1 row inserted.
11. insert into emp values(7499,'allen','salesman',7698,'20-FEB-1981',1600,300,30);
Result: 1 row inserted.
12. insert into emp values(7521,'ward','salesman',7698,'22-FEB-1981',1250,500,30);
Result: 1 row inserted.
13. savepoint firstrow;
Result: savepoint created.
14. delete from emp where empno=7369;
Result: 1 row deleted.
15. savepoint secondrow;
Result: savepoint created.
16. delete from emp where empno=7499;
Result: 1 row deleted.
17. savepoint thirdrow;
Result: savepoint created.
18. delete from emp where empno=7521;
Result: 1 row deleted.
19. select * from emp;

Regd. No:
Database Management Systems Lab
6 Aditya College of Engineering & Technology

Result:
20. rollback to secondrow;
Result: rollback completed.
21. Select * from emp;

Result:

Regd. No:
Database Management Systems Lab

You might also like