0% found this document useful (0 votes)
193 views11 pages

SQL Queries for Employee Data Analysis

This document contains examples of SQL queries and outputs from a series of practice exercises. It includes queries to display employee information based on criteria like last name, calculate salary increases and time employed, use the DECODE function to determine employee grade based on job, count employees by job title, count managers, find employees hired before their managers, filter employees who report to a specific manager, list jobs by department using set operators, and find employees who returned to their original job. The document provides the questions, queries, and expected outputs for each practice.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
193 views11 pages

SQL Queries for Employee Data Analysis

This document contains examples of SQL queries and outputs from a series of practice exercises. It includes queries to display employee information based on criteria like last name, calculate salary increases and time employed, use the DECODE function to determine employee grade based on job, count employees by job title, count managers, find employees hired before their managers, filter employees who report to a specific manager, list jobs by department using set operators, and find employees who returned to their original job. The document provides the questions, queries, and expected outputs for each practice.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

LAB ( EXERCISEs)

PLEASE WRITE THE QUESTION, THE SQL QUERY AND THE OUTPUT IN EVERY EACH QUESTION

PRACTICE 2

13. 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%';
PRACTICE 3

4. Modify your query lab_03_02.sql to add a column that subtracts the old salary from the new salary. Label the column Increase. Save the
contents of the file as lab_03_04.sql. Run the revised query.

SELECT employee_id, last_name, salary, salary + (salary*(15.5/100)) "New Salary", (salary + (salary*(15.5/100))) - salary "Increase"
FROM employees
ORDER BY employee_id;
6. The HR department wants to find the duration of employment for each employee. For each employee, display the last name and calculate
the number of months between today and the date on which the employee was hired. Label the column MONTHS_WORKED. Order your
results by the number of months employed. Round the number of months up to the closest whole number.

SELECT last_name, ROUND(MONTHS_BETWEEN(SYSDATE,hire_date),0) "MONTHS_WORKED"


FROM employees
ORDER BY MONTHS_WORKED;
PRACTICE 4

5. Using the DECODE function, write a query that displays the grade of all employees based on the value of the column JOB_ID, using the
following data:

Job Grade
AD_PRES A
ST_MAN B
IT_PROG C
SA_REP D
ST_CLERK E
None of the above 0

SELECT job_id,
DECODE (job_id, 'AD_PRES', 'A',
'ST_MAN', 'B',
'IT_PROG', 'C',
'SA_REP', 'D',
'ST_CLERK', 'E',
0) "GRADE"
FROM employees;
PRACTICE 5

7. Write a query to display the number of people with the same job.
SELECT JOB_ID,COUNT(JOB_ID)
FROM EMPLOYEES
GROUP BY JOB_ID;
Generalize the query so that the user in the HR department is prompted for a job title
SELECT job_id, COUNT(*)
FROM employees
WHERE job_id = '&job_title'
GROUP BY job_id;

6. Determine the number of managers without listing them. Label the column as Number of Managers.

SELECT COUNT (DISTINCT manager_id) "Number of Managers"


FROM employees;
PRACTICE 6

[Link] HR department needs to find the names and hire dates of all the employees who were hired before their managers, along with their
manager’s names and hire dates.

SELECT e.last_name, TO_CHAR(e.hire_date, 'DD-MON-YY') "HIRE_DATE", m.last_name "LAST_NAME_1", TO_CHAR(m.hire_date, 'DD-MON-YY')


"HIRE_DATE_1"
FROM employees e
JOIN employees m
ON (e.manager_id = m.employee_id)
WHERE e.hire_date < m.hire_date
ORDER BY TO_DATE(HIRE_DATE_1, 'DD-MON-RR');
PRACTICE 7

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
IN (SELECT employee_id
FROM employees
WHERE last_name = 'King')
ORDER BY employee_id;
PRACTICE 8

3. Produce a list of jobs for departments 10, 50, and 20, in that order. Display the job ID and department ID using the set operators.

SELECT DISTINCT job_id, department_id


FROM employees
WHERE department_id = 10
UNION ALL
SELECT DISTINCT job_id, department_id
FROM employees
WHERE department_id = 50
UNION ALL
SELECT DISTINCT job_id, department_id
FROM employees
WHERE department_id = 20;
4. Create a report that lists the employee IDs and job IDs of those employees who currently have a job title that is the same as their job title
when they were initially hired by the company (that is, they changed jobs, but have now gone back to doing their original job).

SELECT employee_id, job_id


FROM employees
INTERSECT
SELECT employee_id, job_id
FROM job_history;

You might also like