0% found this document useful (0 votes)
112 views6 pages

Codigo HR - SQL

This document contains 44 SQL queries on the HR schema of the Oracle database. The queries select, filter, aggregate, and join data from tables like employees, departments, jobs, locations, countries and regions to retrieve various insights. Some examples include getting employee names and departments, total number of employees by department, highest and average salaries by year, and filtering data based on conditions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
112 views6 pages

Codigo HR - SQL

This document contains 44 SQL queries on the HR schema of the Oracle database. The queries select, filter, aggregate, and join data from tables like employees, departments, jobs, locations, countries and regions to retrieve various insights. Some examples include getting employee names and departments, total number of employees by department, highest and average salaries by year, and filtering data based on conditions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

--1

select FIRST_NAME, DEPARTMENT_ID, HIRE_DATE


from EMPLOYEES
order by DEPARTMENT_ID, HIRE_DATE desc;
--2
Select e1.EMPLOYEE_ID||' '||e1.FIRST_NAME||' '||e1.LAST_NAME as Empelado,
e2.EMPLOYEE_ID||' '||
e2.FIRST_NAME||' '||e2.LAST_NAME as Jefe
from EMPLOYEES e1
left join EMPLOYEES e2
on e1.MANAGER_ID=e2.EMPLOYEE_ID;
--3
select r.REGION_ID, REGION_NAME, COUNTRY_NAME
from REGIONS r
inner join COUNTRIES c
on r.REGION_ID=c.REGION_ID;
--4
select e.EMPLOYEE_ID, FIRST_NAME, LAST_NAME, START_DATE, END_DATE
from EMPLOYEES e
inner join JOB_HISTORY j
on e.EMPLOYEE_ID=j.EMPLOYEE_ID;
--5
select FIRST_NAME||' '||LAST_NAME Empleado, SALARY Salario, COMMISSION_PCT
Porcentaje,
nvl(COMMISSION_PCT * SALARY,0) Comision, SALARY + nvl(COMMISSION_PCT * SALARY,0)
"Salario total"
from EMPLOYEES;
--6
SELECT J.JOB_TITLE,E.SALARY
FROM (
select M.MANAGER_ID
from EMPLOYEES M
where M.MANAGER_ID IN (100,125)
GROUP BY M.MANAGER_ID
)M
LEFT JOIN EMPLOYEES E
ON E.EMPLOYEE_ID=M.MANAGER_ID AND E.SALARY > 6000
INNER JOIN JOBS J
ON E.JOB_ID=j.JOB_ID;
--7
select l.LOCATION_ID, L.CITY, D.DEPARTMENT_NAME
from DEPARTMENTS d
INNER JOIN LOCATIONS l
ON d.LOCATION_ID=l.LOCATION_ID
INNER JOIN COUNTRIES c
ON l.COUNTRY_ID=c.COUNTRY_ID and c.COUNTRY_ID !='US';
--8
select r.REGION_ID, REGION_NAME, COUNTRY_NAME
from REGIONS r
INNER JOIN COUNTRIES c
ON r.REGION_ID=c.REGION_ID and REGION_NAME='Asia';
--9
select r.REGION_ID, REGION_NAME, l.LOCATION_ID, l.CITY, c.COUNTRY_ID, COUNTRY_NAME
from LOCATIONS l
INNER JOIN COUNTRIES c
ON l.LOCATION_ID > 2400 AND l.COUNTRY_ID=c.COUNTRY_ID
INNER JOIN REGIONS r
ON c.REGION_ID=r.REGION_ID;
--10
select a.REGION_ID "Region", a.REGION_NAME "Nombre region", 'Codigo Pais:' ||
b.COUNTRY_ID ||' Nombre :' || b.COUNTRY_NAME "Pais", c.LOCATION_ID "Localización",
c.STREET_ADDRESS "Direccion ", c.POSTAL_CODE "Código Postal"
from LOCATIONS c
inner join COUNTRIES b
on b.COUNTRY_ID =c.COUNTRY_ID and c.POSTAL_CODE is not null
inner join REGIONS a
on a.REGION_ID = b.REGION_ID;
--11
select avg(SALARY)
from EMPLOYEES
where DEPARTMENT_ID in (30,80);
--12
select r.REGION_NAME, c.COUNTRY_NAME, l.STATE_PROVINCE, M.MANAGER_ID, e.FIRST_NAME,
e.LAST_NAME
from (
select x.MANAGER_ID
from EMPLOYEES x
GROUP BY x.MANAGER_ID
)M
inner join EMPLOYEES e
on m.MANAGER_ID=e.EMPLOYEE_ID
inner join DEPARTMENTS d
on e.DEPARTMENT_ID=d.DEPARTMENT_ID
inner join LOCATIONS l
on d.LOCATION_ID=l.LOCATION_ID and l.STATE_PROVINCE in('Washington','Oxford')
inner join COUNTRIES c
on l.COUNTRY_ID=c.COUNTRY_ID and c.COUNTRY_ID in ('UK','US')
inner join REGIONS r
on c.REGION_ID=r.REGION_ID;
--13
select FIRST_NAME||' '||LAST_NAME Nombre, COUNTRY_NAME
from EMPLOYEES e
inner join DEPARTMENTS d
on e.DEPARTMENT_ID=d.DEPARTMENT_ID
inner join LOCATIONS l
on d.LOCATION_ID=l.LOCATION_ID
inner join COUNTRIES c
on l.COUNTRY_ID=c.COUNTRY_ID and COUNTRY_NAME like 'C%';
--14
select JOB_TITLE Puesto, FIRST_NAME||' '||LAST_NAME Nombre
from JOBS j
inner join EMPLOYEES e
on j.JOB_ID=e.JOB_ID and email = 'NKOCHHAR' and HIRE_DATE = '21/09/1989';

