You are on page 1of 11

17BI2022 – Database Systems and Administration Lab URK17BI019

Ex no: 4 SUB QUERIES


Date 28-01-2020

Aim :

To execute commands making use of SUB QUERIES on table and retrieving outputs.

Description :

• The subquery (inner query) executes once before the main query.

• The result of the subquery is used by the main query (outer query).

Guidelines for using subqueries:-

-Enclose subqueries in parentheses.

-Place subqueries on the right side of the comparison condition.

-Use single-row operators with single-row subqueries and use multiple-row operators with

multiple-row subqueries.

Ex. No. 4 | SUB QUERIES 1


17BI2022 – Database Systems and Administration Lab URK17BI019

QUERIES AND OUTPUT:


QUES1: List the name of the employees working in sales department
QUERY: select Ename from EXP4_19 where fname in
(
select fname from EXP4_19 where dept='sales'
);
OUTPUT:

QUES2: Who has a salary greater then Jones?


QUERY: select fname,jobid,dept from EXP4_19 where salary >
(
select salary from EXP4_19 where fname='jones'
);
OUTPUT:

Ex. No. 4 | SUB QUERIES 2


17BI2022 – Database Systems and Administration Lab URK17BI019

QUES3: List the employees who are earning less than the average salary of accounting department

QUERY: select fname,dept,salary from EXP4_19 where salary <


(
select avg(salary) from EXP4_19 where dept='acc'
)
and dept='acc';
OUTPUT:

Ex. No. 4 | SUB QUERIES 3


17BI2022 – Database Systems and Administration Lab URK17BI019

QUES4: List the names of the employees who earns more than some employees in sales
QUERY: select fname,dept,salary from EXP4_19 where salary >
(
select avg(salary) from EXP4_19 where dept='sales'
)
and dept='sales';
OUTPUT:

QUES5: List the name of salesman who earns more than all employees of job clerk
QUERY: select fname,dept,salary from EXP4_19 where salary =
(
select max(salary) from EXP4_19 where dept='clerk'
)
order by fname ;
OUTPUT:

Ex. No. 4 | SUB QUERIES 4


17BI2022 – Database Systems and Administration Lab URK17BI019

QUES6: List the name, job, salary of employees who earn more than the average salary of all departments

QUERY:select fname,dept,salary from EXP4_19 where salary >


(
select avg(salary) from EXP4_19
)
order by fname ;
OUTPUT:

Ex. No. 4 | SUB QUERIES 5


17BI2022 – Database Systems and Administration Lab URK17BI019

QUES7: Name the departments which have minimum number of employee.


QUERY:select dept,count(*) as "Total Emp" from EXP4_19 where dept = all
(
select min(dept) from EXP4_19 where jobid >
(
select min(count(dept)) from EXP4_19 group by dept
)
)
group by dept ;
OUTPUT:

Ex. No. 4 | SUB QUERIES 6


17BI2022 – Database Systems and Administration Lab URK17BI019

QUES8: Name the departments which has max no. of employees


QUERY: select dept,count(*) as "Total Emp" from EXP4_19 where dept = all
(
select max(dept) from EXP4_19 where jobid >
(
select max(count(dept)) from EXP4_19 group by dept
)
)
group by dept ;
OUTPUT:

QUES9: Find which job id is more in all other departments

QUERY: select distinct count(jobid) as "Jobid #",max(count(jobid)) as "Total


count" from EXP4_19 where jobid = all
(
select jobid from EXP4_19 group by jobid having count(distinct dept)
>jobid
)
group by jobid;
OUTPUT:

QUES10: Write a query to display the employee name and hire date for all employees in the same department as
blake exclude blake.
QUERY: select fname,dept,hiredate from EXP4_19 where dept = all

Ex. No. 4 | SUB QUERIES 7


17BI2022 – Database Systems and Administration Lab URK17BI019

(
select dept from EXP4_19 where fname = 'don'
)
and fname <> 'don' ;
OUTPUT:

QUES11: Write a query to display the employee member and name for all employees who
work in a department with any employee whose name contains t.
QUERY: select fname as "Name",jobid as "Emp #",dept from EXP4_19 where dept in
(
select dept from EXP4_19 where fname like '%t%'
)
order by dept ;
OUTPUT:

Ex. No. 4 | SUB QUERIES 8


17BI2022 – Database Systems and Administration Lab URK17BI019

QUES12: Find avg salary of employees of those department where average salary >5000
QUERY: select fname,jobid,dept,avg(salary) from EXP4_19 where salary > any
(
select avg(salary) from EXP4_19 group by dept having avg(salary) > 5000
)
group by fname,jobid,dept having avg(salary) > 5000 order by dept ;
OUTPUT:

Ex. No. 4 | SUB QUERIES 9


17BI2022 – Database Systems and Administration Lab URK17BI019

QUES13: Find the sum of salary of employees of those department where salary >5000.

QUERY: select fname,jobid,dept,sum(salary) from EXP4_19 where salary > any


(
select sum(salary) from EXP4_19 group by dept having sum(salary) > 5000
)
group by fname,jobid,dept having sum(salary) > 5000 order by sum(salary);
OUTPUT:

Ex. No. 4 | SUB QUERIES 10


17BI2022 – Database Systems and Administration Lab URK17BI019

Result:
The SUB QUERIES were successfully implemented and the required queries were performed on the
Database.

Ex. No. 4 | SUB QUERIES 11

You might also like