You are on page 1of 15

Ex No : 03

Date :
GROUP FUNCTIONS AND SUBQUERIES

COMMON GROUP FUNCTIONS:

COUNT ( )
COUNT ( * ) : Returns number of rows from the table including duplication and null values.
COUNT ( expr ) : Returns number of non null values in given column or expression.
MAX ( ) : Returns maximum value in the column.
MIN ( ) : Returns minimum value in the column.
SUM ( ) : Returns the sum of all values in the column.
AVG ( ) : Returns average of all values in the column.

Using Group functions

SELECT column ,group_functions(col) FROM table [WHERE condition] [ORDER BY column];

Using GROUP BY clause

SELECT column ,group_functions(col) FROM table [WHERE condition]


[GROUP BY group_by_exp] [ORDER BY column];

Using HAVING Clause

SELECT column ,group_functions(col) FROM table [WHERE condition]


[GROUP BY group_by_exp] [HAVING group_condition] [ORDER BY column];

SUB QUERIES :

SELECT select_list FROM table WHERE expr operator (SELECT select_list FROM table);

 The subquery (inner query) executes once before the main query(outer query).
 The main query uses the result of sub query

Guidelines for sub queries

 A sub-query must be enclosed in parenthesis.


 A sub-query must appear on the right side of comparison operator .
 Sub-queries cannot contain an ORDER BY clause .
 Use single row operators with single row sub-queries .
 Use multiple row operators with multiple row sub-queries .

Types of sub-queries

1. Single row sub-queries

SHASHIKIRAN V., ASSISTANT PROFESSOR (SENIOR), SITE, VIT VELLORE.


A single row sub query is one that returns only one row from the inner SELECT
statement .This type of sub-query uses a single row operator.

Single row comparison operators. = ,> , >= ,< , <= ,<>.


2. Multiple row sub-queries

A multiple row sub-query is one that returns more than one row from the inner
SELECT statement. This type of sub-query uses multiple row comparison operators.

Multiple row comparison operators.

IN - Equal to any member in the sub-query’s result list.


ANY - Compare value to each value returned by the sub-query.
< ANY means less than the maximum.
> ANY means more than the minimum.
= ANY is equivalent to IN.
ALL - Compare value to every value returned by the sub-query .

3. Inline Views

A sub-query in the FROM clause used for defining an intermediate result set to query from.

4. Multiple-column sub-queries

A sub-query that contains more than one column of return data in addition to ,how ever
many rows are given in the output .

QUERIES:

01. Retrieve the birth date and address of the employee(s) whose name ‘John B Smith’.

SQL> select bdate,address from employee where fname='John' and minit='B' and lname='Smith';
BDATE ADDRESS
--------- ------------------------------
09-JAN-65 731 Fondren,Houston,TX

02. For each employee, retrieve the employee’s first and last name and the first and last name
of his or her immediate supervisor.

SQL> select e.fname,e.lname,s.fname,s.lname from employee e,employee s where e.superssn=s.ssn;


FNAME LNAME FNAME LNAME
--------------- --------------- --------------- ---------------
John Smith Franklin Wong
Franklin Wong James Borg
Alicia Zelaya Jennifer Wallace
Jennifer Wallace James Borg
Ramesh Narayan Franklin Wong
Joyce English Franklin Wong
Ahmad Jabbar Jennifer Wallace

SHASHIKIRAN V., ASSISTANT PROFESSOR (SENIOR), SITE, VIT VELLORE.


03. Display ssn of all employees.

SQL> select ssn from employee;


SSN
---------
123456789
333445555
999887777
987654321
666884444
453453453
987987987
888665555

04. Select all combinations of employee ssn and department dname .

SQL> select ssn,dname from employee,department;

SSN DNAME
--------- ---------------
123456789 Research
333445555 Research
999887777 Research
987654321 Research
666884444 Research
453453453 Research
987987987 Research
888665555 Research
123456789 Administration
333445555 Administration
999887777 Administration

