Professional Documents
Culture Documents
Exercises on HR Databases
Course Title: Database System Sessional
Course Code: CCE 224
Submitted by
Sadia Islam Shefa
ID No : 2002020
Reg. No: 09537
Level: 2 Semester: 2
Faculty of CSE, PSTU
Exercises on HR Databases
10. Write a query to get the number of jobs available in the employees table.
Answer:
SELECT COUNT(DISTINCT job_id)
FROM employees;
11. Write a query get all first name from employees table in upper case.
Answer:
SELECT UPPER(first_name)
FROM employees;
12. Write a query to get the first 3 characters of first name from employees table.
Answer:
SELECT SUBSTRING(first_name,1,3)
FROM employees;
13. Write a query to calculate 171*214+625.
Answer:
SELECT 171*214+625 Result;
14. Write a query to get the names (for example Ellen Abel, Sundar Ande etc.) of all the
employees from employees table.
Answer:
SELECT CONCAT(first_name,' ', last_name) 'Employee Name'
FROM employees;
15. Write a query to get first name from employees table after removing white spaces from
both side.
Answer:
SELECT TRIM(first_name)
FROM employees;
16. Write a query to get the length of the employee names (first_name, last_name) from
employees table.
Answer:
SELECT first_name,last_name, LENGTH(first_name)+LENGTH(last_name) 'Length of
Names'
FROM employees;
17. Write a query to check if the first_name fields of the employees table contains numbers.
Answer:
SELECT *
FROM employees
WHERE first_name REGEXP '[0-9]';
18. Write a query to select first 10 records from a table.
Answer:
SELECT employee_id, first_name
FROM employees LIMIT 10;
19. Write a query to get monthly salary (round 2 decimal places) of each and every
employee
Note : Assume the salary field provides the 'annual salary' information.
Answer:
SELECT first_name, last_name, round(salary/12,2) as 'Monthly Salary'
FROM employees;
#MySQL Subqueries
1. Write a query to find the name (first_name, last_name) and the salary of the employees
who have a higher salary than the employee whose last_name='Bull'.
Answer:
SELECT FIRST_NAME, LAST_NAME, SALARY
FROM employees
WHERE SALARY >
(SELECT salary FROM employees WHERE last_name = 'Bull');
2. Write a query to find the name (first_name, last_name) of all employees who works in
the IT department.
Answer:
SELECT s1 FROM t1 WHERE s1 = ANY (SELECT s1 FROM t2);
SELECT s1 FROM t1 WHERE s1 IN (SELECT s1 FROM t2);
3. Write a query to find the name (first_name, last_name) of the employees who have a
manager and worked in a USA based department.
Answer:
SELECT first_name, last_name FROM employees
WHERE manager_id in (select employee_id
FROM employees WHERE department_id
IN (SELECT department_id FROM departments WHERE location_id
IN (select location_id from locations where country_id='US')));
3. Write a query to find the name (first_name, last_name) of the employees who are
managers.
Answer:
SELECT first_name, last_name
FROM employees
WHERE (employee_id IN (SELECT manager_id FROM employees));
4. Write a query to find the name (first_name, last_name), and salary of the employees
whose salary is greater than the average salary.
Answer:
SELECT first_name, last_name, salary FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);
5. Write a query to find the name (first_name, last_name), and salary of the employees
whose salary is equal to the minimum salary for their job grade.
Answer:
SELECT first_name, last_name, salary
FROM employees
WHERE employees.salary = (SELECT min_salary
FROM jobs
WHERE employees.job_id = jobs.job_id);
6. Write a query to find the name (first_name, last_name), and salary of the employees
who earns more than the average salary and works in any of the IT departments.
Answer:
SELECT first_name, last_name, salary
FROM employees
WHERE department_id IN
(SELECT department_id FROM departments WHERE department_name LIKE 'IT
%')
AND salary > (SELECT avg(salary) FROM employees);
7. Write a query to find the name (first_name, last_name), and salary of the employees
who earns more than the earning of Mr. Bell.
Answer:
SELECT first_name, last_name, salary
FROM employees
WHERE salary >
(SELECT salary FROM employees WHERE last_name = 'Bell') ORDER BY
first_name;
8. Write a query to find the name (first_name, last_name), and salary of the employees
who earn the same salary as the minimum salary for all departments.
Answer:
SELECT * FROM employees
WHERE salary >
ALL(SELECT avg(salary)FROM employees GROUP BY department_id);
9. Write a query to find the name (first_name, last_name), and salary of the employees
whose salary is greater than the average salary of all departments.
Answer:
SELECT * FROM employees
WHERE salary >
ALL(SELECT avg(salary)FROM employees GROUP BY department_id);
10. Write a query to find the name (first_name, last_name) and salary of the employees
who earn a salary that is higher than the salary of all the Shipping Clerk (JOB_ID =
'SH_CLERK'). Sort the results of the salary of the lowest to highest.
Answer:
SELECT first_name,last_name, job_id, salary
FROM employees
WHERE salary >
ALL (SELECT salary FROM employees WHERE job_id = 'SH_CLERK') ORDER
BY salary;
11. Write a query to find the name (first_name, last_name) of the employees who are not
supervisors.
Answer:
SELECT b.first_name,b.last_name
FROM employees b
WHERE NOT EXISTS (SELECT 'X' FROM employees a WHERE a.manager_id =
b.employee_id);
12. Write a query to display the employee ID, first name, last name, and department
names of all employees.
Answer:
SELECT employee_id, first_name, last_name,
(SELECT department_name FROM departments d
WHERE e.department_id = d.department_id) department
FROM employees e ORDER BY department;
13. Write a query to display the employee ID, first name, last name, salary of all
employees whose salary is above average for their departments.
Answer:
SELECT employee_id, first_name
FROM employees AS A
WHERE salary >
(SELECT AVG(salary) FROM employees WHERE department_id =
A.department_id);
14. Write a query to fetch even numbered records from employees table.
Answer:
SET @i = 0;
SELECT i, employee_id
FROM (SELECT @i := @i + 1 AS i, employee_id FROM employees)
a WHERE MOD(a.i, 2) = 0;
15. Write a query to find the 5th maximum salary in the employees table.
Answer:
SELECT DISTINCT salary
FROM employees e1
WHERE 5 = (SELECT COUNT(DISTINCT salary)
FROM employees e2
WHERE e2.salary >= e1.salary);
16. Write a query to find the 4th minimum salary in the employees table.
Answer:
SELECT DISTINCT salary
FROM employees e1
WHERE 4 = (SELECT COUNT(DISTINCT salary)
FROM employees e2
WHERE e2.salary <= e1.salary);
17. Write a query to select last 10 records from a table.
Answer:
SELECT * FROM (
SELECT * FROM employees ORDER BY employee_id DESC LIMIT 10) sub
ORDER BY employee_id ASC;
18. Write a query to list the department ID and name of all the departments where no
employee is working.
Answer:
SELECT * FROM departments
WHERE department_id
NOT IN (select department_id FROM employees);
19. Write a query to get 3 maximum salaries.
Answer:
SELECT DISTINCT salary
FROM employees a
WHERE 3 >= (SELECT COUNT(DISTINCT salary)
FROM employees b
WHERE b.salary >= a.salary)
ORDER BY a.salary DESC;
20. Write a query to get 3 minimum salaries.
Answer:
SELECT DISTINCT salary
FROM employees a
WHERE 3 >= (SELECT COUNT(DISTINCT salary)
FROM employees b
WHERE b.salary <= a.salary)
ORDER BY a.salary DESC;
21. Write a query to get nth max salaries of employees.
Answer:
SELECT *
FROM employees emp1
WHERE (1) = (
SELECT COUNT(DISTINCT(emp2.salary))
FROM employees emp2
WHERE emp2.salary > emp1.salary);
#MySQL JOINS
1. Write a query to find the addresses (location_id, street_address, city, state_province,
country_name) of all the departments.
Answer:
SELECT location_id, street_address, city, state_province, country_name
FROM locations
NATURAL JOIN countries;
2. Write a query to find the name (first_name, last name), department ID and name of all the
employees.
Answer:
SELECT first_name, last_name, department_id, department_name
FROM employees
JOIN departments USING (department_id);
3. Write a query to find the name (first_name, last_name), job, department ID and name of
the employees who works in London.
Answer:
SELECT e.first_name, e.last_name, e.job_id, e.department_id, d.department_name
FROM employees e
JOIN departments d
ON (e.department_id = d.department_id)
JOIN locations l ON
(d.location_id = l.location_id)
WHERE LOWER(l.city) = 'London';
4. . Write a query to find the employee id, name (last_name) along with their manager_id
and name (last_name).
Answer:
SELECT e.employee_id 'Emp_Id', e.last_name 'Employee',
m.employee_id 'Mgr_Id', m.last_name 'Manager'
FROM employees e
join employees m
ON (e.manager_id = m.employee_id);
5. Write a query to find the name (first_name, last_name) and hire date of the employees
who was hired after 'Jones'.
Answer:
SELECT e.first_name, e.last_name, e.hire_date
FROM employees e
JOIN employees davies
ON (davies.last_name = 'Jones')
WHERE davies.hire_date < e.hire_date;
6. Write a query to get the department name and number of employees in the department.
Answer:
SELECT department_name AS 'Department Name',
COUNT(*) AS 'No of Employees'
FROM departments
INNER JOIN employees
ON employees.department_id = departments.department_id
GROUP BY departments.department_id, department_name
ORDER BY department_name;
7. Write a query to find the employee ID, job title, number of days between ending date and
starting date for all jobs in department 90.
Answer:
WHERE department_id=90;
8. Write a query to display the department ID and name and first name of manager.
Answer:
FROM departments d
ON (d.manager_id = e.employee_id);
9. Write a query to display the department name, manager name, and city.
Answer:
FROM departments d
JOIN employees e
ON (d.manager_id = e.employee_id)
10. Write a query to display the job title and average salary of employees.
Answer:
FROM employees
GROUP BY job_title;
11. Write a query to display job title, employee name, and the difference between salary of
the employee and minimum salary for the job.
Answer:
FROM employees
12. Write a query to display the job history that were done by any employee who is currently
drawing more than 10000 of salary.
Answer:
JOIN employees e
ON (jh.employee_id = e.employee_id)
13. Write a query to display department name, name (first_name, last_name), hire date,
salary of the manager for all managers whose experience is more than 15 years.
Answer:
ON (d.manager_id = e.employee_id)
1. Write a query to display the first day of the month (in datetime format) three months
before the current month.
Answer:
SELECT date(((PERIOD_ADD
(EXTRACT(YEAR_MONTH
FROM CURDATE()),-3)*100)+1));
2. Write a query to display the last day of the month (in datetime format) three months
before the current month.
Answer:
SELECT (SUBDATE(ADDDATE
(CURDATE(),INTERVAL 1 MONTH),
INTERVAL DAYOFMONTH(CURDATE())DAY))
AS LastDayOfTheMonth;
3. Write a query to get the distinct Mondays from hire_date in employees tables.
Answer:
SELECT DISTINCT(STR_TO_DATE
(CONCAT(YEARWEEK(hire_date),'1'),'%x%v%w'))
FROM employees;
Answer:
Answer:
SELECT STR_TO_DATE(CONCAT(12,31,
Answer:
SELECT YEAR(CURRENT_TIMESTAMP) -
YEAR("1967-06-08") -
(RIGHT(CURRENT_TIMESTAMP, 5) <
Answer:
AS 'Current_date';
8. Write a query to get the current date in Thursday September 2014 format.
Answer:
Answer:
10. Write a query to get the DATE value from a given day (number in N).
Answer:
SELECT FROM_DAYS(730677);
11. Write a query to get the first name and hire date from employees table where hire date
between '1987-06-01' and '1987-07-30'
Answer:
FROM employees
WHERE HIRE_DATE
12. Write a query to display the current date in the following format.
Answer:
SELECT date_format(CURDATE(),'%W %D %M %Y %T');
13. Write a query to display the current date in the following format.
Answer:
SELECT date_format(CURDATE(),'%d/%m/%Y');
14. Write a query to display the current date in the following format.
Answer:
15. Write a query to get the firstname, lastname who joined in the month of June.
Answer:
16. Write a query to get the years in which more than 10 employees joined.
Answer:
SELECT DATE_FORMAT(HIRE_DATE,'%Y')
FROM employees
GROUP BY DATE_FORMAT(HIRE_DATE,'%Y')
17. Write a query to get first name of employees who joined in 1987.
Answer:
FROM employees
WHERE YEAR(HIRE_DATE)=1987;
18. Write a query to get department name, manager name, and salary of the manager for all
managers whose experience is more than 5 years.
Answer:
SELECT DEPARTMENT_NAME, FIRST_NAME, SALARY
FROM departments D
JOIN employees E
ON (D.MANAGER_ID=E.MANAGER_ID)
19. Write a query to get employee ID, last name, and date of first salary of the employees.
Answer:
FROM employees;
20. Write a query to get first name, hire date and experience of the employees.
Answer:
FROM employees;
21. Write a query to get the department ID, year, and number of employees joined.
Answer:
COUNT(EMPLOYEE_ID)
FROM employees
ORDER BY DEPARTMENT_ID;
# MySQL string
Answer:
2. Write a query to update the portion of the phone_number in the employees table, within
the phone number the substring '124' will be replaced by '999'.
Answer:
UPDATE employees
3. Write a query to get the details of the employees where the length of the first name
greater than or equal to 8.
Answer:
SELECT *
FROM employees
4. Write a query to display leading zeros before maximum and minimum salary.
Answer:
FROM jobs;
Answer:
6. Write a query to get the employee id, first name and hire month.
Answer:
7. Write a query to get the employee id, email id (discard the last three characters).
Answer:
from employees;
8. Write a query to find all employees where first names are in upper case.
Answer:
Answer:
10. Write a query to get the last word of the street address.
Answer:
AS 'Last--word-of-street_address'
FROM locations;
11. Write a query to get the locations that have minimum street length.
Answer:
FROM locations);
12. Write a query to display the first word from those job titles which contains more than one
words.
Answer:
SELECT job_title, SUBSTR(job_title,1, INSTR(job_title, ' ')-1)
FROM jobs;
13. Write a query to display the length of first name for employees where last name contain
character 'c' after 2nd position.
Answer:
14. Write a query that displays the first name and the length of the first name for all
employees whose name starts with the letters 'A', 'J' or 'M'. Give each column an
appropriate label. Sort the results by the employees' first names.
Answer:
LENGTH(first_name) "Length"
FROM employees
ORDER BY first_name ;
15. Write a query to display the first name and salary for all employees. Format the salary to
be 10 characters long, left-padded with the $ symbol. Label the column SALARY.
Answer:
SELECT first_name,
FROM employees;
16. Write a query to display the first eight characters of the employees' first names and
indicates the amounts of their salaries with '$' sign. Each '$' sign signifies a thousand
dollars. Sort the data in descending order of salary.
Answer:
SELECT left(first_name, 8),
REPEAT('$', FLOOR(salary/1000))
'SALARY($)', salary
FROM employees
17. Write a query to display the employees with their code, first name, last name and hire
date who hired either on seventh day of any month or seventh month in any year.
Answer:
SELECT employee_id,first_name,last_name,hire_date
FROM employees