Assignmet_Hr_MySql - Solutions
Assignmet_Hr_MySql - Solutions
Data-------------------------------------------
The HR department needs your assistance in creating some queries.
1. Because of budget issues, the HR department needs a report that displays the
last name and salary of employees earning more than $12,000.
2. Open a new SQL Worksheet. Create a report that displays the last name and
department number for employee number 176.
4. Modify above query to list the last name and salary of employees who earn
between
$5,000 and $12,000, and are in department 20 or 50. Label the columns Employee and
Monthly Salary, respectively.
6. Modify lab_03_03.sql to list the last name and salary of employees who earn
between
$5,000 and $12,000, and are in department 20 or 50. Label the columns Employee and
Monthly Salary, respectively.
5. Create a report to display the last name, job ID, and hire date for employees
with the last names of Matos and Taylor. Order the query in ascending order by hire
date.
SELECT last_name, job_id, hire_date FROM employees
WHERE last_name IN ('Matos', 'Taylor')
ORDER BY hire_date;
7. The HR department needs a report that displays the last name and hire date of
all employees who were hired in 2006.
SELECT last_name, hire_date FROM employees
WHERE hire_date >= '01-JAN-06' AND hire_date < '01-JAN-07';
8. Create a report to display the last name and job title of all employees who
do not have a manager.
SELECT last_name, job_id FROM employees
WHERE manager_id IS NULL;
9. Create a report to display the last name, salary, and commission for all
employees who earn commissions. Sort data in descending order of salary and
commissions. Use the column’s numeric position in the ORDER BY clause.
SELECT last_name, salary, commission_pct FROM employees
WHERE commission_pct IS NOT NULL
ORDER BY 2 DESC, 3 DESC;
10. Display the last names of all employees where the third letter of the name is
“a.”
SELECT last_name FROM employees
WHERE last_name LIKE ' a%';
11. Display the last names of all employees who have both an “a” and an “e” in
their last name.
SELECT last_name FROM employees
WHERE last_name LIKE '%a%'
AND last_name LIKE '%e%';
12. Display the last name, job, and salary for all employees whose job is that of
a sales representative or a stock clerk, and whose salary is not equal to $2,500,
$3,500, or $7,000.
SELECT last_name, job_id, salary FROM employees
WHERE job_id IN ('SA_REP', 'ST_CLERK')
AND salary NOT IN (2500, 3500, 7000);
13. Display the last name, salary, and commission for all employees whose
commission amount is 20%.
SELECT last_name "Employee", salary "Monthly Salary",
commission_pct
FROM employees
WHERE commission_pct = .20;
2. Create a query that displays employees’ last names, and indicates the amounts
of their salaries with asterisks. Each asterisk signifies a thousand dollars. Sort
the data in descending order of salary. Label the column
EMPLOYEES_AND_THEIR_SALARIES.
SELECT last_name,
rpad(' ', salary/1000, '*') EMPLOYEES_AND_THEIR_SALARIES
FROM employees ORDER BY salary DESC;
------------------------------------------ Joins
--------------------------------------------------------
1. Write a query for the HR department to produce the addresses of all the
departments. Use the LOCATIONS and COUNTRIES tables. Show the location ID, street
address, city, state or province, and country in the output.
SELECT location_id, street_address, city, state_province, country_name
FROM locations NATURAL JOIN countries;
4. Create a report to display employees’ last names and employee numbers along
with their managers’ last names and manager numbers. Label the columns Employee,
Emp#, Manager, and Mgr#, respectively.
SELECT w.last_name "Employee", w.employee_id "EMP#", m.last_name "Manager",
m.employee_id "Mgr#"
FROM employees w JOIN employees m
ON (w.manager_id = m.employee_id);
6. Create a report for the HR department that displays employee last names,
department numbers, and all employees who work in the same department as a given
employee. Give each column an appropriate label. Save the script to a file named
lab_07_06.sql. Run the query.
SELECT e.department_id department, e.last_name employee, c.last_name colleague
FROM employees e JOIN employees c
ON (e.department_id = c.department_id) WHERE e.employee_id <> c.employee_id
ORDER BY e.department_id, e.last_name, c.last_name;
8. The HR department wants to determine the names of all employees who were
hired after Davies. Create a query to display the name and hire date of any
employee hired after employee Davies.
SELECT e.last_name, e.hire_date
FROM employees e JOIN employees davies
ON (davies.last_name = 'Davies') WHERE davies.hire_date < e.hire_date;
9. The HR department needs to find the names and hire dates of all employees who
were hired before their managers, along with their managers’ names and hire dates.
SELECT w.last_name, w.hire_date, m.last_name, m.hire_date
FROM employees w JOIN employees m
ON (w.manager_id = m.employee_id)
WHERE w.hire_date<m.hire_date
1. The HR department needs a query the to display the last name and hire date
of any employee in the same department as the employee whose name the user supplies
(excluding that employee). For example, if the user enters Zlotkey, find all
employees who work with Zlotkey (excluding Zlotkey).
SELECT last_name, hire_date FROM employees
WHERE department_id = (SELECT department_id
FROM employees
WHERE last_name = '&&Enter_name') AND last_name <> 'Zlotkey';
2. Create a report that displays the employee number, last name, and salary of
all employees who earn more than the average salary. Sort the results in ascending
order by salary.
SELECT employee_id, last_name, salary FROM employees
WHERE salary > (SELECT AVG(salary)
FROM employees)
ORDER BY salary;
3. Write a query that displays the employee number and last name of all
employees who work in a department with any employee whose last name contains the
letter “u.”
SELECT employee_id, last_name FROM employees
WHERE department_id IN (SELECT department_id
FROM employees
WHERE last_name like '%u%');
4. The HR department needs a report that displays the last name, department
number, and job ID of all employees whose department location ID is 1700.
SELECT last_name, department_id, job_id FROM employees
WHERE department_id IN (SELECT department_id
FROM departments
WHERE location_id = 1700);
5. Create a report for HR that displays the last name and salary of every
employee who reports to King.
SELECT last_name, salary FROM employees
WHERE manager_id = (SELECT employee_id
FROM employees
WHERE last_name = 'King');
6. Create a report for HR that displays the department number, last name, and
job ID for every employee in the Executive department.
SELECT department_id, last_name, job_id FROM employees
WHERE department_id IN (SELECT department_id
FROM departments
WHERE department_name =
'Executive');
7. Create a report that displays a list of all employees whose salary is more
than the salary of any employee from department 60.
SELECT last_name FROM employees WHERE salary > ANY (SELECT salary
FROM employees
WHERE department_id=60);
8. Modify above query in display the employee number, last name, and salary of
all employees who earn more than the average salary and who work in a department
with any employee whose last name contains the letter “u.”
SELECT employee_id, last_name, salary FROM employees
WHERE department_id IN (SELECT department_id
FROM employees
WHERE last_name like '%u%') AND salary > (SELECT AVG(salary)
FROM employees);
1. The HR department needs a list of department IDs for departments that do not
contain the job ID ST_CLERK. Use the set operators to create this report.
SELECT department_id FROM departments MINUS
SELECT department_id FROM employees
WHERE job_id = 'ST_CLERK';
3. Produce a list of all the employees who work in departments 50 and 80.
Display the employee ID, job ID, and department ID by using the set operators.
SELECT employee_id, job_id, department_id FROM EMPLOYEES
WHERE department_id=50 UNION ALL
SELECT employee_id, job_id, department_id FROM EMPLOYEES
WHERE department_id=80;
4. Create a report that lists the detail of all employees who are sales
representatives and are currently working in the sales department.
SELECT EMPLOYEE_ID FROM EMPLOYEES
WHERE JOB_ID='SA_REP' INTERSECT
SELECT EMPLOYEE_ID FROM EMPLOYEES
WHERE DEPARTMENT_ID=80;
2. Create an INSERT statement to add the first row of data to the MY_EMPLOYEE
table from the following sample data. Do not list the columns in the INSERT clause.
3. Populate the MY_EMPLOYEE table with the second row of the sample data from
the preceding list. This time, list the columns explicitly in the INSERT clause.
INSERT INTO my_employee (id, last_name, first_name,
userid, salary)
VALUES (2, 'Dancs', 'Betty', 'bdancs', 860);
11. Change the salary to $1,000 for all employees with a salary less than $900.
UPDATE my_employee SET salary = 1000
WHERE salary < 900;
delimiter //
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetEmployeesByManager`(IN p_manager_id
INT)
BEGIN
SELECT employee_id, first_name, last_name, job_id, salary
FROM employees
WHERE manager_id = p_manager_id;
IF ROW_COUNT() = 0 THEN
SELECT 'No employees found under manager ID ' AS Message;
END IF;
END //
delimiter //
DELIMITER $$
IF ROW_COUNT() = 0 THEN
SELECT 'Employee with ID ' AS Message, p_employee_id AS employee_id, ' not
found.' AS Status;
ELSE
SELECT 'Salary updated for employee ID ' AS Message, p_employee_id AS
employee_id;
END IF;
END$$
DELIMITER ;
DELIMITER $$
SELECT COUNT(*)
INTO v_count
FROM employees
WHERE department_id = p_department_id;
IF v_count = 0 THEN
SELECT 'No employees found in department ID ' AS Message, p_department_id
AS department_id;
ELSE
SELECT v_count AS EmployeeCount;
END IF;
END$$
DELIMITER ;
DELIMITER $$
IF ROW_COUNT() = 0 THEN
SELECT 'No employees found with job ID ' AS Message, p_job_id AS job_id;
END IF;
END$$
DELIMITER ;
5. Promote Employee
Objective: Create a stored procedure to promote an employee by changing their job
title and salary.
Input: p_employee_id IN NUMBER, p_new_job_id IN VARCHAR2, p_salary_increase IN
NUMBER
Output: Success or failure message.
Business Logic: The procedure should check if the employee exists, and then update
the job_id and salary based on the provided values. Ensure salary increase is not
less than a certain percentage (e.g., 10%).
DELIMITER $$
IF ROW_COUNT() = 0 THEN
SELECT 'Employee with ID ' AS Message, p_employee_id AS employee_id, ' not
found.' AS Status;
ELSE
SELECT 'Employee ID ' AS Message, p_employee_id AS employee_id, ' promoted
to ' AS NewJob, p_new_job_id AS new_job_id;
END IF;
END$$
DELIMITER ;
DELIMITER $$
CREATE PROCEDURE GetEmployeePayrollReport(IN p_start_date DATE, IN p_end_date DATE)
BEGIN
SELECT e.employee_id, e.first_name, e.last_name, e.salary, b.bonus, (e.salary +
IFNULL(b.bonus, 0)) AS total_pay
FROM employees e
LEFT JOIN bonuses b ON e.employee_id = b.employee_id
WHERE e.hire_date BETWEEN p_start_date AND p_end_date;
IF ROW_COUNT() = 0 THEN
SELECT 'No employees found within the given date range.' AS Message;
END IF;
END$$
DELIMITER ;
CALL GetEmployeePayrollReport('2023-01-01', '2023-12-31');
7. Employee Tenure
Objective: Create a stored procedure that calculates and returns the tenure (in
years) for an employee based on their hire date.
Input: p_employee_id IN NUMBER
Output: Employee's tenure in years.
Business Logic: The procedure should calculate the difference between the current
date and the employee's hire date, returning the number of years the employee has
worked.
DELIMITER $$
CREATE PROCEDURE GetEmployeeTenure(IN p_employee_id INT)
BEGIN
DECLARE v_tenure DECIMAL(5, 2);
IF ROW_COUNT() = 0 THEN
SELECT 'Employee with ID ' AS Message, p_employee_id AS employee_id, ' not
found.' AS Status;
ELSE
SELECT 'Employee ID ' AS Message, p_employee_id AS employee_id, ' has a
tenure of ' AS Tenure, v_tenure AS tenure;
END IF;
END$$
CALL GetEmployeeTenure(104);
8. Department Budget Summary
Objective: Create a stored procedure that calculates and returns the total salary
expense for a department.
Input: p_department_id IN NUMBER
Output: Total salary expense for the department.
Business Logic: The procedure should sum the salaries of all employees in the given
department and return the total salary expense.
DELIMITER $$
CREATE PROCEDURE GetDepartmentBudgetSummary(IN p_department_id INT)
BEGIN
DECLARE v_total_salary DECIMAL(15, 2);
SELECT SUM(salary)
INTO v_total_salary
FROM employees
WHERE department_id = p_department_id;
IF ROW_COUNT() = 0 THEN
SELECT 'No employees found in department ID ' AS Message, p_department_id
AS department_id;
ELSE
SELECT 'Total salary expense for department ' AS Message, p_department_id
AS department_id, ' is ' AS TotalSalary, v_total_salary AS total_salary;
END IF;
END$$
DELIMITER ;
CALL GetDepartmentBudgetSummary(10);
DELIMITER $$
IF ROW_COUNT() = 0 THEN
SELECT 'No employees found with no manager.' AS Message;
END IF;
END$$
DELIMITER ;
CALL GetEmployeesWithNoManager();
DELIMITER $$
DELIMITER ;