--15
select FIRST_NAME||' '||LAST_NAME
from EMPLOYEES
where DEPARTMENT_ID in (10,20,80)
and months_between (sysdate,HIRE_DATE) >6
and COMMISSION_PCT >= 0.2
and (FIRST_NAME like 'J%' or LAST_NAME like 'J%');
--16
select FIRST_NAME||' '||LAST_NAME, DEPARTMENT_NAME
from EMPLOYEES e
INNER JOIN DEPARTMENTS d
ON e.DEPARTMENT_ID=d.DEPARTMENT_ID
and PHONE_NUMBER like'515%'
and length(PHONE_NUMBER)=12;
--17
select e.EMPLOYEE_ID, FIRST_NAME||','||LAST_NAME "Nombre Completo", SALARY Salario,
d.DEPARTMENT_ID "Código de Departamento", DEPARTMENT_NAME Descripción
from EMPLOYEES e
INNER JOIN DEPARTMENTS d
ON e.DEPARTMENT_ID=d.DEPARTMENT_ID
and DEPARTMENT_NAME='IT'
order by SALARY Desc;
--18
select FIRST_NAME, LAST_NAME, e.SALARY, DEPARTMENT_NAME, STREET_ADDRESS,
POSTAL_CODE, l.CITY
from EMPLOYEES e
inner join DEPARTMENTS d
on e.DEPARTMENT_ID=d.DEPARTMENT_ID
and d.DEPARTMENT_ID in (100,80,50)
and e.SALARY between 4000 and 8000
inner join LOCATIONS l
on d.LOCATION_ID=l.LOCATION_ID
and l.CITY ='South San Francisco';

