You are on page 1of 5

SOLUCION A LA SEGUNDA GUIA

--PREGUNTA 1
SELECT s.first_name, s.last_name, s.phone
FROM student s, zipcode z
WHERE s.zip=z.zip
AND z.city='LOS ANGELES' AND s.last_name like '%R%';

--PREGUNTA 2
SELECT ins.first_name, ins.last_name, count(sec.section_id)
FROM instructor ins, section sec
WHERE ins.instructor_id=sec.instructor_id
GROUP BY ins.first_name, ins.last_name
HAVING count(sec.section_id) > 1;

--PREGUNTA3
SELECT s.student_id, co.course_no, co.description, co.cost, se.section_no,
ins.first_name || ' ' || ins.last_name as "NOMBRE DOCENTE"
FROM student s, enrollment e, section se, course co, instructor ins
WHERE s.student_id=e.student_id AND e.section_id=se.section_id AND
se.course_no=co.course_no AND se.instructor_id=ins.instructor_id;

--PREGUNTA4
SELECT se.section_no,
co.description,
ins.first_name || ' ' || ins.last_name AS "DOCENTE A
CARGO",
se.capacity,
se.capacity - (SELECT COUNT(*) FROM enrollment WHERE
enrollment.section_id = en.section_id) as "DISPONIBILIDAD"
FROM course co, section se, enrollment en, instructor ins
WHERE co.course_no=se.course_no AND se.section_id=en.section_id AND
se.instructor_id=ins.instructor_id;

--PREGUNTA5
SELECT cour.course_no, cour.description, count(enr.student_id) as
"CANTIDAD_DE_ALUMNOS"
FROM enrollment enr, section sec, course cour
WHERE enr.section_id=sec.section_id AND sec.course_no=cour.course_no
GROUP BY cour.course_no, cour.description
HAVING count(enr.student_id) < ( SELECT avg(count(enr.student_id))
as "CANTIDAD_DE_ALUMNOS"
FROM
enrollment enr, section sec, course cour
WHERE
enr.section_id=sec.section_id AND sec.course_no=cour.course_no
GROUP BY
cour.course_no, cour.description);

--PREGUNTA6
SELECT e.first_name, e.hire_date, e.salary, d.department_name, j.job_title
FROM jobs j, employees e, departments d
WHERE j.job_id=e.job_id AND e.department_id=d.department_id
AND e.salary>=4500 AND e.salary<15000;

--PREGUNTA7
SELECT e.employee_id, e.first_name, c.country_name
FROM jobs j, employees e, departments d, locations l, countries c
WHERE j.job_id=e.job_id AND e.department_id=d.department_id AND
d.location_id=l.location_id AND l.country_id=c.country_id
AND j.job_title='Accountant';