SSN DNAME
--------- ---------------
987654321 Administration
666884444 Administration
453453453 Administration
987987987 Administration
888665555 Administration
123456789 Headquarters
333445555 Headquarters
999887777 Headquarters
987654321 Headquarters
666884444 Headquarters
453453453 Headquarters

SSN DNAME
--------- ---------------
987987987 Headquarters
888665555 Headquarters

24 rows selected.

05. Retrieve the salary of every employee.

SQL> select all salary from employee;


SHASHIKIRAN V., ASSISTANT PROFESSOR (SENIOR), SITE, VIT VELLORE.
SALARY
----------
30000
40000
25000
43000
38000
25000
25000
55000

8 rows selected.

06. Retrieve all employees whose address is in Houston, Texas. .

SQL> select fname,lname from employee where address like '%Houston,TX%';


FNAME LNAME
--------------- ---------------
John Smith
Franklin Wong
Joyce English
Ahmad Jabbar
James Borg

07. Show the resulting salaries if every employee working on the ‘ProductX’ project is given a
10 percent raise.

SQL> select fname,lname,1.1*salary from employee,works_on,project where ssn=essn and


pno=pnumber and pname='ProductX';
FNAME LNAME 1.1*SALARY
--------------- --------------- ----------
John Smith 33000
Joyce English 27500

08. Retrieve all employees in department 5 whose salary is between $30,000 and $40,000.

SQL> select * from employee where ( salary between 30000 and 40000) and dno=5;

FNAME M LNAME SSN BDATE ADDRESS


--------------- - --------------- --------- --------- -----------------------
John B Smith 123456789 09-JAN-65 731 Fondren,Houston,TX
Franklin T Wong 333445555 08-DEC-55 638 Voss,Houston,TX
Ramesh K Narayan 666884444 15-SEP-62 975 Fire Oak,Humble,TX
S SALARY SUPERSSN DNO
- ---------- --------- ----------
M 30000 333445555 5
M 40000 888665555 5
M 38000 333445555 5

09. Retrieve a list of employees and the projects they are working on, ordered by department
and within each department, ordered alphabetically by last name, first name.

SQL> select dname,lname,fname,pname from department t,employee,works_on,project


SHASHIKIRAN V., ASSISTANT PROFESSOR (SENIOR), SITE, VIT VELLORE.
2 where dnumber=dno and ssn=essn and pno=pnumber order by dname,lname,fname;
DNAME LNAME FNAME PNAME
--------------- --------------- --------------- ---------------
Administration Jabbar Ahmad Newbenefits
Administration Jabbar Ahmad Computerization
Administration Wallace Jennifer Reorganization
Administration Wallace Jennifer Newbenefits
Administration Zelaya Alicia Newbenefits
Administration Zelaya Alicia Computerization
Research English Joyce ProductX
Research English Joyce ProductY
Research Narayan Ramesh ProductZ
Research Smith John ProductX
Research Smith John ProductY
Research Wong Franklin ProductY
Research Wong Franklin Reorganization
Research Wong Franklin ProductZ
Research Wong Franklin Computerization

10. Retrieve the name of each employee who has a dependent with the same first name and
same sex as the employee.

SQL> select e.fname,e.lname from employee e where e.ssn


2 in (select essn from dependent where e.fname=dependent_name and e.sex=sex);
no rows selected

11. Retreive the social security numbers of all employees who works on
project number 1,2 or 3.

SQL> select distinct essn from works_on where pno in(1,2,3);


ESSN
---------
123456789
333445555
453453453
666884444

12. Retrieve the names of all employees who do not have supervisors.

SQL> select fname,lname from employee where superssn is NULL;

FNAME LNAME
--------------- ---------------
James Borg

13. Find the sum of the salaries of all employees, the maximum salary, the minimum salary,
and the average salary.

SQL> select sum(salary),max(salary),min(salary),avg(salary) from employee;


SUM(SALARY) MAX(SALARY) MIN(SALARY) AVG(SALARY)
----------- ----------- ----------- -----------
281000 55000 25000 35125

SHASHIKIRAN V., ASSISTANT PROFESSOR (SENIOR), SITE, VIT VELLORE.