--19
select EMPLOYEE_ID Codigo, LAST_NAME||', '||FIRST_NAME Nombres,
Initcap(email)||'@eisi.ues.edu.sv' email,
'('||substr(PHONE_NUMBER,1,3)||')-'||substr(PHONE_NUMBER,5,3)||'-'||
substr(PHONE_NUMBER,9,4) Telefono
from EMPLOYEES
where length(PHONE_NUMBER)=12
UNION
select EMPLOYEE_ID Codigo, LAST_NAME||', '||FIRST_NAME Nombres,
Initcap(email)||'@eisi.ues.edu.sv' email,'('||substr(PHONE_NUMBER,1,3)||'-'||
substr(PHONE_NUMBER,5,2)||'-'||substr(PHONE_NUMBER,8,4)||'-'||
substr(PHONE_NUMBER,13,6)||'-' Telefono
from EMPLOYEES
where length(PHONE_NUMBER)>12 order by 1;
--20
select CITY, COUNTRY_ID, (case
when COUNTRY_ID in ( select COUNTRY_ID
from COUNTRIES
where COUNTRY_NAME = 'United Kingdom')
then 'UNKing' else 'Non- UNKing' end)
from LOCATIONS
where CITY like 'S%';
--21
select DEPARTMENT_ID "Codigo del Departamento", count(*) "Numero empleados"
from EMPLOYEES
group by DEPARTMENT_ID order by 1;
--22
select FIRST_NAME
from EMPLOYEES
group by FIRST_NAME
having count(*)>1;
--23
select FIRST_NAME
from EMPLOYEES
group by FIRST_NAME
having count(*)=1;
--24
select r.REGION_ID, REGION_NAME, count(*)
from REGIONS r
INNER JOIN COUNTRIES c
ON r.REGION_ID=c.REGION_ID
group by r.REGION_ID,REGION_NAME order by 3 desc;

--25
select j.JOB_ID, count(*) numero
from EMPLOYEES e
inner join JOBS j
on e.JOB_ID=j.JOB_ID
group by j.JOB_ID order by 2 desc;
--26
select d.DEPARTMENT_ID, DEPARTMENT_NAME, count(*)
from EMPLOYEES e
inner join DEPARTMENTS d
on e.DEPARTMENT_ID=d.DEPARTMENT_ID
group by d.DEPARTMENT_ID,DEPARTMENT_NAME
order by DEPARTMENT_NAME;

--27
select r.REGION_ID, count(*)
from DEPARTMENTS d, LOCATIONS l, COUNTRIES c, REGIONS r
where d.LOCATION_ID=l.LOCATION_ID
and l.COUNTRY_ID=c.COUNTRY_ID
and c.REGION_ID=r.REGION_ID
group by r.REGION_ID
order by 1 asc;
--28
select d.DEPARTMENT_ID, DEPARTMENT_NAME, sum(SALARY)
from EMPLOYEES e
inner join DEPARTMENTS d
on e.DEPARTMENT_ID=d.DEPARTMENT_ID
group by d.DEPARTMENT_ID,DEPARTMENT_NAME
order by 3 desc;
--29
select extract(year from HIRE_DATE), min(SALARY), max(SALARY),avg(SALARY)
from EMPLOYEES
group by extract(year from HIRE_DATE)
order by 1 desc;
--30
select d.DEPARTMENT_ID "Codigo del Departamento", j.JOB_ID "Puesto de Trabajo",
count(*)
from DEPARTMENTS d
inner join EMPLOYEES e
on e.DEPARTMENT_ID=d.DEPARTMENT_ID and d.DEPARTMENT_ID in (50,80)
inner join JOBS j
on e.JOB_ID=j.JOB_ID
group by d.DEPARTMENT_ID,j.JOB_ID
order by d.DEPARTMENT_ID desc ,j.JOB_ID desc;
--31
select DEPARTMENT_ID "Codigo del Departamento", JOB_ID "Puesto de Trabajo",
count(*)
from EMPLOYEES
group by DEPARTMENT_ID,JOB_ID
having count(*)=1;

--32
select CITY, count(*)
from EMPLOYEES e,DEPARTMENTS d,LOCATIONS l
where e.DEPARTMENT_ID=d.DEPARTMENT_ID
and d.LOCATION_ID=l.LOCATION_ID
and SALARY >=5000
group by CITY
having count(*)>3;

