SQL 5

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

PRACTICA TEMA 5 SQL GBD

1-
Verdadero

2-

Falso

3-

Verdadero

4-

SELECT ROUND (MAX(salary), 0)


“Maximum” , ROUND(MIN(salary), 0)
“Minimum” , ROUND(MAX(salary), 0)
ROUND (AVG(salary), 0) “Average”
FROM employees;

5-

SELECTjob_id,
ROUND(MAX(salary),0) "Maximum",
ROUND(MIN(salary),0) "Minimum",
ROUND(SUM(salary),0) "Sum",
ROUND(AVG(salary),0) "Average"
FROM employees
GROUP BY job_id;

6-

SELECTjob_id, COUNT(*)
FROM employees
GROUP BY job_id;

7-

SELECTCOUNT(DISTINCT manager_id) "Number of Managers"


FROM employees;

8-

SELECTMAX(salary) - MIN(salary)
DIFFERENCE FROM employees;
9-

SELECTmanager_id, MIN(salary)
FROM employees
WHERE manager_id IS NOT NULL
GROUP BY manager_id
HAVING MIN(salary) > 6000
ORDER BY MIN(salary) DESC;

10-

SELECT d.department_name "Name", d.location_id "Location ",


COUNT(*) "Number of People",
ROUND(AVG(salary),2) "Salary"
FROM employees e, departments d
WHERE e.department_id = d.department_id
GROUP BY d.department_name, d.location_id;

11-

SELECTCOUNT(*) total,
SUM(DECODE(TO_CHAR(hire_date, 'YYYY'),1995,1,0))"1 995",
SUM(DECODE(TO_CHAR(hire_date,'YYYY'),1996,1,0))"1 996",
SUM(DECODE(TO_CHAR(hire_date,'YYYY'),1997,1,0))"1 997",
SUM(DECODE(TO_CHAR(hire_date,'YYYY'),1998,1,0))"1 998"
FROM employees;

12-

SELECTjob_id "Job",
SUM(DECODE(department_id , 20, salary)) "Dept 20" ,
SUM(DECODE(department_id , 50, salary)) "Dept 50" ,
SUM(DECODE(department_id , 80, salary)) "Dept 80" ,
SUM(DECODE(department_id , 90, salary)) "Dept 90" , SUM(salary)
"Total"
FROM employees
GROUP BY job_id;
EJERCICIOS ADICIONALES TEMA 5 ORACLE

1. Mostrar la comisión más alta, el empleado con el último apellido (orden


alfabético) y la fecha de contratación más reciente.

SELECT max(comisión_pct),max(last_name),max(hire_date)
FROM employees;

2. Idem pero para los empleados del departamento 50.

SELECT max(comisión_pct),max(last_name),max(hire_date)
FROM employees
WHERE department_id=50;

SELECT max(comisión_pct),max(last_name),max(hire_date)
FROM employees
GROUP BY department_id=50
HAVING department_id=50

3. Mostrar la fecha más antigua de inicio de contrato y la más nueva de final de


contrato para cada departamento (departamento_id). Ordenar por
departamento.

SELECT max(hire_date), min(hire_date)


FROM employees
GROUP BY department_id
ORDER BY department_id;

4. Idem pero mostrando el nombre del departamento y ordenando por éste.

SELECT d.department_name, max(j.start_date), min(j.end_date)


FROM job_history, j departments d
GROUP BY department_name
ORDER BY department_name
5. Mostrar los nombres de los países para cada región (nombre) ordenado por
nombre de región.

SELECT c.country_name, r.region_name


FROM countries c, regions r
WHERE r.region_id= c.region_id
ORDER BY r.region_name;

SELECT c.country_name, r.region_name


FROM countries c
JOIN regiONs r
ON r.region_id= c.region_id
ORDER BY r.region_name;

6. Mostrar la media de dias de contrato (redondeado a 0 decimales) para cada


categoría laboral. Ordenar por esta última.

SELECT avg(jh.end_date-jh.start_date), j.job_title


FROM job_history jh, jobs j
WHERE jh.job_id = j.job_id
GROUP BY job_title
ORDER BY job_title;

SELECT avg(jh.end_date-jh.start_date), j.job_title


FROM job_history jh
JOIN jobs j
USING(job_id)
GROUP BY job_title
ORDER BY job_title;
7. Mostrar el nombre de cada departamento con el apellido de su jefe. Ordenar
por nombre de departamento.

SELECT d.department_name, e.last_name


FROM departments d, employees e
WHERE d.manager_id=e.employee_id(+)
ORDER BY d.department_name;

SELECT d.department_name, e.last_name


FROM departments d
LEFT OUTER JOIN employees e
ON d.manager_id=e.employee_id
ORDER BY d.department_name;

8. Idem pero mostrANDo también los departamentos sin jefe. En este caso que
diga ‘Sin jefe’ en lugar del apellido.

SELECT d.department_name, nvl(e.last_name, ‘no tiene jefe’)