14. Find the sum of the salaries of all employees of the ‘Research’ department, as well as the
maximum salary, the minimum salary, and the average salary in this department.

SQL> select sum(salary),max(salary),min(salary),avg(salary) from employee,department


2 where dno=dnumber and dname='Research';
SUM(SALARY) MAX(SALARY) MIN(SALARY) AVG(SALARY)
----------- ----------- ----------- -----------
133000 40000 25000 33250

15. Retrieve the total number of employees in the company.

SQL> select count (*) from employee ;


COUNT(*)
----------
8

16. Retrieve the number of employees in the ‘Research’ department .

SQL> select count (*) from employee,department where dno=dnumber and dname='Research';

COUNT(*)
----------
4

17. Count the number of distinct salary values in the database.

SQL> select count (distinct salary) from employee;


COUNT(DISTINCTSALARY)
---------------------
6

18. For each department, retrieve the department number, the number of employees in the
department, and their average salary.

SQL> select dno,count (*),avg (salary) from employee group by dno;


DNO COUNT(*) AVG(SALARY)
---------- ---------- -----------
1 1 55000
4 3 31000
5 4 33250

19. For each project, retrieve the projrct number, the project name, and the number of
employees who work on that project.

SQL> select pnumber,pname,count(*) from project,works_on where pnumber=pno group by


pnumber,pname;

SHASHIKIRAN V., ASSISTANT PROFESSOR (SENIOR), SITE, VIT VELLORE.


PNUMBER PNAME COUNT(*)
---------- --------------- ----------
1 ProductX 2
2 ProductY 3
3 ProductZ 2
10 Computerization 3
20 Reorganization 2
30 Newbenefits 3

6 rows selected.

20. For each project on which more than two employees work, retrieve the project number, the
project name, and the number of employees who work on the project.

SQL> select pnumber,pname,count(*) from project,works_on where pnumber=pno group by


pnumber,pname having count(*)>2;
PNUMBER PNAME COUNT(*)
---------- --------------- ----------
2 ProductY 3
10 Computerization 3
30 Newbenefits 3

21. For each project, retrieve the project number, the project name, and the number of
employees from department 5 who work on the project.

SQL> select pnumber,pname,count(*) from project,works_on,employee where pnumber=pno and


ssn=essn and dno=5 group by pnumber,pname;
PNUMBER PNAME COUNT(*)
---------- --------------- ----------
1 ProductX 2
2 ProductY 3
3 ProductZ 2
10 Computerization 1
20 Reorganization 1

22. For each department that has more than five employees, retrieve the department number
and the number of its employees who are making more than $40,000.

SQL> select dnumber,count(*) from department,employee where dnumber=dno and salary>40000


and dno in(select dno from employee group by dno having count(*)>5) group by dnumber;
no rows selected

23. Find the number of employees who get commission.

SQL> select count(comm) from emp where deptno=30;


COUNT(COMM)
-----------
4

SHASHIKIRAN V., ASSISTANT PROFESSOR (SENIOR), SITE, VIT VELLORE.


24. Find the average commission for employees.

SQL> select avg(nvl(comm,0)) from emp;


AVG(NVL(COMM,0))
----------------
157.142857

25. Find the average salary in each department.

SQL> select deptno,avg(sal) from emp group by deptno;

DEPTNO AVG(SAL)
---------- ----------
10 2916.66667
20 2175
30 1566.66667

26. Find the departments having maximum salary greater than $2000.

SQL> select deptno,max(sal) from emp group by deptno having max(sal)>2000;


DEPTNO MAX(SAL)
---------- ----------
10 5000
20 3000
30 2850

27. Find the departments having average salary greater than $2000.

SQL> select deptno,max(sal) from emp group by deptno having avg(sal)>2000;


DEPTNO MAX(SAL)
---------- ----------
10 5000
20 3000

28. Display the job title and monthly salary for every job whose payroll exceeds $5000.

