12.07.2015 Views

Subconsultas Multi-Columna

Subconsultas Multi-Columna

Subconsultas Multi-Columna

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

7<strong>Subconsultas</strong> <strong>Multi</strong>-<strong>Columna</strong>Copyright © Oracle Corporation, 1997. All rights reserved.


ObjetivosAl completar esta lección, debería a sercapaz de hacer lo siguiente:• Escribir una subconsulta multi-columna• Describir y explicar el comportamientode las subconsultas cuando serecuperan valores nulos• Escribir una subconsulta en unacláusula FROM.7-2 Copyright © Oracle Corporation, 1997. All rights reserved.


<strong>Subconsultas</strong> <strong>Multi</strong>-<strong>Columna</strong>Consulta PrincipalMANAGER 10SubquerySALESMAN 30MANAGER 10CLERK 20La C. PrincipalcomparaMANAGER 10conValores de una subc. multi-registroy multi-columnaSALESMAN 30MANAGER 10CLERK 207-3 Copyright © Oracle Corporation, 1997. All rights reserved.


Usando<strong>Subconsultas</strong> <strong>Multi</strong>-<strong>Columna</strong>Visualizar el nombre, nº nde departamento,salario y comisión n de cualquier empleado cuyosalario y comisión n se correspondan (ambos)con la comisión n y salario de cualquier empleadodel departamento 30.SQL> SELECT ename, deptno, sal, comm2 FROM emp3 WHERE (sal, NVL(comm,0)) IN4 (SELECT sal, NVL(comm,0)5 FROM emp6 WHERE deptno = 30);7-4 Copyright © Oracle Corporation, 1997. All rights reserved.


Comparaciones entre <strong>Columna</strong>sPairwiseSAL COMM1600 3001250 5001250 140028501500 0950NonpairwiseSAL COMM1600 3001250 5001250 140028501500 09507-5 Copyright © Oracle Corporation, 1997. All rights reserved.


Comparación de <strong>Subconsultas</strong>“Nonpairwise”Visualizar el nombre, nº nde departamento, salario ycomisión n de cualquier empleado cuyo salario ycomisión n se corresponda con la comisión n y salariode cualquier empleado del departamento 30.SQL> SELECT ename, deptno, sal, comm2 FROM emp3 WHERE sal IN (SELECT sal4 FROM emp5 WHERE deptno = 30)6 AND7 NVL(comm,-1) IN (SELECT NVL(comm,-1)8 FROM emp9 WHERE deptno = 30);7-6 Copyright © Oracle Corporation, 1997. All rights reserved.


Modificación de la Tabla EMP• Asuma que el salario y comisión de “Clark”son modificados.• El salario se cambia a 1500$ y la comisión a300$.ENAME SAL COMM---------- --------- ---------...CLARK 1500 300...ALLEN 1600 300TURNER 1500 0...14 rows selected.7-7 Copyright © Oracle Corporation, 1997. All rights reserved.


Subconsulta “Pairwise”SQL> SELECT ename, deptno, sal, comm2 FROM emp3 WHERE (sal, NVL(comm,0)) IN4 (SELECT sal, NVL(comm,0)5 FROM emp6 WHERE deptno = 30);ENAME DEPTNO SAL COMM---------- --------- --------- ---------JAMES 30 30 950WARD 30 30 1250 500MARTIN 30 30 1250 1400TURNER 30 30 1500 0ALLEN 30 30 1600 300BLAKE 30 30 28506 rows selected.7-8 Copyright © Oracle Corporation, 1997. All rights reserved.


Valores Nulos en una SubconsultaSQL> SELECT employee.ename2 FROM emp employee3 WHERE employee.empno NOT IN(SELECT manager.mgrFROM emp manager);no rows selected.7-10 Copyright © Oracle Corporation, 1997. All rights reserved.


Uso de un Subconsulta en laCláusula FROMSQL> SELECT a.ename, a.sal, a.deptno, b.salavg2 FROM emp a, (SELECT deptno, avg(sal) salavg3 FROM emp4 GROUP BY deptno) b5 WHERE a.deptno = b.deptno6 AND a.sal > b.salavg;ENAME SAL DEPTNO SALAVG---------- --------- --------- ----------KING 5000 10 10 2916.6667JONES 2975 20 20 2175SCOTT 3000 20 20 2175...6 rows selected.7-11 Copyright © Oracle Corporation, 1997. All rights reserved.


Resumen• Una subconsulta multi-columnadevuelve más de una columna.• Las comparaciones entre columnas enuna comparación multi-columna,pueden ser “pairwise” o “nonpairwise”.• Una subconsulta multi-columna puedetambién ser usada en la cláusula FROMde una sentencia SELECT.7-12 Copyright © Oracle Corporation, 1997. All rights reserved.


Visión General de la PrácticaCreación de <strong>Subconsultas</strong> <strong>Multi</strong>-<strong>Columna</strong>7-13 Copyright © Oracle Corporation, 1997. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!