Luis Fernando Mendez Barrera
Grupo:SI-302
Asignatura: Base de Datos ll
Ejercicios: Practica l
Universidad Tecnológica Fidel Velázquez
Ejercicio 1
SQL> select e.ename"Employee",e.hiredate"EMPHiredate",j.ename"Manager",j.hireda
te"Mgr Hiredate"
2 from emp e,emp j
3 where e.mgr=j.empno and e.hiredate<j.hiredate;
Ejercicio 2
SQL> select e.ename,de.dname,j.ename"jefe",dj.dname
2 from emp e,emp j,dept de,dept dj
3 where e.mgr=j.empno
4 and e.deptno=de.deptno
5 and j.deptno=dj.deptno;
Ejercicio 3
SQL> select ename,job,dname,sal,grade
2 from emp,salgrade,dept
3 where sal between losal and hisal
4 and dept.deptno=emp.deptno;
Ejercicio 4
SQL> select e.ename,j.ename"jefe",e.job,de.dname,e.sal,grade
2 from emp e,emp j,dept de,salgrade
3 where e.mgr=j.empno(+)
4 and e.deptno=de.deptno
5 and e.sal between losal and hisal
6 and e.ename like '%N%';
Ejercicio 5
SQL> select count(count(mgr))as"Number of Manager"
2 from emp
3 having(mgr)>0
4 group by mgr;
Ejercicio 6
SQL> select dept.deptno,dept.dname,dept.loc,count(emp.empno)
2 from emp
3 full outer join dept on dept.deptno=emp.deptno
4 group by dept.deptno,dept.dname,dept.loc;
Ejercicio 7
SQL> select emp.job as "JOB",count(emp.job) as "Frecuency"
2 from emp,dept
3 where dept.deptno=emp.deptno
4 and dept.dname in ('RESEARCH','SALES')
5 group by emp.job
6 order by count(emp.job) desc;
Ejercicio 8
SQL> select emp.empno,emp.ename,dept.deptno,avg(emp.sal)
2 from emp
3 inner join dept on dept.deptno=emp.deptno
4 group by emp.empno,emp.ename,dept.deptno;
Ejercicio 9
SQL> select mgr,min(sal)
2 from emp
3 where MGR is not null
4 having min(sal) > 1000
5 group by mgr
6 order by min(sal) desc;
Ejercicio 10
SQL> select empno,ename
2 from emp
3 where deptno in(select deptno from emp where ename like'%T%');
Ejercicio 11
SQL> select emp.ename,dept.deptno,emp.sal
2 from emp,dept
3 where emp.deptno in
4 (select b.deptno from emp c inner join dept b on b.deptno= c.comm is not null and
emp.deptno=dept.deptno)
5 and emp.sal in
6 (select a.sal from emp a where a.comm is not null);
Ejercicio 12
SQL> select a.deptno,min(a.sal)
2 from emp a
3 having max(a.sal)=(select max(avg(b.sal)) from emp b group by b.sal)
4 group by a.deptno;
Ejercicio 13
SQL> select b.*
2 from dept b
3 where b.deptno not in(select a.deptno from emp a where a.job='SALESMAN');
Ejercicio 14
SQL> select emp.empno,emp.ename,emp.sal
2 from emp
3 inner join dept on dept.deptno=emp.deptno
4 where dept.dname in (select a.dname from dept a inner join emp b on b.deptn
o=a.deptno where b.ename like '%T%')
5 and emp.sal in
6 (select emp.sal from emp where emp.sal>(select AVG(emp.sal) from emp));
Ejercicio 15
Mapeo
Tabla: DEPARTAMENTO
columna Numero Nombre Localización
Tipo de llave PK
Nulos/único NN, U NN NN
FK tabla referencia
FK columna referencia
Tipo de dato NUMBER(4) VARCHAR(2) VARCHAR(2)
Tabla: EMPLEADO
columna Clave Nombre Numero
Tipo de llave PK PK,FK
Nulos/único NN, U NN NN,U
FK tabla referencia DEPARTAMENTO
FK columna referencia Numero
Tipo de dato NUMBER(4) VARCHAR(2) NUMBER(4)
Ejercicio 16,17,18
SQL> CREATE TABLE Departamento(
2 numero number(4) not null primary key,
3 nombre varchar2(250) not null,
4 localizacion varchar2(250) not null);
Tabla creada.
SQL> insert into Departamento
2 values(10,'VENTAS','QUERETARO')
3 ;
1 fila creada.
SQL> insert into Departamento
2 values(20,'SISTEMAS','TLAXCALA');
1 fila creada.
SQL> insert into Departamento
2 values(30,'SALUD','VERACRUZ');
1 fila creada.
SQL> insert into Departamento
2 values(40,'CONTABILIDAD','CAMPECHE');
1 fila creada.
SQL> insert into Departamento
2 values(50,'REDES','OAXACA');
1 fila creada.
SQL> select numero,nombre,localizacion
2 from Departamento;