SQL> select job,sum(sal) from emp group by job having sum(sal)>5000 order by sum(sal);
JOB SUM(SAL)
--------- --------------
SALESMAN 5600
ANALYST 6000
MANAGER 8275

29. Find the department that has maximum average salary.

SQL> select max(avg(sal)) from emp group by deptno;

MAX(AVG(SAL))
-------------
SHASHIKIRAN V., ASSISTANT PROFESSOR (SENIOR), SITE, VIT VELLORE.
2916.66667

30. Find the employees who get salary greater than the salary of the employee whose number is
7566.

SQL> select ename from emp where sal>(select sal from emp where empno=7566);
ENAME
----------
SCOTT
KING
FORD

31. Find the employees who have same job as employee numbered 7369 and having salary
same as the salary of the employee numbered 7876 .

SQL> select ename, job from emp where job=(select job from emp where empno=7369) and
sal>(select sal from emp where empno=7876);
ENAME JOB
---------- ---------
MILLER CLERK

32. Display the department numbers of employees whose salary is less than the salary of any
employee in department no 30 .

SQL> select deptno from emp where sal < any(select sal from emp where deptno=30);

DEPTNO
----------
20
30
30
30
10
30
20
30
10

33. Display the names and salaries of employees whose salary is less than the salary of any
clerk and whose job is not clerk.

SQL> select ename,sal from emp where sal<any(select sal from emp where job like 'CLERK') and
job!= 'CLERK';
ENAME SAL
---------- ----------
WARD 1250
MARTIN 1250

34. Display the employee names whose salary is greater than the average salary of all the
departments.

SQL> select ename,sal from emp where sal > all(select avg(sal) from emp group by deptno);
SHASHIKIRAN V., ASSISTANT PROFESSOR (SENIOR), SITE, VIT VELLORE.
ENAME SAL
---------- ----------
JONES 2975
SCOTT 3000
KING 5000
FORD 3000

35. Display the name, salary and the department of employees who get salary greater than the
average salary of their respective department.

SQL> select d.ename,d.sal,d.deptno from emp e,emp d where


2 e.deptno=d.deptno group by d.deptno,d.sal,d.ename,e.deptno
3 having d.sal > avg(e.sal);
ENAME SAL DEPTNO
---------- ---------- ----------
KING 5000 10
FORD 3000 20
SCOTT 3000 20
JONES 2975 20
ALLEN 1600 30
BLAKE 2850 30

36. Display the names of all employees and their hire date who work in the same department as
Blake.

SQL> select ename,hiredate from emp where deptno = (select deptno from emp where ename like
'BLAKE');
ENAME HIREDATE
---------- ---------
ALLEN 20-FEB-81
WARD 22-FEB-81
MARTIN 28-SEP-81
BLAKE 01-MAY-81
TURNER 08-SEP-81
JAMES 03-DEC-81

37. Display the employee number and names of all employees who earn more than the average
salary .Sort the results in descending order of salary.

SQL> select ename,empno,sal from emp where sal > (select avg(sal) from emp) order by sal desc;
ENAME EMPNO SAL
---------- ---------- ----------
KING 7839 5000
SCOTT 7788 3000
FORD 7902 3000
JONES 7566 2975
BLAKE 7698 2850
CLARK 7782 2450

6 rows selected.

SHASHIKIRAN V., ASSISTANT PROFESSOR (SENIOR), SITE, VIT VELLORE.


38. Display the employee number and name for all the employees who work in a department
with any employee whose name contains a T.

SQL> select empno,ename from emp where deptno in(select deptno from emp where ename like
'%T%');

EMPNO ENAME
---------- ----------
7369 SMITH
7876 ADAMS
7902 FORD
7788 SCOTT
7566 JONES
7499 ALLEN
7698 BLAKE
7654 MARTIN
7900 JAMES
7844 TURNER
7521 WARD

11 rows selected.

39. Display the employee name, department number and job title for all employees whose
department location is Dallas.