--33
select DEPARTMENT_ID "Codigo del Departamento", count(*)
from EMPLOYEES
group by DEPARTMENT_ID
having count(*)>10;
--34
select LAST_NAME, FIRST_NAME, SALARY
from EMPLOYEES
where SALARY = (select max(SALARY) from EMPLOYEES);
--35
select DEPARTMENT_NAME, FIRST_NAME, LAST_NAME
from EMPLOYEES e
Inner join DEPARTMENTS d
on e.DEPARTMENT_ID=d.DEPARTMENT_ID
and d.DEPARTMENT_ID in (select DEPARTMENT_ID from EMPLOYEES where FIRST_NAME =
'John');

--36
select DEPARTMENT_ID, FIRST_NAME||' '||LAST_NAME
from EMPLOYEES e1
where (select count(*) from EMPLOYEES e2 where
e1.DEPARTMENT_ID=e2.DEPARTMENT_ID)<10 order by DEPARTMENT_ID;

--37
select d.DEPARTMENT_ID, DEPARTMENT_NAME, SALARY
from DEPARTMENTS d
inner join EMPLOYEES e
on e.DEPARTMENT_ID=d.DEPARTMENT_ID
and SALARY=(select max(SALARY) from EMPLOYEES e2 where
e.DEPARTMENT_ID=e2.DEPARTMENT_ID);

--38
select *
from EMPLOYEES e1
where 2 = ( select count(*) from EMPLOYEES e2 where
e1.DEPARTMENT_ID=e2.DEPARTMENT_ID);

--39
select DEPARTMENT_ID, FIRST_NAME||’ ‘||LAST_NAME
from EMPLOYEES e1
where (select count(*) from EMPLOYEES e2 where
e1.DEPARTMENT_ID=e2.DEPARTMENT_ID)<10 order by DEPARTMENT_ID;

--40
select DEPARTMENT_ID, FIRST_NAME||' '||LAST_NAME, SALARY
from EMPLOYEES
where DEPARTMENT_ID =30
and SALARY= (select max(SALARY) from EMPLOYEES where DEPARTMENT_ID=30);
--41
select DEPARTMENT_ID, DEPARTMENT_NAME
from DEPARTMENTS d
where not exists (select * from EMPLOYEES e where e.DEPARTMENT_ID=DEPARTMENT_ID);
--42
select FIRST_NAME, LAST_NAME
from EMPLOYEES
where DEPARTMENT_ID <> 30
and SALARY > all (select SALARY from EMPLOYEES where DEPARTMENT_ID=30);
--43
select e1.EMPLOYEE_ID, e1.FIRST_NAME||' '||e1.LAST_NAME, count(*) cuantos
from EMPLOYEES e1
inner join EMPLOYEES e2
on e2.MANAGER_ID=e1.EMPLOYEE_ID
and e2.MANAGER_ID in (select d.MANAGER_ID from DEPARTMENTS d where d.MANAGER_ID is
not null)
group by e1.EMPLOYEE_ID,e1.FIRST_NAME||' '||e1.LAST_NAME
having count(*) > 5
order by cuantos desc;
--44
select a.EMPLOYEE_ID, a.LAST_NAME ,a.SALARY, b.REGION_NAME , c.COUNTRY_NAME,
d.STATE_PROVINCE, e.DEPARTMENT_ID, e.DEPARTMENT_NAME
from EMPLOYEES a
INNER JOIN DEPARTMENTS e
ON a.DEPARTMENT_ID = e.DEPARTMENT_ID
and e.DEPARTMENT_NAME <> 'Finance'
INNER JOIN LOCATIONS d
ON e.LOCATION_ID = d.LOCATION_ID
and d.STATE_PROVINCE <> 'Texas'
INNER JOIN COUNTRIES c
ON d.COUNTRY_ID = c.COUNTRY_ID
INNER JOIN REGIONS b
ON c.REGION_ID = b.REGION_ID
WHERE
a.SALARY > (select avg(SALARY) from EMPLOYEES g where a.DEPARTMENT_ID =
g.DEPARTMENT_ID)
order by a.EMPLOYEE_ID;

You might also like