You are on page 1of 6

SQL> show user

USER is "HR"
SQL>
#Correlated subquery
#Pairwise comparison subquery
--People working under the same manager_id and same job_id
SELECT employee_id, manager_id, department_id
FROM employees
WHERE (manager_id, department_id) IN
(SELECT manager_id, department_id
FROM employees
WHERE first_name = 'John')
#Nonpair wise comparison subquery
select employee_id,first_name,last_name,manager_id,department_id from employees
where manager_id in
(select manager_id from employees where first_name='John')
and department_id in
(select department_id from employees where first_name='John')
order by manager_id,department_id;
Display those employee salaries who earn more than the average salary of their d
ept.
select employee_id,first_name,last_name,salary
from employees a
where salary>
(select avg(salary) from employees b where b.department_id=a.department_id)
#Exists operator
select employee_id,first_name,last_name,department_id
from employees e where exists
(select manager_id from employees where manager_id=e.employee_id)
SQL> create table emps
2 (
3
eid number,
4
ename varchar2(20),
5
did number
6 );
Table created.
SQL> desc emps
Name
Null?
----------------------------------------- -------EID
ENAME
DID
SQL> select * from emps;
no rows selected
SQL> insert into emps

Type
---------------------------NUMBER
VARCHAR2(20)
NUMBER

2 select employee_id,first_name,department_id
3 from employees;
107 rows created.
SQL> set pagesize 120
SQL> select * from emps;
EID
---------198
199
200
201
202
203
204
205
206
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140

ENAME
DID
-------------------- ---------Donald
50
Douglas
50
Jennifer
10
Michael
20
Pat
20
Susan
40
Hermann
70
Shelley
110
William
110
Steven
90
Neena
90
Lex
90
Alexander
60
Bruce
60
David
60
Valli
60
Diana
60
Nancy
100
Daniel
100
John
100
Ismael
100
Jose Manuel
100
Luis
100
Den
30
Alexander
30
Shelli
30
Sigal
30
Guy
30
Karen
30
Matthew
50
Adam
50
Payam
50
Shanta
50
Kevin
50
Julia
50
Irene
50
James
50
Steven
50
Laura
50
Mozhe
50
James
50
TJ
50
Jason
50
Michael
50
Ki
50
Hazel
50
Renske
50
Stephen
50
John
50
Joshua
50

141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197

Trenna
Curtis
Randall
Peter
John
Karen
Alberto
Gerald
Eleni
Peter
David
Peter
Christopher
Nanette
Oliver
Janette
Patrick
Allan
Lindsey
Louise
Sarath
Clara
Danielle
Mattea
David
Sundar
Amit
Lisa
Harrison
Tayler
William
Elizabeth
Sundita
Ellen
Alyssa
Jonathon
Jack
Kimberely
Charles
Winston
Jean
Martha
Girard
Nandita
Alexis
Julia
Anthony
Kelly
Jennifer
Timothy
Randall
Sarah
Britney
Samuel
Vance
Alana
Kevin

107 rows selected.

50
50
50
50
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50

SQL> alter table emps


2 add(dname varchar2(20));
Table altered.
SQL> desc emps
Name
Null?
----------------------------------------- -------EID
ENAME
DID
DNAME

Type
---------------------------NUMBER
VARCHAR2(20)
NUMBER
VARCHAR2(20)

SQL> select * from emps;


EID
---------198
199
200
201
202
203
204
205
206
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133

ENAME
DID DNAME
-------------------- ---------- -------------------Donald
50
Douglas
50
Jennifer
10
Michael
20
Pat
20
Susan
40
Hermann
70
Shelley
110
William
110
Steven
90
Neena
90
Lex
90
Alexander
60
Bruce
60
David
60
Valli
60
Diana
60
Nancy
100
Daniel
100
John
100
Ismael
100
Jose Manuel
100
Luis
100
Den
30
Alexander
30
Shelli
30
Sigal
30
Guy
30
Karen
30
Matthew
50
Adam
50
Payam
50
Shanta
50
Kevin
50
Julia
50
Irene
50
James
50
Steven
50
Laura
50
Mozhe
50
James
50
TJ
50
Jason
50

134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193

Michael
Ki
Hazel
Renske
Stephen
John
Joshua
Trenna
Curtis
Randall
Peter
John
Karen
Alberto
Gerald
Eleni
Peter
David
Peter
Christopher
Nanette
Oliver
Janette
Patrick
Allan
Lindsey
Louise
Sarath
Clara
Danielle
Mattea
David
Sundar
Amit
Lisa
Harrison
Tayler
William
Elizabeth
Sundita
Ellen
Alyssa
Jonathon
Jack
Kimberely
Charles
Winston
Jean
Martha
Girard
Nandita
Alexis
Julia
Anthony
Kelly
Jennifer
Timothy
Randall
Sarah
Britney

50
50
50
50
50
50
50
50
50
50
50
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
50
50
50
50
50
50
50
50
50
50
50
50
50
50

194
195
196
197

Samuel
Vance
Alana
Kevin

50
50
50
50

107 rows selected.


SQL> ed
Wrote file afiedt.buf
1 update emps e
2* set dname=(select department_name from departments d where d.department_id=
e.did)
SQL> /
107 rows updated.

You might also like