SQL> select ename,deptno,job from emp where deptno = (select deptno from dept where loc =
'DALLAS');
ENAME DEPTNO JOB
---------- ---------- ---------
SMITH 20 CLERK
JONES 20 MANAGER
SCOTT 20 ANALYST
ADAMS 20 CLERK
FORD 20 ANALYST

40. Display the employee name and salary of all employees who report to King.

SQL> select ename,sal from emp where mgr=(select empno from emp where ename like 'KING');
ENAME SAL
---------- ----------
JONES 2975
BLAKE 2850
CLARK 2450

41. Display the employee number, name and salary for all employees who earn more than the
average salary and who work in a department with any employee with a ‘T’ in their name.

SQL> select empno,ename,sal from emp where sal>(select avg(sal) from emp )and
2 deptno in (select deptno from emp where ename like '%T%');

EMPNO ENAME SAL


---------- ---------- ----------
7902 FORD 3000
7788 SCOTT 3000
SHASHIKIRAN V., ASSISTANT PROFESSOR (SENIOR), SITE, VIT VELLORE.
7566 JONES 2975
7698 BLAKE 2850

42. Find Nth maximum salary of any employee using employee table.

SQL> select fname,minit,lname,salary from employee order by salary desc;


FNAME M LNAME SALARY
--------------- - --------------- ----------
James E Borg 55000
Jennifer S Wallace 43000
Franklin T Wong 40000
Ramesh K Narayan 38000
John B Smith 30000
Alicia J Zelaya 25000
Joyce A English 25000
Ahmad V Jabbar 25000

8 rows selected.

SQL> select e1.fname,e1.minit,e1.lname,e1.salary from employee e1 where (&n-1) =


2 (select count(distinct(e2.salary)) from employee e2 where e2.salary > e1.salary);

Enter value for n: 1


old 1: select e1.fname,e1.minit,e1.lname,e1.salary from employee e1 where (&n-1) =
new 1: select e1.fname,e1.minit,e1.lname,e1.salary from employee e1 where (1-1) =

FNAME M LNAME SALARY


--------------- - --------------- ----------
James E Borg 55000

SQL> /
Enter value for n: 2
old 1: select e1.fname,e1.minit,e1.lname,e1.salary from employee e1 where (&n-1) =
new 1: select e1.fname,e1.minit,e1.lname,e1.salary from employee e1 where (2-1) =

FNAME M LNAME SALARY


--------------- - --------------- ----------
Jennifer S Wallace 43000

SQL> /
Enter value for n: 6
old 1: select e1.fname,e1.minit,e1.lname,e1.salary from employee e1 where (&n-1) =
new 1: select e1.fname,e1.minit,e1.lname,e1.salary from employee e1 where (6-1) =

FNAME M LNAME SALARY


--------------- - --------------- ----------
Alicia J Zelaya 25000
Joyce A English 25000
Ahmad V Jabbar 25000

SQL> /
Enter value for n: 7
old 1: select e1.fname,e1.minit,e1.lname,e1.salary from employee e1 where (&n-1) =
new 1: select e1.fname,e1.minit,e1.lname,e1.salary from employee e1 where (7-1) =

no rows selected

43. Display the department number, name and job for all employees in the SALES department
SHASHIKIRAN V., ASSISTANT PROFESSOR (SENIOR), SITE, VIT VELLORE.
SQL> select e.deptno,ename,job from emp e,dept d where e.deptno=d.deptno and dname='SALES';
DEPTNO ENAME JOB
---------- ---------- ---------
30 ALLEN SALESMAN
30 BLAKE MANAGER
30 MARTIN SALESMAN
30 JAMES CLERK
30 TURNER SALESMAN
30 WARD SALESMAN

44. Display the name, department number and salary of any employee whose department
number and salary matches both the department number and salary of any employee who
earns a commission .

SQL> select ename,deptno,sal from emp where (deptno,sal) in (select deptno,sal from emp where
comm is not NULL);

ENAME DEPTNO SAL


---------- ---------- ----------
WARD 30 1250
MARTIN 30 1250
TURNER 30 1500
ALLEN 30 1600

45. Display the name, department name and salary of any employee whose salary and
commission matches both the salary and commission of any employee located in DALLAS.

