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

1.

Display employee details whose salary


greater than 5000.
select * from emp where salary >5000
Eid Ename did salary Jid
100 a 10 10000 j1
101 b 10 12000 j1
103 d 20 8000 j3
104 e 10 14000 j4

2. Display the employee details whose salary


is greater than the average salary of
department 30àunknown condition.
select * from emp
where salary > (select avg(salary) from emp
where did=30)
only once the subquery will get executed and
after that based on the subquery output , the
main query table records will be checked
Eid Ename did salary Jid
100 a 10 10000 j1
101 b 10 12000 j1
103 d 20 8000 j3
104 e 10 14000 j4

3. Display the employee details whose salary


is greater than the average salary of their
department
subquery itself is unknown
Select * from emp
where salary>(select avg(salary) from emp
where did=?)

Correlated subquery

select * from emp e1 where salary>(select


avg(salary) from emp where did=e1.did);
1. Initially main query table will supply the
first record as the input to the inner query
example did=10
2. Based on the input , the inner query will
get executed
select * from emp e1 where salary>(select
avg(salary) from emp where did=10);
output: 12000
3. This subquery output will act as the input
to the mainquery
4. After that, condition will be verified on the
mainquery record
10000>12000XXXXXXXXXXXXX
then the record will be disqualified(record will
not be displayed on the output)

Eid Ename did salary Jid


100 a 10 10000 j1
101 b 10 12000 j1
102 c 20 2000 j2
103 d 20 8000 j3
104 e 10 14000 j4
105 f 30 5000 j3
eid==100
did=10
avg(salary) of did 10 is 12000
and 10000>12000

1. Display employee details whose salary


greater than 5000.
select * from emp where salary >5000
Eid Ename did salary jid
100 a 10 10000 j1
101 b 10 12000 j1
103 d 20 8000 j3
104 e 10 14000 j4
2. Display the employee details whose salary
is greater than the average salary of
department 30àunknown condition.
select * from emp
where salary > (select avg(salary) from emp
where did=30)
only once the subquery will get executed and
after that based on the subquery output , the
main query table records will be checked

Eid Ename did salary jid


100 a 10 10000 j1
101 b 10 12000 j1
103 d 20 8000 j3
104 e 10 14000 j4
3. Display the employee details whose salary
is greater than the average salary of their
department
subquery itself is unknown
Select * from emp
where salary>(select avg(salary) from emp
where did=?)

Correlated subquery

select * from emp e1 where salary>(select


avg(salary) from emp where did=e1.did);

The following steps will be repeated for all the


record in the main query table
1. Initially main query table will supply the
first record as the input to the inner query
example did=10
2. Based on the input , the inner query will
get executed
select * from emp e1 where salary>(select
avg(salary) from emp where did=10);
output: 12000
3. This subquery output will act as the input
to the mainquery
4. After that, condition will be verified on the
mainquery record
10000>12000XXXXXXXXXXXXX
then the record will be disqualified(record will
not be displayed on the output)

Eid Ename did salary jid


100 a 10 10000 j1
101 b 10 12000 j1
102 c 20 2000 j2
103 d 20 8000 j3
104 e 10 14000 j4
105 f 30 5000 j3
1. eid==100
did=10
avg(salary) of did 10 is 12000
and 10000>12000
2. eid=101 did=10 sq opà12000
12000>12000(record disqualified)
3. eid=102 did=20 sq opà5000
2000>5000(record disqualified)
4. eid=103 did=20 sqopà5000
8000>5000(record qualified)
5. eid=104 did=10 sq op=12000
14000>12000(record qualified)
6. eid=105 did=30 sqopà5000
5000>5000(record disqualified)

You might also like