--PREGUNTA8
SELECT *
FROM jobs j, employees e, departments d, locations l, countries c
WHERE j.job_id=e.job_id AND e.department_id=d.department_id AND
d.location_id=l.location_id AND l.country_id=c.country_id
AND (l.city='Mexico City' or c.country_name='United States of
America');

--PREGUNTA9
SELECT *
FROM jobs j, employees e, departments d, locations l, countries c
WHERE j.job_id=e.job_id AND e.department_id=d.department_id AND
d.location_id=l.location_id AND l.country_id=c.country_id
AND c.country_name='United Kingdom' AND
d.department_name='Sales';

--PREGUNTA10
SELECT e.first_name, e.last_name
FROM jobs j, employees e, departments d, locations l, countries c, regions r
WHERE j.job_id=e.job_id AND e.department_id=d.department_id AND
d.location_id=l.location_id AND l.country_id=c.country_id AND
c.region_id=r.region_id
AND r.region_name='Americas' AND e.salary < (SELECT
AVG(e1.salary) FROM employees e1 WHERE e1.job_id = e.job_id);

--PREGUNTA11
SELECT r.region_name, c.country_name, count(e.employee_id) AS "Cantidad de
Empleados"
FROM employees e, departments d, locations l, countries c, regions r
WHERE e.department_id=d.department_id AND d.location_id=l.location_id AND
l.country_id=c.country_id AND c.region_id=r.region_id
GROUP BY r.region_name, c.country_name
HAVING count(e.employee_id)>3;

--PREGUNTA12
SELECT MAX(e.hire_date) as "MAS RECIENTE", MIN(e.hire_date) AS "MAS
ANTIGUO", (MAX(e.hire_date)-MIN(e.hire_date)) as "DIFERENCIA"
FROM employees e, departments d
WHERE e.department_id=d.department_id
AND d.department_name<>'Executive';

--PREGUNTA13
SELECT e.employee_id, e.first_name, e.last_name, NVL(tbl1."CANTIDAD
SUBORDINADOS",0) AS "SUBORDINADOS"
FROM employees e LEFT JOIN
(SELECT e1.manager_id, COUNT(e1.manager_id) AS
"CANTIDAD SUBORDINADOS"
FROM employees e1
GROUP BY e1.manager_id) tbl1 ON
tbl1.manager_id=e.employee_id
WHERE NVL(tbl1."CANTIDAD SUBORDINADOS",0)<8
ORDER BY e.employee_id;

--PREGUNTA14
SELECT 'El empleado ' || e.first_name || ' ' || e.last_name || ' ha
trabajado por ' || TRUNC(TRUNC(sysdate-e.hire_date)/365,2) || ' anios, ' ||
TRUNC(TRUNC(sysdate-e.hire_date)/30,2) || ' meses y ' || trunc(sysdate-e.hire_date)
|| ' días'
FROM employees e, departments d, locations l, countries c, regions r
WHERE e.department_id=d.department_id AND d.location_id=l.location_id AND
l.country_id=c.country_id AND c.region_id=r.region_id;

--PREGUNTA15
SELECT jh.employee_id, e.first_name || ' ' || e.last_name AS "EMPLEADO",
j.job_title, jh.start_date, jh.end_date
FROM job_history jh, jobs j, employees e
WHERE jh.job_id=j.job_id AND jh.employee_id=e.employee_id
UNION ALL
SELECT e.employee_id, e.first_name || ' ' || e.last_name AS "EMPLEADO",
j.job_title, e.hire_date as START_DATE, sysdate AS END_DATE
FROM employees e,jobs j
WHERE e.job_id=j.job_id;

--PREGUNTA16
CREATE TABLE emp001
AS (
SELECT LPAD(e.employee_id,8,0) AS "employee_id",
UPPER(e.first_name) AS "first_name",
UPPER(e.last_name) AS "last_name",
e.email,
e.phone_number,
e.hire_date,
e.job_id,
e.salary,
e.commission_pct,
(SELECT first_name || ' ' || last_name FROM employees WHERE
employees.employee_id = e.manager_id) AS "SUPERVISOR",
d.department_name
FROM employees e LEFT JOIN departments d ON
e.department_id=d.department_id
WHERE e.salary > (SELECT AVG(salary) FROM employees)
);

SELECT * FROM emp001;

--PREGUNTA17
UPDATE emp001 e
SET e.salary=e.salary+(0.1*e.salary)
WHERE e.salary < (SELECT AVG(salary)
FROM emp001
WHERE department_name IS NOT NULL
AND department_name =
e.department_name
GROUP BY department_name);

--PREGUNTA18
UPDATE emp001 e
SET e.salary=e.salary+100
WHERE e."employee_id" in (SELECT e."employee_id"
FROM emp001 e, jobs j
WHERE j.job_id=e.job_id
AND
j.job_title='Programmer');

--PREGUNTA19
ALTER TABLE emp001
ADD (
COMMISSION NUMBER(8,2),
TOTAL_SALARY NUMBER(8,2)
);

--PREGUNTA20
UPDATE emp001 e
SET e.commission=NVL(e.commission_pct,0)*e.salary,
e.total_salary=NVL(e.commission,0)+e.salary;

--PREGUNTA21
DELETE FROM emp001 e
WHERE e."employee_id" IN (SELECT e."employee_id"
FROM
emp001 e, departments d, locations l, countries c, regions r
WHERE
e.department_name=d.department_name AND d.location_id=l.location_id AND
l.country_id=c.country_id AND c.region_id=r.region_id

AND r.region_name='Americas');

--PREGUNTA22
TRUNCATE TABLE emp001;

--PREGUNTA23
CREATE TABLE emp001
AS (
SELECT LPAD(e.employee_id,8,0) AS "employee_id",
UPPER(e.first_name) AS "first_name",
UPPER(e.last_name) AS "last_name",
e.email,
e.phone_number,
e.hire_date,
e.job_id,
e.salary,
e.commission_pct ,
(SELECT first_name || ' ' || last_name FROM employees WHERE
employees.employee_id = e.manager_id) AS "SUPERVISOR",
d.department_name,
(NVL(e.commission_pct,0)*e.salary) AS "COMMISSION",
(NVL(e.commission_pct,0)*e.salary) + e.salary AS
"TOTAL_SALARY"
FROM employees e LEFT JOIN departments d ON
e.department_id=d.department_id
WHERE e.salary > (SELECT AVG(salary) FROM employees)
);

--EXAMEN

--PREGUNTA 4
SELECT t1.last_name,MAX(t1."CANTIDAD"),MIN(t1."CANTIDAD"),AVG(t1."CANTIDAD")
FROM (SELECT ins.last_name,
count(en.student_id) AS "CANTIDAD"
FROM enrollment en,section se, instructor ins, zipcode z
WHERE en.section_id=se.section_id AND
se.instructor_id=ins.instructor_id AND ins.zip=z.zip
AND z.zip <> 'Miami'
GROUP BY ins.last_name
HAVING count(en.student_id) > 1) T1
GROUP BY t1.last_name ;

--PREGUNTA 5
SELECT
tbl1.first_name,tbl1.last_name,tbl1.salary,tbl1.region_name,tbl2."cantidad",tbl2."p
romedio"
FROM (SELECT e.first_name,
e.last_name,
e.salary,
r.region_name
FROM employees e, departments d, locations l, countries
c, regions r
WHERE e.department_id=d.department_id AND
d.location_id=l.location_id AND l.country_id=c.country_id AND
c.region_id=r.region_id
AND c.country_name <> 'Mexico'
AND e.last_name not like '%s%s%') tbl1,
(SELECT r.region_name,COUNT(*) AS "cantidad",avg(e.salary)
AS "promedio"
FROM employees e, departments d, locations l, countries
c, regions r
WHERE e.department_id=d.department_id AND
d.location_id=l.location_id AND l.country_id=c.country_id AND
c.region_id=r.region_id
group by r.region_name) tbl2
WHERE tbl1.region_name=tbl2.region_name;

You might also like