SQL> select e1.ename,d1.dname,e1.sal from emp e1,dept d1 where e1.deptno=d1.deptno and


(e1.sal,nvl(e1.comm,0))
2 = any (select e2.sal,nvl(e2.comm,0) from emp e2,dept d2 where e2.deptno=d2.deptno and
d2.loc='DALLAS');

ENAME DNAME SAL


---------- -------------- ----------
SMITH RESEARCH 800
ADAMS RESEARCH 1100
JONES RESEARCH 2975
FORD RESEARCH 3000
SCOTT RESEARCH 3000

46. Display the name, hire-date and salary for all employees who have both the same salary
and commission as SCOTT.

SQL> select ename,hiredate,sal from emp where (sal,nvl(comm,0))


2 in (select sal,nvl(comm,0) from emp where ename='SCOTT') and ename!='SCOTT';
ENAME HIREDATE SAL
---------- --------- ----------
FORD 03-DEC-81 3000

SHASHIKIRAN V., ASSISTANT PROFESSOR (SENIOR), SITE, VIT VELLORE.


47. Display the employees who earn a salary that is higher than the salary of any of the
CLERKS .Sort the results on salary from highest to lowest.

SQL> select ename,job,sal from emp where sal


2 >all(select sal from emp where job='CLERK') order by sal desc;

ENAME JOB SAL


---------- --------- ----------
KING PRESIDENT 5000
SCOTT ANALYST 3000
FORD ANALYST 3000
JONES MANAGER 2975
BLAKE MANAGER 2850
CLARK MANAGER 2450
ALLEN SALESMAN 1600
TURNER SALESMAN 1500

8 rows selected.

48. Display the number of employees working in department number 30.

SQL> select count(*) from emp where deptno = 30;

COUNT(*)
----------
6

49. Display the average commission for all employees.

SQL> select avg(comm) from emp;

AVG(COMM)
----------
550

50. Display the department number, job and sum of salary for each job grouped by
department number.

SQL> select deptno,job,sum(sal) from emp group by deptno,job;

DEPTNO JOB SUM(SAL)


---------- --------- ----------
10 CLERK 1300
10 MANAGER 2450
10 PRESIDENT 5000
20 CLERK 1900
20 ANALYST 6000
20 MANAGER 2975
30 CLERK 950
30 MANAGER 2850
30 SALESMAN 5600

SHASHIKIRAN V., ASSISTANT PROFESSOR (SENIOR), SITE, VIT VELLORE.


51. Display the title and monthly salary for every job whose payroll exceeds $5000 .

SQL> select job,sum(sal) payroll from emp where job not like 'SALES%'
2 group by job having sum(sal)>5000 order by sum(sal);

JOB PAYROLL
--------- ----------
ANALYST 6000
MANAGER 8275

52. Display the employee name ,job title and salary of employees whose salary is equal to the
minimum salary .

SQL> select ename,job,sal from emp where sal = (select min(sal) from emp);

ENAME JOB SAL


---------- --------- ----------
SMITH CLERK 800

53. Display all departments that have a minimum salary greater than that of department
whose department number is 20 .

SQL> select deptno,min(sal) from emp group by deptno having min(sal) > (select min(sal) from emp
where deptno = 20);

DEPTNO MIN(SAL)
---------- ----------
10 1300
30 950

54. Display the name, department number, salary and commission of any employee whose
salary and commission matches both the commission and salary of any employee in
department number 30.

SQL> select ename,deptno,sal,comm from emp where (sal,nvl(comm,0)) in (select sal,nvl(comm,0)


from emp where deptno = 30);

ENAME DEPTNO SAL COMM


---------- ---------- ---------- ----------
JAMES 30 950
WARD 30 1250 500
MARTIN 30 1250 1400
TURNER 30 1500 0
ALLEN 30 1600 300
BLAKE 30 2850

6 rows selected.

SHASHIKIRAN V., ASSISTANT PROFESSOR (SENIOR), SITE, VIT VELLORE.

You might also like