Realizar las siguientes SELECT mediante JOINS y SUBCONSULTAS, utilizando las tablas
TEMPLE, TDEPTO y TCENTR.
1.
2. Para los departamentos cuyo presupuesto anual supera los seis millones de Ptas., hallar
cuntos empleados hay en promedio por cada extensin telefnica.
select numde, count(*)/count(distinct extel)
from temple
where numde in
(select distinct numde
from tdepto
where presu>6)
group by numde
order by 1;
select e.numde, count(*)/count(distinct extel)
from temple e, tdepto d
where e.numde=d.numde
and presu >6
group by e.numde
order by 1;
NUMDE COUNT(*)/COUNT(DISTINCTEXTEL)
--------- ----------------------------100
1
110
1
111
2
112
1,4
3. Obtener por orden alfabtico los nombres de empleados cuyo apellido empieza por G y
trabajan en un departamento ubicado en algn centro de trabajo de la calle Alcal.
select nomem
from temple
where nomem liKe 'G%' and numde in
(select numde
from tdepto
where numce in
(select distinct numce
from tcentr
where domi like '%ALCALA%'))
order by nomem;
select nomem
from temple e, tdepto d, tcentr c
where e.numde=d.numde and d.numce=c.numce and
nomem like 'G%' and domi like '%ALCALA%'
order by nomem;
NOMEM
-------------------GALVEZ, PILAR
GARCIA, AUGUSTO
GIL, GLORIA
4. Hallar por orden alfabtico, los nombres de los empleados que son directores en
funciones.
select nomem
from temple
where numem in
(select direc
from tdepto
where tidir='F')
order by nomem;
select nomem
from tdepto d, temple e
where d.direc=e.numem and tidir = 'F'
order by nomem;
NOMEM
-------------------PEREZ, JULIO
PEREZ, MARCOS
6. Obtener por orden alfabtico, todos los datos de los centros de trabajo en los que hay
algn departamento cuyo director lo sea en funciones.
select *
from tcentr
where exists
(select *
from tdepto
where numce=tcentr.numce and tidir ='F')
order by nomce;
select distinct c.*
from tdepto d, tcentr c
where c.numce =d.numce and tidir = 'F'
order by 1;
NUMCE NOMCE
DOMI
-------- ------------------------- ------------------------10 SEDE CENTRAL
C. ALCALA, 820, MADRID
20 RELACIN CON CLIENTES
C. ATOCHA, 405, MADRID
Hallar por orden alfabtico los nombres de los empleados cuyo Director del
departamento es Marcos Prez, o bien en propiedad o bien en funciones, indicando cual
es el caso para cada uno de ellos.
select e1.nomem, tidir
from temple e1, tdepto d, temple e2
where e1.numde=d.numde and
direc=e2.numem and
e2.nomem='PEREZ, MARCOS'
order by 1;
NOMEM
-------------------AGUIRRE, AUREO
CAMPOS, ROMULO
DURAN, LIVIA
LARA, DORINDA
LARA, LUCRECIA
MORAN, CARMEN
PEREZ, MARCOS
PINO, DIANA
SANTOS, SANCHO
SANZ, LAVINIA
VAZQUEZ, HONORIA
T
F
P
F
F
F
P
P
F
F
F
F
11 filas seleccionadas.
8.
Para cada empleado que es director, hallar por orden alfabtico su nombre y la suma
de los salarios de los empleados que estn directamente a su cargo (o sea en los
departamentos que l dirige), en dos grupos separados segn sea en funciones o
propiedad.
select e2.nomem, tidir, sum(e1.salar)
from temple e1, tdepto d, temple e2
where e1.numde=d.numde and
direc=e2.numem
group by direc, tidir, e2.nomem
order by 1;
NOMEM
-------------------CAMPS, AURELIO
GARCIA, AUGUSTO
GARCIA, OCTAVIO
LOPEZ, ANTONIO
PEREZ, JULIO
PEREZ, JULIO
PEREZ, MARCOS
PEREZ, MARCOS
T SUM(E1.SALAR)
- ------------P
1620
P
1110
P
1870
P
1550
F
270
P
1240
F
1745
P
895
8 filas seleccionadas.
9.
Hallar, por orden alfabtico, los nombres de los empleados que dirigen departamentos
de los que dependen otros departamentos, indicando cuntos empleados hay en total en
stos.
select dir.nomem, count(*)
from tdepto d, tdepto dep, temple e, temple dir
where dep.depde=d.numde and
e.numde=dep.numde and
d.direc=dir.numem
group by dir.numem, dir.nomem
order by 1;
NOMEM
COUNT(*)
-------------------- --------LOPEZ, ANTONIO
7
PEREZ, JULIO
9
PEREZ, MARCOS
15
11. Para los empleados que trabajan en la calle Atocha y comparten su extensin telefnica
con otro con menor salario que ellos, hallar la suma de sus salarios por departamento y
el nombre de ste, por orden alfabtico.
select d.nomde, sum(e.salar)
from temple e, tdepto d, tcentr c
where e.numde=d.numde and
d.numce=c.numce and
domi like '%ATOCHA%' and
exists (select *
from temple
where extel=e.extel and salar<e.salar)
group by d.nomde
order by 1;
NOMDE
SUM(E.SALAR)
-------------------- -----------SECTOR INDUSTRIAL
715
SECTOR SERVICIOS
590
12. Hallar, por orden alfabtico, los nombres de los empleados que disfrutan de una
extensin telefnica no compartida con otros. Hallar tambin para cada uno de ellos su
salario y el salario medio de su departamento, excluyndoles a ellos.
select e1.nomem, e1.salar, avg(e2.salar)
from temple e1, temple e2
where e1.numde=e2.numde and
e1.numem<>e2.numem and
not exists (select *
from temple
where extel=e1.extel and numem<>e1.numem)
group by e1.numem, e1.nomem, e1.salar
order by 1;
NOMEM
SALAR AVG(E2.SALAR)
-------------------- --------- ------------AGUIRRE, AUREO
310
205
ALBA, ADRIANA
450
550
CAMPOS, ROMULO
200
347,5
CAMPS, AURELIO
450
292,5
FIERRO, CLAUDIA
400
355
FLOR, DOROTEA
290
410
GALVEZ, PILAR
380
585
GARCIA, AUGUSTO
420
345
LOPEZ, ANTONIO
720
415
MARTIN, MICAELA
180
281,66667
MORA, VALERIANA
210
352,5
MORAN, CARMEN
215
340
MUOZ, AZUCENA
175
361,25
PEREZ, MARCOS
480
207,5
RUIZ, FABIOLA
190
350
TEROL, LUCIANO
290
263,33333
16 filas seleccionadas.
NOMEM
-------------------AGUIRRE, AUREO
ALBA, ADRIANA
CAMPS, AURELIO
DIEZ, AMELIA
GALVEZ, PILAR
GARCIA, OCTAVIO
LASA, MARIO
LOPEZ, ANTONIO
MARTIN, MICAELA
PEREZ, SABINA
POLO, OTILIA
SANZ, CORNELIO
SANZ, LAVINIA
TEROL, LUCIANO
TORRES, HORACIO
15 filas seleccionadas.
14. Hallar por orden alfabtico, los nombres y salarios de los empleados cuyo salario
supera a la media de los salarios de los que ingresaron 2 aos antes o despus que l,
excluyndole.
select nomem, salar
from temple e
where salar > (select avg (salar)
from temple
where fecin between (e.fecin - 730) and
(e.fecin + 730)
and numem<>e.numem)
order by nomem;
NOMEM
SALAR
-------------------- --------ALBA, ADRIANA
450
CAMPOS, ROMULO
200
CAMPS, AURELIO
450
FIERRO, CLAUDIA
400
GARCIA, AUGUSTO
420
LOPEZ, ANTONIO
720
MORA, VALERIANA
210
MORAN, CARMEN
215
8 filas seleccionadas.
15. Hallar, por orden alfabtico, los nombres de los departamentos en los que hay algn
empleado que cumpla este ao mas de 50 aos de edad.
select d.nomde
from tdepto d
where exists (select *
from temple
where numde = d.numde and
((sysdate - fecna)/365) >= 50 )
order by numde;
NOMDE
-------------------DIRECCIN GENERAL
DIRECCIN COMERCIAL
SECTOR INDUSTRIAL
SECTOR SERVICIOS
PERSONAL
16. Hallar el nmero de departamento y el salario mximo para los departamentos cuyo
salario mximo es menor que el salario medio de los empleados de todos los dems
departamentos.
select numde, max(salar)
from temple e
group by numde
having max(salar)<(select avg(salar)
from temple
where not numde = e.numde)
order by numde;
NUMDE MAX(SALAR)
--------- ---------111
310
120
270