Академический Документы
Профессиональный Документы
Культура Документы
FA C U LTA D D E I N G E N I E R I A Y A R Q U I T E C T U R A Ciclo
Objetivos
Que el estudiante sea capaz de:
Realizar las operaciones bsicas de consulta en SQL sobre Oracle utilizando una sola tabla.
Comprender los conceptos fundamentales referentes a consultas en SQL.
Utilizar funciones para manejo de valores nulos, cadenas de caracteres, numricas, de
fecha, de conversin y otras.
Utilizar las formas de consulta con totalizacin y funciones de grupo.
Introduccin
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
1
Universidad de El Salvador BAD-115 Gua de Laboratorio #4
Desarrollo.
Script: select ename, sal, comm, (sal + comm) as neto from emp;
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
2
Universidad de El Salvador BAD-115 Gua de Laboratorio #4
Funcin NVL
Funcin NVL2
Remplaza un valor nulo por otro valor, si no es nulo tambin lo remplaza por otro valor
diferente.
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
3
Universidad de El Salvador BAD-115 Gua de Laboratorio #4
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
4
Universidad de El Salvador BAD-115 Gua de Laboratorio #4
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
5
Universidad de El Salvador BAD-115 Gua de Laboratorio #4
Script: select initcap( first_name || ' ' || last_name) from employees where department_i d = 30;
Funciones Numricas
Funcin Descripcin Ejemplo
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
6
Universidad de El Salvador BAD-115 Gua de Laboratorio #4
Trunc(456.678,2) = 456.67
TRUNC Trunca un nmero.
Trunc(456.678,-1) = 450
c) Desplegar el nombre y el apellido separados con un espacio en blanco y el salario diario sin
decimales de los empleados del departamento nmero 30 .
Script: select initcap( first_name || ' ' || last_name), trunc(salary/30) from employees where
department_id = 30;
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
7
Universidad de El Salvador BAD-115 Gua de Laboratorio #4
Funciones de Fecha
Establecer el Formato de Fecha
Add_Months
d) Con el formato establecido muestre el dia de hoy y cual ser la fecha dentro de 3 meses.
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
8
Universidad de El Salvador BAD-115 Gua de Laboratorio #4
Current_Date
Current_Timestamp
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
9
Universidad de El Salvador BAD-115 Gua de Laboratorio #4
Extract
Extraer el mes:
Script: select sysdate as Hoy, extract(month from sysdate) as Mes2 from dual;
Extraer el dia:
Script: select sysdate as Hoy, extract(day from sysdate) as Da2 from dual;
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
10
Universidad de El Salvador BAD-115 Gua de Laboratorio #4
Last_Day
Month_Between
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
11
Universidad de El Salvador BAD-115 Gua de Laboratorio #4
SysDate
Funciones de Conversin
Cast
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
12
Universidad de El Salvador BAD-115 Gua de Laboratorio #4
To_Char
Convierte un dato tipo fecha nmero a una cadena con un formato especifico.
Script: select to_char(sysdate, 'Day, Month YYYY',' NLS_DATE_LANGUAGE=S panish') from dual;
Script: Select to_char(sysdate, 'Day, DD "de" MONTH "de" YYYY') from dual;
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
13
Universidad de El Salvador BAD-115 Gua de Laboratorio #4
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
14
Universidad de El Salvador BAD-115 Gua de Laboratorio #4
To_Date
To_Number
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
15
Universidad de El Salvador BAD-115 Gua de Laboratorio #4
Otras Funciones
NULLIF
Compara dos expresiones expr1 y expr2, si ambas son iguales retorna NULL, de lo
contrario retorna exp1. expr1 no puede ser el literal NULL.
Script: select ename, mgr, comm, NULLIF(comm,0) test1, NULLIF(0, comm) test2,
NULLIF(mgr,comm) test3 from emp where empno in (7844,7839, 7654, 7369);
Sys_Connect_By_Path
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
16
Universidad de El Salvador BAD-115 Gua de Laboratorio #4
Sys_Context
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
17
Universidad de El Salvador BAD-115 Gua de Laboratorio #4
UID
User
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
18
Universidad de El Salvador BAD-115 Gua de Laboratorio #4
COUNT
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
19
Universidad de El Salvador BAD-115 Gua de Laboratorio #4
MAX
MIN
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
20
Universidad de El Salvador BAD-115 Gua de Laboratorio #4
SUM
GROUP BY
Se utiliza para agrupar data en base a una ms columnas, para aplicar funciones de
grupo.
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
21
Universidad de El Salvador BAD-115 Gua de Laboratorio #4
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
22
Universidad de El Salvador BAD-115 Gua de Laboratorio #4
Script: select extract(year from hire_date) as ao, count(*) as empleados from employees group by
extract(year from hire_date);
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
23
Universidad de El Salvador BAD-115 Gua de Laboratorio #4
HAVING
Permite limitar mediante una condicin de grupo el resultado obtenido despus de aplicar
GROUP BY, tal como se aprecia en el siguiente grfico.
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
24
Universidad de El Salvador BAD-115 Gua de Laboratorio #4
Mostrar los puestos de trabajo de los que solo hay un empleado en la empresa.
Script: select job_id as Puesto, count(*) as Empleados from employees group by job_id having
count(*) = 1;
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
25