Professional Documents
Culture Documents
Date :
GROUP FUNCTIONS AND SUBQUERIES
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.
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
Types of 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.
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.
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.
8 rows selected.
07. Show the resulting salaries if every employee working on the ‘ProductX’ project is given a
10 percent raise.
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;
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.
10. Retrieve the name of each employee who has a dependent with the same first name and
same sex as the employee.
11. Retreive the social security numbers of all employees who works on
project number 1,2 or 3.
12. Retrieve the names of all employees who do not have supervisors.
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 count (*) from employee,department where dno=dnumber and dname='Research';
COUNT(*)
----------
4
18. For each department, retrieve the department number, the number of employees in the
department, and their average salary.
19. For each project, retrieve the projrct number, the project name, and the number of
employees who work on that project.
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.
21. For each project, retrieve the project number, the project name, and the number of
employees from department 5 who work on the project.
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.
DEPTNO AVG(SAL)
---------- ----------
10 2916.66667
20 2175
30 1566.66667
26. Find the departments having maximum salary greater than $2000.
27. Find the departments having average salary greater than $2000.
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
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.
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.
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%');
42. Find Nth maximum salary of any employee using employee table.
8 rows selected.
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) =
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) =
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);
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.
46. Display the name, hire-date and salary for all employees who have both the same salary
and commission as SCOTT.
8 rows selected.
COUNT(*)
----------
6
AVG(COMM)
----------
550
50. Display the department number, job and sum of salary for each job grouped by
department number.
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);
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.
6 rows selected.