0% found this document useful (0 votes)
9 views

SQL Queries for Employee

The document contains a series of SQL queries designed to extract information from an employee and department database. It includes queries using JOINs to retrieve employee details along with department information, as well as subqueries to analyze employee salaries and job roles. The queries cover various scenarios, such as filtering by department, salary ranges, and specific employee attributes.

Uploaded by

Imran Omar Omari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

SQL Queries for Employee

The document contains a series of SQL queries designed to extract information from an employee and department database. It includes queries using JOINs to retrieve employee details along with department information, as well as subqueries to analyze employee salaries and job roles. The queries cover various scenarios, such as filtering by department, salary ranges, and specific employee attributes.

Uploaded by

Imran Omar Omari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

SQL Queries for Employee and Department Database

1) Queries with JOINs

a) First name, last name, department number, and department name for each employee

SELECT e.FIRST_NAME, e.LAST_NAME, e.DEPARTMENT_ID, d.DEPARTMENT_NAME


FROM employees e
JOIN departments d ON e.DEPARTMENT_ID = d.DEPARTMENT_ID;

b) First and last name, department, city, and state province for each employee

SELECT e.FIRST_NAME, e.LAST_NAME, d.DEPARTMENT_NAME, l.CITY, l.STATE_PROVINCE


FROM employees e
JOIN departments d ON e.DEPARTMENT_ID = d.DEPARTMENT_ID
JOIN locations l ON d.LOCATION_ID = l.LOCATION_ID;

c) Employees from departments 80 or 40

SELECT e.FIRST_NAME, e.LAST_NAME, e.DEPARTMENT_ID, d.DEPARTMENT_NAME


FROM employees e
JOIN departments d ON e.DEPARTMENT_ID = d.DEPARTMENT_ID
WHERE e.DEPARTMENT_ID IN (80, 40);

d) Employees whose first name contains 'z'

SELECT e.FIRST_NAME, e.LAST_NAME, d.DEPARTMENT_NAME, l.CITY, l.STATE_PROVINCE


FROM employees e
JOIN departments d ON e.DEPARTMENT_ID = d.DEPARTMENT_ID
JOIN locations l ON d.LOCATION_ID = l.LOCATION_ID
WHERE e.FIRST_NAME LIKE '%z%';

e) All departments, including those without employees

SELECT d.DEPARTMENT_NAME, e.FIRST_NAME, e.LAST_NAME


FROM departments d
LEFT JOIN employees e ON d.DEPARTMENT_ID = e.DEPARTMENT_ID;

f) Employees working in the same department as 'Taylor'

SELECT e.FIRST_NAME, e.LAST_NAME, e.DEPARTMENT_ID


FROM employees e
WHERE e.DEPARTMENT_ID = (SELECT DEPARTMENT_ID FROM employees WHERE LAST_NAME =
'Taylor');

g) Department name, average salary, and number of employees who got a commission

SELECT d.DEPARTMENT_NAME, AVG(e.SALARY) AS Avg_Salary, COUNT(e.EMPLOYEE_ID) AS


Num_Employees
FROM employees e
JOIN departments d ON e.DEPARTMENT_ID = d.DEPARTMENT_ID
WHERE e.COMMISSION_PCT IS NOT NULL
GROUP BY d.DEPARTMENT_NAME;

h) Job title and average salary

SELECT j.JOB_TITLE, AVG(e.SALARY) AS Avg_Salary


FROM employees e
JOIN jobs j ON e.JOB_ID = j.JOB_ID
GROUP BY j.JOB_TITLE;

i) Country name, city, and number of departments with at least 2 employees

SELECT c.COUNTRY_NAME, l.CITY, COUNT(d.DEPARTMENT_ID) AS Num_Departments


FROM departments d
JOIN locations l ON d.LOCATION_ID = l.LOCATION_ID
JOIN countries c ON l.COUNTRY_ID = c.COUNTRY_ID
WHERE d.DEPARTMENT_ID IN (
SELECT e.DEPARTMENT_ID FROM employees e GROUP BY e.DEPARTMENT_ID HAVING
COUNT(e.EMPLOYEE_ID) >= 2
)
GROUP BY c.COUNTRY_NAME, l.CITY;