FROM departments d
LEFT OUTER JOIN employees e
ON d.manager_id=e.employee_id
GROUP BY d.department_name, e.last_name
ORDER BY d.department_name;

9. Idem pero sólo para los departamentos ‘Accounting’ y ‘Benefits’.

SELECT d.department_name, nvl(e.last_name, ‘no tiene jefe’)


FROM departments d
LEFT OUTER JOIN employees e
ON d.manager_id=e.employee_id
GROUP BY d.department_name, e.last_name
HAVING d.department_name in (‘Accounting’, ‘Benefits’)
ORDER BY d.department_name;
EXERCICIS ADICIONALS TEMA 5 ORACLE (II)

1. Mostra el nom de la categoria laboral i el salari mitjà de cada empleat que té


aquesta categoria (ordena per aquesta última).

SELECT j.job_title, avg(salary)


FROM employees e jobs j
WHERE j.job_id=e.job_id
GROUP BY j.job_title
ORDER BY j.job_title;

SELECT j.job_title, avg(salary)


FROM employees e
JOIN jobs j
USING (job_id)
GROUP BY j.job_title
ORDER BY j.job_title;

SELECT j.job_title, avg(salary)


FROM employees e
JOIN jobs j
ON e.job_id=j.job_id
GROUP BY j.job_title
ORDER BY j.job_title;
2. Per a cada ciutat mostra el nombre de departaments que tenen seu en ella.

SELECT l.city, count(d.department_name)


FROM locations l, departments d
WHERE l.location_id = d.location_id
GROUP BY l.city;

SELECT l.city, count(d.department_name)


FROM locations l
JOIN departments d
ON l.location_id = d.location_id
GROUP BY l.city;

SELECT l.city, count(d.department_name)


FROM locations l
JOIN departments d
USING (location_id)
GROUP BY l.city;

3. Ídem del 2 però només de les ciutats que pertanyin als ‘United States of America’.

SELECT l.city, count(d.department_name)


FROM locations l
JOIN departments d
USING (location_id)
JOIN countries c
USING (country_id)
WHERE c.country_name = ‘United States of America’
GROUP BY l.city;
SELECT l.city, count(d.department_name)
FROM locations l, departments d, countries c
WHERE l.location_id=d.location_id
AND l.country_id=c.country_id
AND c.country_name = ‘United States of America’
GROUP BY l.city;

4. Contar quants empleats cobren el matexi salari dintre de cada departament (nom).

SELECT d.department_name, e.salary, count(*)


FROM employees e
CROSS JOIN employees e2
JOIN departments d
ON d.department_id = e.department_id
WHERE e.salary = e2.salary
GROUP BY d.department_name, e.salary;

SELECT d.department_name, e.salary, count(*)


FROM employees e, employees e2, departments d
WHERE d.department_id = e.department_id
AND e.salary = e2.salary
GROUP BY d.department_name, e.salary;
EXERCICIS ADICIONALS TEMA 5 ORACLE (III)

1. Listar cuántos empleados tienen fecha de inicio de cONtrato (JOB_HISTORY) el


año 87.

SELECT count(employee_id)
FROM job_history
WHERE start_date like ‘%/%/87’;

2. Listar el nombre de departamento y cuántos empleados tienen fecha de inicio


de contrato los años 98 y 99.

SELECT d.department_name, count(j.employee_id)


FROM departments d
JOIN job_history j
USING (department_id)
WHERE j.start_date like ‘%98’
OR j.start_date like ‘%99’
GROUP BY department_name;

3. Listar el número total de empleados y cuántos empleados tienen fecha de inicio


de contrato los años 98 y 99.

SELECT COUNT(DISTINCT employee_id),


COUNT(DECODE(SUBSTR(start_date,7),’99’,’1’,’98’,’1’ ))
FROM job_history;

4. Listar para cada last_name de jefe, el salario medio de los empleados que
dependen de él.

SELECT j.last_name, avg(e.salary)


FROM employees j
JOIN employees e
ON j.manager_id = e.employee_id
GROUP BY j.last_name;
5. Listar el nombre de departamento y el año más reciente en que se ha hecho
contrato (hire_date) a algún empleado de ese departamento. Ordenar el
resultado por nombre de departamento.

SELECT d.department_name, max(e.hire_date)


FROM departments d, employees e
WHERE d.department_id = e.department_id
GROUP BY department_name
ORDER BY d.department_name;

SELECT d.department_name, max(e.hire_date)


FROM departments d
JOIN employees e
ON d.department_id = e.department_id
GROUP BY department_name
ORDER BY d.department_name;

SELECT d.department_name, max(e.hire_date)


FROM departments d
JOIN employees e
USING (department_id)
GROUP BY department_name
ORDER BY d.department_name;

6. Listar el nombre de los países y el número de empleados total de los


departamentos ubicados en esos países. Mostrar el resultado solo para ‘United
States of America’ , para ‘Canada’ y para ‘Japan’.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy