Está en la página 1de 112

TEMA:

1. Introducción

Tomado de https://www.oracle.com
Se trata del esquema que se utiliza en este curso. En los
registros de recursos humanos (HR), cada empleado tiene
un número de identificación, una dirección de correo
electrónico, un código de identificación de cargo, un
salario y un gestor. Algunos empleados ganan comisiones
además de su salario.
La compañía también registra información sobre cargos
dentro de la organización. Cada cargo dispone de un
código de identificación, un cargo y un rango de salario
máximo y mínimo. Algunos empleados llevan bastante
tiempo en la compañía y han desempeñado distintos
trabajos en ella. Cuando se reasigna un empleado, se
registran la duración del trabajo del empleado, el número
de identificación del cargo y el departamento.

2
Esquema HR

3
4
5
Ejercicios iniciales esquema HR
Ingresar a Oracle Apex

6
Seleccionar Taller de SQL

7
Seleccionar Comandos SQL

8
Se debe de mostrar una ventana como esta:

9
DESC[RIBE] employees;
DESC[RIBE] departments;
DESC[RIBE] locations;
DESC[RIBE] job_history;
DESC[RIBE] jobs;
DESC[RIBE] countries;
DESC[RIBE] regions;

10
1. Obtener todos los datos de la tabla de empleados:
SELECT * FROM employees;

2. Obtener los códigos y nombres de los empleados que


trabajan en el departamento 10:

SELECT employee_id, first_name 
FROM employees WHERE department_id = 10
;
3 . Seleccionar todos los datos del departamento 40:

SELECT * FROM departments
WHERE department_id = 40;

11
4. Seleccionar los datos de los empleados del departamento 40:

SELECT * FROM employee WHERE deptno = 40

5. Obtener los datos de los departamentos ordenados


por el nombre del departamento

SELECT * FROM departments ORDER BY department_
name;

SELECT * FROM departments  ORDER BY 2;

12
6. Obtener la comisión, departamento y nombre de los
empleados cuyo salario sea inferior a 2,500, ordenándolos por
departamento en orden creciente, y por comisión en orden
decreciente dentro de cada departamento:

SELECT commission_pct AS COMISION,
department_id AS DEPARTAMENTO, 
first_name NOMBRE
FROM employees
WHERE salary <2500
ORDER BY department_id, commission_pct D
ESC

13
7. Hallar todas las combinaciones diferentes de valores de
puesto de trabajo (job_id) y año de contratación en el
departamento 30:

SELECT job_id, TO_CHAR(hire_date,'yyyy') CONTRATADO
FROM employees
WHERE department_id = 30;

SELECT distinct job_id, TO_CHAR(hire_date,'yyyy') CONTR
ATADO
FROM employees
WHERE department_id = 30;

14
8. Obtener las diferentes comisiones de los empleados cuyo
salario es inferior a 2500, ordenándolas de forma decreciente.

SELECT COMMISSION_PCT AS COMIS
ION
FROM EMPLOYEES
WHERE SALARY<2500
ORDER BY COMMISSION_PCT DESC

15
Predicados

Los predicados expresan condiciones. Si aparecen en una


cláusula WHERE indican una condición que las filas de las tablas
involucradas deben cumplir. Pueden aparecer también en una
clausula HAVING, como veremos posteriormente. Pueden ser
simples, si incluyen una ´única condición, o compuestos si
constan de varias, o sea, si combinan varios predicados simples,
unidos por los operadores AND, OR y NOT.

16
Predicados simples
Los predicados simples en su formato más elemental. Son
los siguientes:
De comparación. Comparan dos expresiones según un
operador de comparación, que puede ser:
<
<= Ejemplo:
=
!= SELECT commission_pct AS COMISION, 
<> department_id AS DEPARTAMENTO,
>=  first_name NOMBRE
> FROM employees
|| WHERE salary <2500

17
De valor nulo. Los predicados de comparación no sirven para
determinar los valores nulos. Por ejemplo, no es valido
commission_pct = NULL porque sería discernir si un valor
que puede ser desconocido es igual a otro también desconocido.

Se requiere un predicado especial, con formato: IS [NOT] NULL

SELECT * FROM employees
Where commission_pct is NULL

select * from employees 
where commission_pct is NOT NUL
L;

18
De rango (BETWEEN). Compara si los valores de una expresión
están o no entre los de otras dos.

SELECT * FROM employees 
WHERE salary BETWEEN 3000 AND 1000
0;
De pertenencia a un conjunto (IN). Comprueba si el valor de
una expresión coincide con alguno de los incluidos en una lista
de valores.

select * from employees
WHERE department_id IN (10, 30)
;
SELECT FIRST_NAME FROM EMPLOYEES WHE
RE JOB_ID IN ('PU_CLERK','PU_MAN');

19
De correspondencia con un patrón o modelo (LIKE). Comprueba
si el valor de una expresión alfanumérica se corresponde con un
modelo. El modelo puede incluir dos caracteres que actúan como
comodines:
_ Indica un único carácter, incluido el blanco.
% Indica una cadena de caracteres de cualquier longitud, incluida
la cadena vacía.

select * from employees 
WHERE first_name LIKE '%tt%'

select * from employees
WHERE first_name LIKE '____'

20
Predicados compuestos
Son la unión de dos o más predicados simples mediante los
operadores lógicos AND, OR y NOT. Al existir una lógica de
tres valores, debemos considerar el efecto del valor NULL:

21
Obtener los nombres, salarios y fechas de contratación de los
empleados que, o bien ingresaron después de 10-12-00, o bien
tienen un salario inferior a 3500. Clasificar los resultados por
fecha y nombre.
SELECT first_name, salary, hire_date
From employees
wHERE hire_date > '10-12-2000'
OR salary < 3500
ORDER BY 3,1 

Nombre de los empleados que ganan más de 3500 en total

SELECT first_name, salary, hire_date
From employees
wHERE salary > 3500 or salary+COMMISSION_PCT>3
500

22
select * from employees 
WHERE first_name LIKE '_i%'

select * from employees 
WHERE first_name LIKE '_i%m'

23
24
1. Display employees who joined in
2000 and doing job that has maximum
salary more than 10000.

select first_name,job_id,salary, hire_date


from employees
where to_char(hire_date,'yyyy') = 2000
and job_id in
(select job_id from jobs
where max_salary > 10000)

25
2. Display departments where the name of
the manager is MICHAEL.

select * from departments


where manager_id in
(select employee_id
from employees
where upper(first_name) like '%MICHAEL
%')

26
3. Display jobs where the minimum salary is less than
salary of employee 105.

select * from jobs where min_salary <


(select salary
from employees
where employee_id = 105)

27
4. Display employees who have underscore
in their email address

select * from employees


where email like '%\_%' ESCAPE '\'

28
5. Display employee name and manager name of
the employee.

select e1.first_name Employee,


e2.first_name Manager
from employees e1, employees e2
where e1.manager_id = e2.employee_id

29
6. Display number of employees joined in each
year into department 30.

select to_char(hire_date,'yyyy') ,
count(*)
from employees
where department_id = 30
group by to_char(hire_date,'yyyy');

30
7. Display job id, department id and sum of salary by
including al possible dimensions.

select department_id department,


job_id job, sum(salary) TotalSalary
from employees
group by cube( department_id, job_id)
order by department_id, job_id;

31
8. Display employee name and job title of jobs where salary
of employee is between minimum and maximum salary for
job.

select first_name, job_title


from employees e, jobs j
where salary between
min_salary and max_salary
order by first_name;

32
9. Display how many employees have commission
percentage and how many do not have.

select count(commission_pct)
NoEmployeesWithCommission,
count(*) - count(commission_pct)
NoEmployeesWithoutCommssion
from employees

33
10. Display first name, job title, department name of
employees who joined on 28th Feb.
select first_name, job_title,
department_name
from employees e, jobs j, departments
d
where e.job_id = j.job_id
and e.department_id = d.department_id
and to_char(hire_date,'ddmm') =
'2802';

34
11. Display details of jobs where the minimum
salary is greater than 10000.

SELECT * FROM JOBS


WHERE MIN_SALARY > 10000

35
12. Display the first name and join date of the employees
who joined between 2002 and 2005.

SELECT FIRST_NAME, HIRE_DATE


FROM EMPLOYEES
WHERE TO_CHAR(HIRE_DATE, 'YYYY')
BETWEEN 2002 AND 2005 ORDER BY HIRE_DATE

36
13. Display first name and join date of the employees who is
either IT Programmer or Sales Man.

SELECT FIRST_NAME, HIRE_DATE


FROM EMPLOYEES
WHERE JOB_ID IN ('IT_PROG', 'SA_MAN')

37
14. Display employees who joined after 1st
January 2008.

SELECT * FROM EMPLOYEES


where hire_date > '01-jan-2008'

38
15. Display details of employee with ID 150 or
160.

SELECT * FROM EMPLOYEES


WHERE EMPLOYEE_ID in (150,160)

39
16. Display first name, salary, commission pct, and hire date
for employees with salary less than 10000.

SELECT FIRST_NAME, SALARY,


COMMISSION_PCT, HIRE_DATE
FROM EMPLOYEES
WHERE SALARY < 10000

40
17. Display job Title, the difference between minimum and
maximum salaries for jobs with max salary in the range
10000 to 20000.

SELECT JOB_TITLE,
MAX_SALARY-MIN_SALARY DIFFERENCE
FROM JOBS
WHERE MAX_SALARY
BETWEEN 10000 AND 20000

41
18. Display first name, salary, and round the
salary to thousands.

SELECT FIRST_NAME,
SALARY,
ROUND(SALARY, -3)
FROM EMPLOYEES

42
19. Display details of jobs in the descending order
of the title.

SELECT * FROM JOBS


ORDER BY JOB_TITLE

43
20. Display employees where the first name or
last name starts with S.

SELECT FIRST_NAME, LAST_NAME


FROM EMPLOYEES
WHERE FIRST_NAME LIKE 'S%'
OR
LAST_NAME LIKE 'S%'

44
21. Display employees who joined in the month
of May.

SELECT * FROM EMPLOYEES


WHERE
TO_CHAR(HIRE_DATE, 'MON')= 'MAY'

45
22. Display details of the employees where commission
percentage is null and salary in the range 5000 to 10000 and
department is 30.

SELECT * FROM EMPLOYEES


WHERE COMMISSION_PCT IS NULL
AND
SALARY BETWEEN 5000
AND
10000 AND DEPARTMENT_ID=30

46
23. Display first name and date of first salary of the
employees.

SELECT FIRST_NAME,
HIRE_DATE, LAST_DAY(HIRE_DATE)+1
FROM EMPLOYEES

47
24. Display first name and experience of the
employees.

SELECT FIRST_NAME,
HIRE_DATE,
FLOOR((SYSDATE-HIRE_DATE)/365)
FROM EMPLOYEES

48
25. Display first name of employees who joined in
2001.

SELECT FIRST_NAME,
HIRE_DATE
FROM EMPLOYEES
WHERE TO_CHAR(HIRE_DATE, 'YYYY')=2001

49
26. Display first name and last name after converting the first
letter of each name to upper case and the rest to lower case.

SELECT
INITCAP(FIRST_NAME),
INITCAP(LAST_NAME)
FROM EMPLOYEES

50
27. Display the first word in job title.

SELECT JOB_TITLE,
SUBSTR(JOB_TITLE,1, INSTR(JOB_TITLE || ' '
FROM JOBS

51
28. Display the length of first name for employees where last
name contain character ‘b’ after 3rd position.

SELECT FIRST_NAME, LAST_NAME


FROM EMPLOYEES
WHERE INSTR(LAST_NAME,'B') > 3

52
29. Display first name in upper case and email address in
lower case for employees where the first name and email
address are same irrespective of the case.

SELECT UPPER(FIRST_NAME),
LOWER(EMAIL)
FROM EMPLOYEES
WHERE UPPER(FIRST_NAME)= UPPER(EMAIL)

53
30. Display employees who joined in the current
year.

SELECT * FROM EMPLOYEES


WHERE
TO_CHAR(HIRE_DATE,'YYYY')=TO_CHAR(SYSDATE,

54
31. Display the number of days between system
date and 1st January 2011.

SELECT SYSDATE - to_date('01-jan-2011')


FROM DUAL

55
32. Display how many employees joined in each
month of the current year.

SELECT TO_CHAR(HIRE_DATE,'MM'), COUNT (*)


FROM EMPLOYEES
WHERE
TO_CHAR(HIRE_DATE,'YYYY') = TO_CHAR(SYSDA
GROUP BY TO_CHAR(HIRE_DATE,'MM')

56
33. Display manager ID and number of employees
managed by the manager.

SELECT MANAGER_ID, COUNT(*)


FROM EMPLOYEES
GROUP BY MANAGER_ID

57
34. Display employee ID and the date on which
he ended his previous job.

SELECT EMPLOYEE_ID, MAX(END_DATE)


FROM JOB_HISTORY
GROUP BY EMPLOYEE_ID

58
35. Display number of employees joined after
15th of the month.

SELECT COUNT(*)
FROM EMPLOYEES
WHERE TO_CHAR(HIRE_DATE,'DD') > 15

59
36. Display the country ID and number of cities
we have in the country.

SELECT COUNTRY_ID,
COUNT(*)
FROM LOCATIONS
GROUP BY COUNTRY_ID

60
37. Display average salary of employees in each department
who have commission percentage.

SELECT DEPARTMENT_ID,
AVG(SALARY)
FROM EMPLOYEES
WHERE COMMISSION_PCT IS NOT NULL
GROUP BY DEPARTMENT_ID

61
38. Display job ID, number of employees, sum of salary, and
difference between highest salary and lowest salary of the
employees of the job.

SELECT JOB_ID,
COUNT(*),
SUM(SALARY),
MAX(SALARY)-MIN(SALARY) SALARY
FROM EMPLOYEES
GROUP BY JOB_ID

62
39. Display job ID for jobs with average salary
more than 10000.

SELECT JOB_ID,
AVG(SALARY)
FROM EMPLOYEES
GROUP BY JOB_ID
HAVING AVG(SALARY)>10000

63
40. Display years in which more than 10
employees joined.

SELECT TO_CHAR(HIRE_DATE,'YYYY')
FROM EMPLOYEES
GROUP BY TO_CHAR(HIRE_DATE,'YYYY')
HAVING COUNT(EMPLOYEE_ID) > 10

64
41. Display departments in which more than five employees
have commission percentage.

SELECT DEPARTMENT_ID
FROM EMPLOYEES
WHERE COMMISSION_PCT IS NOT NULL
GROUP BY DEPARTMENT_ID
HAVING COUNT(COMMISSION_PCT)>5

65
42. Display employee ID for employees who did
more than one job in the past.

SELECT EMPLOYEE_ID
FROM JOB_HISTORY
GROUP BY EMPLOYEE_ID
HAVING COUNT(*) > 1

66
43. Display job ID of jobs that were done by more than 3
employees for more than 100 days.

SELECT JOB_ID
FROM JOB_HISTORY
WHERE END_DATE-START_DATE > 100
GROUP BY JOB_ID
HAVING COUNT(*)>3

67
44. Display department ID, year, and Number of
employees joined.
SELECT DEPARTMENT_ID,
TO_CHAR(HIRE_DATE,'YYYY'),
COUNT(EMPLOYEE_ID)
FROM EMPLOYEES
GROUP BY DEPARTMENT_ID, TO_CHAR(HIRE_DAT
ORDER BY DEPARTMENT_ID

68
45. Display departments where any manager is managing
more than 5 employees.

SELECT DISTINCT DEPARTMENT_ID


FROM EMPLOYEES
GROUP BY DEPARTMENT_ID, MANAGER_ID
HAVING COUNT(EMPLOYEE_ID) > 5

69
46. Change salary of employee 115 to 8000 if the existing
salary is less than 6000.

UPDATE EMPLOYEES SET SALARY = 8000


WHERE EMPLOYEE_ID = 115 AND SALARY < 600

70
47. Insert a new employee into employees with all the
required details.

INSERT INTO EMPLOYEES


(EMPLOYEE_ID, FIRST_NAME, LAST_NAME,
EMAIL, PHONE_NUMBER, HIRE_DATE,JOB_ID,
SALARY, DEPARTMENT_ID)
VALUES
(207, 'ANGELA', 'SNYDER',
'ANGELA','215 253 4737', SYSDATE, 'SA_MA
12000, 80)

71
48. Delete department 20.

DELETE FROM DEPARTMENTS


WHERE DEPARTMENT_ID=20

72
49. Change job ID of employee 110 to IT_PROG if the
employee belongs to department 10 and the existing job ID
does not start with IT.

UPDATE EMPLOYEES SET JOB_ID= 'IT_PROG'


WHERE EMPLOYEE_ID=110
AND
DEPARTMENT_ID=10
AND
NOT JOB_ID LIKE 'IT%'

73
50. Insert a row into departments table with manager ID 120
and location ID in any location ID for city Tokyo.

INSERT INTO
DEPARTMENTS (150,'SPORTS',120,1200)

74
51. Display department name and number of employees in
the department.

SELECT DEPARTMENT_NAME, COUNT(*)


FROM EMPLOYEES
JOIN DEPARTMENTS
USING (DEPARTMENT_ID)
GROUP BY DEPARTMENT_NAME

75
52. Display job title, employee ID, number of days between
ending date and starting date for all jobs in department 30
from job history.

SELECT EMPLOYEE_ID, JOB_TITLE,


END_DATE-START_DATE DAYS
FROM JOB_HISTORY NATURAL JOIN JOBS
WHERE DEPARTMENT_ID=30

76
53. Display department name and manager first
name.

SELECT DEPARTMENT_NAME, FIRST_NAME


FROM DEPARTMENTS D JOIN EMPLOYEES E
ON(D.MANAGER_ID=E.EMPLOYEE_ID)

77
54. Display department name, manager name,
and city.

SELECT DEPARTMENT_NAME, FIRST_NAME, CITY


FROM DEPARTMENTS D JOIN EMPLOYEES E
ON (D.MANAGER_ID=E.EMPLOYEE_ID) JOIN LOC
USING (LOCATION_ID)

78
55. Display country name, city, and department
name.

SELECT COUNTRY_NAME, CITY, DEPARTMENT_NA


FROM COUNTRIES JOIN LOCATIONS
USING (COUNTRY_ID) JOIN DEPARTMENTS
USING (LOCATION_ID)

79
56. Display job title, department name, employee last name,
starting date for all jobs from 2000 to 2005.

SELECT JOB_TITLE, DEPARTMENT_NAME, LAST_


START_DATE FROM JOB_HISTORY JOIN JOBS
USING (JOB_ID) JOIN DEPARTMENTS
USING (DEPARTMENT_ID) JOIN EMPLOYEES
USING (EMPLOYEE_ID)
WHERE TO_CHAR(START_DATE,'YYYY')
BETWEEN 2000 AND 2005

80
57. Display job title and average salary of employees

SELECT JOB_TITLE, AVG(SALARY)


FROM EMPLOYEES NATURAL JOIN JOBS
GROUP BY JOB_TITLE

81
58. Display job title, employee name, and the difference
between maximum salary for the job and salary of the
employee.

SELECT JOB_TITLE, FIRST_NAME,


MAX_SALARY-SALARY DIFFERENCE
FROM EMPLOYEES NATURAL JOIN JOBS

82
59. Display last name, job title of employees who have
commission percentage and belongs to department 30.

SELECT JOB_TITLE, FIRST_NAME,


MAX_SALARY-SALARY DIFFERENCE
FROM EMPLOYEES NATURAL JOIN JOBS
WHERE DEPARTMENT_ID = 30

83
60. Display details of jobs that were done by any employee
who is currently drawing more than 15000 of salary.

SELECT JH.* FROM JOB_HISTORY JH JOIN EMP


ON (JH.EMPLOYEE_ID = E.EMPLOYEE_ID)
WHERE SALARY > 15000

84
61. Display department name, manager name, and salary of
the manager for all managers whose experience is more than
5 years.

SELECT DEPARTMENT_NAME, FIRST_NAME, SALA


FROM DEPARTMENTS D JOIN EMPLOYEES E
ON (D.MANAGER_ID=E.MANAGER_ID)
WHERE (SYSDATE-HIRE_DATE) / 365 > 5

85
62. Display employee name if the employee
joined before his manager.

SELECT FIRST_NAME
FROM EMPLOYEES E1 JOIN EMPLOYEES E2
ON (E1.MANAGER_ID=E2.EMPLOYEE_ID)
WHERE E1.HIRE_DATE < E2.HIRE_DATE

86
63. Display employee name, job title for the jobs employee
did in the past where the job was done less than six months.

SELECT FIRST_NAME, JOB_TITLE


FROM EMPLOYEES E JOIN JOB_HISTORY JH
ON (JH.EMPLOYEE_ID = E.EMPLOYEE_ID) JOIN
ON( JH.JOB_ID = J.JOB_ID)
WHERE MONTHS_BETWEEN(END_DATE,START_DATE

87
64. Display employee name and country in which he is
working.

SELECT FIRST_NAME, COUNTRY_NAME


FROM EMPLOYEES JOIN DEPARTMENTS
USING(DEPARTMENT_ID) JOIN LOCATIONS
USING( LOCATION_ID) JOIN COUNTRIES
USING ( COUNTRY_ID)

88
65. Display department name, average salary and number of
employees with commission within the department.

SELECT DEPARTMENT_NAME,
AVG(SALARY), COUNT(COMMISSION_PCT)
FROM DEPARTMENTS JOIN EMPLOYEES
USING (DEPARTMENT_ID)
GROUP BY DEPARTMENT_NAME

89
66. Display the month in which more than 5 employees
joined in any department located in Sydney.

SELECT TO_CHAR(HIRE_DATE,'MON-YY')
FROM EMPLOYEES JOIN DEPARTMENTS
USING (DEPARTMENT_ID) JOIN LOCATIONS
USING (LOCATION_ID)
WHERE CITY = 'Seattle'
GROUP BY TO_CHAR(HIRE_DATE,'MON-YY')
HAVING COUNT(*) > 5

90
67. Display details of departments in which the maximum
salary is more than 10000.

SELECT * FROM DEPARTMENTS


WHERE DEPARTMENT_ID
IN
(SELECT DEPARTMENT_ID
FROM EMPLOYEES
GROUP BY DEPARTMENT_ID
HAVING MAX(SALARY)>10000)

91
68. Display details of departments managed by
‘Smith’.

SELECT * FROM DEPARTMENTS


WHERE MANAGER_ID
IN
(SELECT EMPLOYEE_ID
FROM EMPLOYEES
WHERE FIRST_NAME='SMITH')

92
69. Display jobs into which employees joined in
the current year.

SELECT * FROM JOBS


WHERE JOB_ID IN
(SELECT JOB_ID
FROM EMPLOYEES
WHERE TO_CHAR(HIRE_DATE,'YYYY')
=
TO_CHAR(SYSDATE,'YYYY'))

93
70. Display employees who did not do any job in
the past.

SELECT * FROM EMPLOYEES


WHERE EMPLOYEE_ID
NOT IN
(SELECT EMPLOYEE_ID
FROM JOB_HISTORY)

94
71. Display job title and average salary for
employees who did a job in the past.
SELECT JOB_TITLE,
AVG(SALARY)
FROM JOBS NATURAL JOIN EMPLOYEES
GROUP BY JOB_TITLE
WHERE EMPLOYEE_ID
IN
(SELECT EMPLOYEE_ID
FROM JOB_HISTORY)

95
72. Display country name, city, and number of departments
where department has more than 5 employees.

SELECT COUNTRY_NAME, CITY, COUNT(DEPARTM


FROM COUNTRIES JOIN LOCATIONS
USING (COUNTRY_ID) JOIN DEPARTMENTS
USING (LOCATION_ID)
WHERE DEPARTMENT_ID IN
(SELECT DEPARTMENT_ID
FROM EMPLOYEES
GROUP BY DEPARTMENT_ID
HAVING COUNT(DEPARTMENT_ID)>5)
GROUP BY COUNTRY_NAME, CITY; 96
73. Display details of manager who manages
more than 5 employees.

SELECT FIRST_NAME
FROM EMPLOYEES WHERE EMPLOYEE_ID
IN
(SELECT MANAGER_ID
FROM EMPLOYEES
GROUP BY MANAGER_ID
HAVING COUNT(*)>5)

97
74. Display employee name, job title, start date, and end
date of past jobs of all employees with commission
percentage null.

SELECT FIRST_NAME, JOB_TITLE,


START_DATE, END_DATE
FROM JOB_HISTORY JH
JOIN JOBS J
USING (JOB_ID) JOIN EMPLOYEES E
ON ( JH.EMPLOYEE_ID = E.EMPLOYEE_ID)
WHERE COMMISSION_PCT IS NULL

98
75. Display the departments into which no
employee joined in last two years.

SELECT * FROM DEPARTMENTS


WHERE DEPARTMENT_ID NOT IN
(SELECT DEPARTMENT_ID
FROM EMPLOYEES
WHERE FLOOR((SYSDATE-HIRE_DATE)/365) <

99
76. Display the details of departments in which the max
salary is greater than 10000 for employees who did a job in
the past.

SELECT * FROM DEPARTMENTS


WHERE DEPARTMENT_ID IN
(SELECT DEPARTMENT_ID
FROM EMPLOYEES
WHERE EMPLOYEE_ID
IN
(SELECT EMPLOYEE_ID
FROM JOB_HISTORY)
GROUP BY DEPARTMENT_ID
HAVING MAX(SALARY) >10000)
100
77. Display details of current job for employees who worked
as IT Programmers in the past.

SELECT * FROM JOBS


WHERE JOB_ID IN
(SELECT JOB_ID
FROM EMPLOYEES
WHERE EMPLOYEE_ID IN
(SELECT EMPLOYEE_ID
FROM JOB_HISTORY
WHERE JOB_ID='IT_PROG'))

101
78. Display the details of employees drawing the highest
salary in the department.

SELECT DEPARTMENT_ID,FIRST_NAME,
SALARY FROM EMPLOYEES OUTER
WHERE SALARY =
(SELECT MAX(SALARY)
FROM EMPLOYEES
WHERE DEPARTMENT_ID = OUTER.DEPARTME

102
79. Display the city of employee whose employee
ID is 105.

SELECT CITY FROM LOCATIONS


WHERE LOCATION_ID =
(SELECT LOCATION_ID FROM DEPARTMENTS
WHERE DEPARTMENT_ID =
(SELECT DEPARTMENT_ID
FROM EMPLOYEES
WHERE EMPLOYEE_ID=105))

103
80. Display third highest salary of all employees

select salary
from employees main
where 2 =
(select count( distinct salary )
from employees
where salary > main.salary)

104
105
106
107
108
109
110
111
112

También podría gustarte