2) Queries with Subqueries

j) Employees who earn more than employee ID 163

SELECT FIRST_NAME, LAST_NAME


FROM employees
WHERE SALARY > (SELECT SALARY FROM employees WHERE EMPLOYEE_ID = 163);

k) Employees in the same job as employee ID 169

SELECT FIRST_NAME, LAST_NAME, SALARY, DEPARTMENT_ID, JOB_ID


FROM employees
WHERE JOB_ID = (SELECT JOB_ID FROM employees WHERE EMPLOYEE_ID = 169);

l) Employees earning the lowest salary in any department

SELECT FIRST_NAME, LAST_NAME, SALARY, DEPARTMENT_ID


FROM employees
WHERE SALARY = (SELECT MIN(SALARY) FROM employees GROUP BY DEPARTMENT_ID);

m) Employees reporting to 'Payam'

SELECT e.FIRST_NAME, e.LAST_NAME, e.EMPLOYEE_ID, e.SALARY


FROM employees e
WHERE e.MANAGER_ID = (SELECT EMPLOYEE_ID FROM employees WHERE FIRST_NAME = 'Payam');

n) Employees whose salary is within the range of the lowest salary and 2500

SELECT * FROM employees


WHERE SALARY BETWEEN (SELECT MIN(SALARY) FROM employees) AND 2500;
o) Employees not in departments with managers having ID between 100 and 200

SELECT * FROM employees


WHERE DEPARTMENT_ID NOT IN (
SELECT DISTINCT DEPARTMENT_ID FROM employees WHERE MANAGER_ID BETWEEN 100 AND
200
);

p) Employees working in the same department as anyone with 'T' in their name

SELECT EMPLOYEE_ID, FIRST_NAME, LAST_NAME


FROM employees
WHERE DEPARTMENT_ID IN (
SELECT DISTINCT DEPARTMENT_ID FROM employees WHERE FIRST_NAME LIKE '%T%'
);

q) Employees earning more than the average salary and working with an employee with 'J' in their name

SELECT EMPLOYEE_ID, FIRST_NAME, LAST_NAME, SALARY


FROM employees
WHERE SALARY > (SELECT AVG(SALARY) FROM employees)
AND DEPARTMENT_ID IN (
SELECT DISTINCT DEPARTMENT_ID FROM employees WHERE FIRST_NAME LIKE '%J%'
);

r) Employees earning less than anyone in 'MK_MAN' job

SELECT e.EMPLOYEE_ID, e.FIRST_NAME, e.LAST_NAME, j.JOB_TITLE


FROM employees e
JOIN jobs j ON e.JOB_ID = j.JOB_ID
WHERE e.SALARY < (SELECT MIN(SALARY) FROM employees WHERE JOB_ID = 'MK_MAN');

s) Total salary per department where at least one employee exists

SELECT DEPARTMENT_ID, SUM(SALARY) AS Total_Salary


FROM employees
GROUP BY DEPARTMENT_ID
HAVING COUNT(EMPLOYEE_ID) > 0;

t) Employees earning above average and working in IT departments

SELECT e.FIRST_NAME, e.LAST_NAME, e.SALARY, d.DEPARTMENT_NAME


FROM employees e
JOIN departments d ON e.DEPARTMENT_ID = d.DEPARTMENT_ID
WHERE e.SALARY > (SELECT AVG(SALARY) FROM employees)
AND d.DEPARTMENT_NAME LIKE '%IT%';

u) Employees earning the max salary among those who joined between Jan 1, 2002, and Dec 31, 2003

SELECT e.EMPLOYEE_ID, e.FIRST_NAME, e.LAST_NAME, e.SALARY, d.DEPARTMENT_NAME, l.CITY


FROM employees e
JOIN departments d ON e.DEPARTMENT_ID = d.DEPARTMENT_ID
JOIN locations l ON d.LOCATION_ID = l.LOCATION_ID
WHERE e.SALARY = (
SELECT MAX(SALARY) FROM employees
WHERE HIRE_DATE BETWEEN '2002-01-01' AND '2003-12-31'
);

You might also like