|
|
(2 revisiones intermedias por el mismo usuario no mostrado) |
Línea 113: |
Línea 113: |
| [[Archivo:dml_func_22.png]]<br /> | | [[Archivo:dml_func_22.png]]<br /> |
| <hr /> | | <hr /> |
− |
| |
− | ====Ejercicios select/from/where con funciones ====
| |
− | <br />
| |
− | {{MRM_Actividad|Title=Realiza las siguientes consultas|
| |
− |
| |
− | [[Archivo:ej1_query.png]]
| |
− | *En este caso JOB_ID nos da el tipo de trabajo
| |
− | Secretari@ es el tipo '''''ST_CLERK'''''
| |
− | <source lang=sql>
| |
− | mysql> SELECT *
| |
− | FROM EMPLOYEES
| |
− | WHERE JOB_ID ="ST_CLERK"
| |
− | AND
| |
− | HIRE_DATE>"1997-12-31";
| |
− | </source>
| |
− | <hr />
| |
− | [[Archivo:ej2_query.png]]
| |
− |
| |
− | <source lang=sql>
| |
− | SELECT LAST_NAME, JOB_ID,SALARY, COMMISSION_PCT
| |
− | FROM EMPLOYEES
| |
− | WHERE COMMISSION_PCT IS NOT NULL
| |
− | ORDER BY LAST_NAME DESC;
| |
− | </source>
| |
− |
| |
− | <hr />
| |
− | [[Archivo:ej3_query.png]]
| |
− | <source lang=sql>
| |
− | SELECT 'El salario de '||Last_name||
| |
− | 'va a ser con un incremento del 10% de '||
| |
− | round(Salary *1.10) ||
| |
− | ' dólares' as "Nuevo Salario"
| |
− | FROM EMPLOYEES
| |
− | where COMMISSION_pCT IS NULL
| |
− |
| |
− | </source>
| |
− | <hr />
| |
− | [[Archivo:ej4_query.png]]
| |
− | <source lang=sql>
| |
− | select last_name,
| |
− | trunc(months_between(sysdate, hire_date)/12) as "años",
| |
− | trunc(mod(months_between(sysdate, hire_date),12))as "meses"
| |
− | from employees
| |
− | order by hire_date;
| |
− | </source>
| |
− | <hr />
| |
− | [[Archivo:ej5.png]]
| |
− | <source lang=sql>
| |
− | select *
| |
− | from empleados
| |
− | where substr(last_name,1,1) in ('J', 'K', 'L', 'M')
| |
− |
| |
− | </source>
| |
− | <hr />
| |
− | [[Archivo:ej6.png]]
| |
− | <source lang=sql>
| |
− |
| |
− | select last_name, salary,
| |
− | decode (commission_pct,NULL, 'No' ,
| |
− | 'Yes') as "comisión"
| |
− | from employees
| |
− |
| |
− | </source>
| |
− | <hr />
| |
− | [[Archivo:ej7.png]]
| |
− | <source lang=sql>
| |
− |
| |
− |
| |
− | select department_name, location_id,job_id, salary
| |
− | from departments d, employees e
| |
− | where d.department_id = e.department_id
| |
− | and
| |
− | location_id = '&localizacion';
| |
− | </source>
| |
− | <hr />
| |
− | [[Archivo:ej8.png]]
| |
− | <source lang=sql>
| |
− | SELECT COUNT(*)
| |
− | FROM employees
| |
− | WHERE last_name LIKE '%n';
| |
− | --or
| |
− | SELECT COUNT(*)
| |
− | FROM employees
| |
− | WHERE SUBSTR(last_name, -1) = 'n';
| |
− | </source>
| |
− | <hr />
| |
− | [[Archivo:ej10.png]]
| |
− | <source lang=sql>
| |
− | SELECT DISTINCT job_id
| |
− | FROM employees
| |
− | WHERE department_id IN (10, 20);
| |
− | </source>
| |
− | <hr />
| |
− | [[Archivo:ej12.png]]
| |
− | <source lang=sql>
| |
− | SELECT last_name, hire_date
| |
− | FROM employees
| |
− | WHERE TO_CHAR(hire_date, 'DD') < 16;
| |
− | </source>
| |
− | <hr />
| |
− | [[Archivo:ej13.png]]
| |
− | <source lang=sql>
| |
− | SELECT last_name, salary, TRUNC(salary, -3)/1000
| |
− | FROM employees;
| |
− | </source>
| |
− | <hr />
| |
− | [[Archivo:ej14.png]]
| |
− | <source lang=sql>
| |
− | SELECT e.last_name, m.last_name manager, m.salary,
| |
− | j.grade_level
| |
− | FROM employees e JOIN employees m
| |
− | ON e.manager_id = m.employee_id
| |
− | JOIN job_grades j
| |
− | ON m.salary BETWEEN j.lowest_sal AND j.highest_sal
| |
− | AND m.salary > 15000;
| |
− | </source>
| |
− | <hr />
| |
− | [[Archivo:ej21.png]]
| |
− | <source lang=sql>
| |
− | SELECT last_name, TO_CHAR(hire_date, 'Month DD') BIRTHDAY
| |
− | FROM employees
| |
− | ORDER BY TO_CHAR(hire_date, 'DDD');
| |
− | </source>
| |
− | <hr />
| |
− | }}
| |
− |
| |
− | ====Ejercicios de agrupación ====
| |
− | <br />
| |
− | {{MRM_Actividad|Title=Ejercicios de agrupación|
| |
− | [[Archivo:ej9.png]]
| |
− | <source lang=sql>
| |
− | SELECT d.department_id, d.department_name, d.location_id, COUNT(e.employee_id)
| |
− | FROM employees e
| |
− | RIGHT OUTER JOIN departments d
| |
− | ON
| |
− | e.department_id = d.department_id
| |
− | GROUP BY d.department_id, d.department_name, d.location_id;
| |
− | </source>
| |
− | <hr />
| |
− | [[Archivo:ej11.png]]
| |
− | <source lang=sql>
| |
− | SELECT e.job_id, count(e.job_id) FREQUENCY
| |
− | FROM employees e
| |
− | JOIN departments d
| |
− | ON e.department_id = d.department_id
| |
− | WHERE d.department_name IN ('Administration', 'Executive')
| |
− | GROUP BY e.job_id
| |
− | ORDER BY FREQUENCY DESC;
| |
− | </source>
| |
− | <hr />
| |
− |
| |
− | [[Archivo:ej15.png]]
| |
− | <source lang=sql>
| |
− | SELECT d.department_id, d.department_name, count(e1.employee_id) employees,
| |
− | NVL(TO_CHAR(AVG(e1.salary), '99999.99'), 'No average') avg_sal,
| |
− | e2.last_name, e2.salary, e2.job_id
| |
− | FROM departments d RIGHT OUTER JOIN employees e1
| |
− | ON d.department_id = e1.department_id
| |
− | RIGHT OUTER JOIN employees e2
| |
− | ON d.department_id = e2.department_id
| |
− | GROUP BY d.department_id, d.department_name, e2.last_name,
| |
− | e2.salary,
| |
− | e2.job_id
| |
− | ORDER BY d.department_id, employees;
| |
− | </source>
| |
− | <hr />
| |
− |
| |
− | [[Archivo:ej18.png]]
| |
− | ;a) Contrate menos de tres empleados:
| |
− | <source lang=sql>
| |
− | SELECT d.department_id, d.department_name, COUNT(*)
| |
− | FROM
| |
− | departments d JOIN employees e
| |
− | ON
| |
− | d.department_id = e.department_id
| |
− | GROUP BY d.department_id, d.department_name
| |
− | HAVING COUNT(*) < 3;
| |
− | </source>
| |
− | ;b) Tenga el mayor número de empleados:
| |
− | <source lang=sql>
| |
− | SELECT d.department_id, d.department_name, COUNT(*)
| |
− | FROM
| |
− | departments d JOIN employees e
| |
− | ON
| |
− | d.department_id = e.department_id
| |
− | GROUP BY d.department_id, d.department_name
| |
− | HAVING COUNT(*) = (SELECT MAX(COUNT(*))
| |
− | FROM
| |
− | employees
| |
− | GROUP BY department_id);
| |
− | </source>
| |
− |
| |
− | ;c) Tenga el menor número de empleados:
| |
− | <source lang=sql>
| |
− | SELECT d.department_id, d.department_name, COUNT(*)
| |
− | FROM
| |
− | departments d JOIN employees e
| |
− | ON
| |
− | d.department_id = e.department_id
| |
− | GROUP BY d.department_id, d.department_name
| |
− | HAVING COUNT(*) = (SELECT MIN(COUNT(*))
| |
− | FROM
| |
− | employees
| |
− | GROUP BY department_id);
| |
− | </source>
| |
− |
| |
− | <hr />
| |
− | [[Archivo:ej19.png]]
| |
− | <source lang=sql>
| |
− | SELECT e.employee_id, e.last_name, e.department_id, e.salary,
| |
− | AVG(s.salary)
| |
− | FROM
| |
− | employees e JOIN employees s
| |
− | ON
| |
− | e.department_id = s.department_id
| |
− | GROUP BY e.employee_id, e.last_name, e.department_id,
| |
− | e.salary;
| |
− | </source>
| |
− | <hr />
| |
− | }}
| |
− |
| |
− | ====Ejercicios con subconsultas ====
| |
− | <br />
| |
− | {{MRM_Actividad|Title=Ejercicios de subconsultas
| |
− | [[Archivo:ej16.png]]
| |
− | <source lang=sql>
| |
− | SELECT department_id, MIN(salary)
| |
− | FROM employees
| |
− | GROUP BY department_id
| |
− | HAVING AVG(salary) = (SELECT MAX(AVG(salary))
| |
− | FROM employees
| |
− | GROUP BY department_id);
| |
− | </source>
| |
− | <hr />
| |
− | [[Archivo:ej17.png]]
| |
− | <source lang=sql>
| |
− | SELECT *
| |
− | FROM
| |
− | departments
| |
− | WHERE department_id NOT IN(SELECT department_id
| |
− | FROM employees
| |
− | WHERE job_id = 'SA_REP'
| |
− | AND department_id IS NOT NULL);
| |
− |
| |
− | </source>
| |
− | <hr />
| |
− | [[Archivo:ej20.png]]
| |
− | <source lang=sql>
| |
− | SELECT last_name, TO_CHAR(hire_date, 'DAY') day
| |
− | FROM employees
| |
− | WHERE TO_CHAR(hire_date, 'Day') = (SELECT TO_CHAR(hire_date, 'Day')
| |
− | FROM employees
| |
− | GROUP BY TO_CHAR(hire_date, 'Day')
| |
− | HAVING COUNT(*) = (SELECT MAX(COUNT(*))
| |
− | FROM employees
| |
− | GROUP BY TO_CHAR(hire_date,'Day')));
| |
− |
| |
− | </source>
| |
− | <hr />
| |
− | }}
| |
Esta sentencia que empieza por la claúsual SELECT va a terner hasta 5 posibles claúsulas, de las cuales solo son obligatorias las dos primeras