SQL Exercises for Human Resources Reports
SQL Exercises for Human Resources Reports
You have been hired as an SQL programmer at XYZW Corporation. Your first task is to create some reports based on
in the data of the human resources tables.
1. Your first task is to determine the structure of the DEPARTMENTS table and its content.
DESCRIBE departments
SELECT *
FROM departments;
DESCRIBE employees
3. The human resources department wants to run an inquiry to display the surname, the code of
position, the date of admission, and the phone number of each employee, with the employee number displayed first. Provide the
nickname STARTDATE for the HIRE_DATE column.
FROM employees;
4. The human resources department needs a query to display all job codes
exclusive to the EMPLOYEES table.
FROM employees;
1. Due to budgetary issues, the department needs a report with the last name and the salary.
of employees who earn more than $12,000.
FROM employees
FROM employees
FROM employees
4. Create a report to display the surname, job ID, and hire date of employees whose
Last names are Matos and Taylor. Organize the query in ascending order by date of admission.
FROM employees
ORDER BY hire_date;
5. Display the last name and department number of all employees in departments 20 and 50.
ascending alphabetical order by name.
SELECT last_name, department_id
FROM employees
6. Build a query to display the last name and salary of employees who earn between $5,000 and $
12.000 e estão no departamento 20 ou 50. Atribua às colunas os labels Employee e Monthly Salary, respectivamente.
FROM employees
7. The human resources department needs a report displaying the last name and the admission date.
of all the employees hired in 1994.
FROM employees
8. Crie um relatório que exiba o sobrenome e o cargo de todos os funcionários não subordinados a um gerente.
FROM employees
10. Members of the human resources department wish to have more flexibility regarding inquiries.
created. They want a report that displays the last name and salary of employees who earn more than a
specified amount by the user after the prompt. (ONLY THE SCRIPT)
FROM employees
11. The human resources department wants to run reports based on a manager. Create a query.
that requests a manager ID from the user and generates the employee ID, surname, salary, and department of
employees of this manager. The human resources department wishes to have permission to classify the report in
a selected column. You can test the data with the following values: (ONLY THE SCRIPT)
ID do gerente = 103, classificado pelo sobrenome do funcionário:
ID do gerente = 201, classificado pelo salário:
ID do gerente = 124, classificado pelo ID do funcionário:
FROM employees
ORDER BY &order_col;
12. Show all last names of employees whose third letter of the name is 'a'.
SELECT last_name
FROM employees
SELECT last_name
FROM employees
FROM employees
15. Build a query to display the surname, salary, and commission of all employees whose commission
be 20%.
FROM employees
1. The human resources department requested a report of all employees and their respective IDs.
Display the last name concatenated with the job ID (separated by a comma and a space) and name the column
as Employee and Title.
FROM employees;
2. The human resources department needs a report to display the employee number, the
surname, the salary and the salary with a 15.5% increase (specified as an integer) of each employee.
Assign the label New Salary to the column.
FROM employees;
3. Modify the previous exercise to add a column that subtracts the old salary from the new salary.
Assign the label Increase to the column.
FROM employees;
4. Create a query that displays the last name and the length of the last name of all employees whose names
start with the letter J, A or M. Assign an appropriate label to each column. Classify the results by the
surnames of the employees.
LENGTH(last_name) "Length"
FROM employees
ORDER BY last_name ;
5. Recreate the previous query so that the user is asked to provide the initial letter of the last name.
For example, if the user inputs H when a letter is requested, the output should show all employees whose
last names start with the letter H.
LENGTH(last_name) "Length"
FROM employees
ORDER BY last_name;
6. The human resources department wants to know the length of employment of each employee. For
for each employee, display the last name and calculate the number of months between today and the employee's hiring date.
Assign the label MONTHS_WORKED to the column. Sort the results by the number of months the employee has been.
employee. Round the number of months to the nearest whole number.
FROM employees
ORDER BY MONTHS_WORKED;
7. Create a report that produces this information only for employees with salaries between 2000 and
4000:
<employee's surname> receives <salary> monthly, but wishes for <3 times the salary>.
SELECT CONCAT(last_name, 'receives ', salary, ' monthly, but desires ', salary * 3, '.')
as ‘Dream Salaries’
9. Display the last name, admission date, and end of the probation period (90 days after hiring) of everyone
the employees whose role is SALES REPRESENTATIVE (SA_REP). Assign the labels hiring date and end
from the experience to the respective columns. Format the dates to be displayed in the format '2000-july-23th'.
10. Create a query that displays the last names and commissions of the employees. If an employee does not earn
Commission, the information "No Commission" must be displayed. Assign the label COMM to the column.
11. With a CASE function, create a query that displays the level of all employees based on the value of
column JOB_ID. Use this data:
Cargo Level
AD_PRES A
ST_MAN B
IT_PROG C
SA_REP D
ST_CLERK E
WHEN 'AD_PRES' THEN 'A ELSE '0' END GRADE FROM employees;
EXTRAS
1. For budgetary purposes, HR needs a report on the projected salary increases. The report must
display the employees who do not earn commission but will have a salary increase of 10% (round the salaries)
for the monetary format)
FORMAT:
The salary of <employee> after a 10% raise is <salary with raise>.
2. Crie um relatório contendo os funcionários , os salários e os respectivos tempos de emprega (em anos). Orden
the report by the length of employment of the employees. The employee who has been employed the longest should be in
start of the list.
3. Display the employees whose last names start with the letters J, K, L, or M (REQUIRED TO USE THE
FUNCTION IN). Sort by last name.
- Cargo
- Salary
- Participation in the company's profits based on time with the company, according to the following criteria:
SA_REP,MK_REP - “NEGÓCIOS”
The report should include employees hired between the years of 1990 and 1997.
The report should be ordered by time in the company in descending order and by salary in ascending order.
ANSWERS
1–
Select
from employees
2–
Select
from employees
3–
where
order by last_name;
OU
where
order by last_name
4-
select
job_id as cargo,
salary as salario,
date_format(hire_date,'%d/%M/%Y') as admissao,
round(datediff(curdate(),hire_date)/365,0) as tempo_de_casa,
else salary*0.50
end
as Gratification,
else 'SUPPORT'
end
as Escalao
FROM employees
where
order by