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

DDA Tutorial 3-5

The document describes an entity relationship diagram (ERD) for a human resources database with entities such as employees, departments, jobs, and locations. It then provides 24 SQL queries to retrieve information from the database, including simple queries with filters, joins across multiple tables, aggregate functions, and subqueries. The queries cover a range of reporting needs such as finding employees by name or department, jobs by salary range, aggregate salary and employee counts by department and location, and more.

Uploaded by

骆文乐
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
198 views

DDA Tutorial 3-5

The document describes an entity relationship diagram (ERD) for a human resources database with entities such as employees, departments, jobs, and locations. It then provides 24 SQL queries to retrieve information from the database, including simple queries with filters, joins across multiple tables, aggregate functions, and subqueries. The queries cover a range of reporting needs such as finding employees by name or department, jobs by salary range, aggregate salary and employee counts by department and location, and more.

Uploaded by

骆文乐
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Database Development and Application Tutorial 3-5

The HR Entity Relationship Diagram

DEPARTMENTS LOCATIONS
department_id location_id
department_name street_address
manager_id postal_code
location_id city
state_province
country_id

JOB_HISTORY
employee_id
start_date
end_date EMPLOYEES
job_id employee_id
department_id first_name
last_name COUNTRIES
email country_id
phone_number country_name
hire_date region_id
job_id
salary
commission_pct
manager_id
JOBS department_id
job_id
job_title
min_salary
max_salary REGIONS
region_id
region_name

Write SQL queries to do the following:

Simple queries
1. Show the manager for each department, include relevant information.

SELECT
manager_id,
department_id,
department_name
FROM Department;
2. Show all employees with the first name ‘Alexander’.

SELECT
employee_id,
first_name,
last_name,
email
FROM
Employees
WHERE first_name = ‘Alexander’;
WHERE first_name LIKE ‘Alexander%’;
3. Show all employees belonging to department 170.

SELECT
employee_id,
first_name,
last_name,
department_id
FROM Employees
WHERE (department_id = 170);

4. Show all employees earning less than 10,000 belonging to department 170, ordering the
result from the highest to the lowest salary.

SELECT
employee_id,
first_name,
last_name,
salary
FROM Employees
WHERE (department_id = 170) AND (salary < 10000)
ORDER BY salary DESC(descending order);

5. Show all jobs that pay at least 15,000 salary.

SELECT
job_id,
job_title,
FROM Jobs
WHERE (min_salary >= 15000);

6. List all locations with a street address that has the lucky number “8” in it.

SELECT
location_id,
street_address,
postal_code,
city
FROM Locations
WHERE street_address LIKE ‘%8%’;

7. List all locations with a post code that ends with “18” or “28” or “98”.
SELECT
location_id,
street_address,
postal_code,
city
FROM Locations
WHERE
(postal_code LIKE ‘%18%’
OR
(postal_code LIKE ‘%28%’)
OR
(postal_code LIKE ‘%18%’);

8. List all employees that has been employed for between 5 to 8 years.
SELECT
employees_id,
first_name,
last_name,
email
(
Extract(YEAR FROM sysdate) #display 2 year
Extract(YEAR FROM hire_date)
/
ROUND(MONTH_BETWEEN(sysdate,hire_date)/12,0) AS working_years
#display 1 year
)
FROM Employees
WHERE((MONTH_BETWEEN(sysdate,hire_date)/12 BETWEEN 5 AND 8)
ORDER BY working_years;

Multiple table queries


9. List employees working in the state province of California.
SELECT
E.employee_id,
E.first_name,
E.last_name,
D.department_name
FROM
Employee E JOIN Departments D
ON E.department_id = D.department_id
JOIN Location L
ON D.location_id = L.location_id
WHERE (L.state_province = ‘California’);

10. List employees working in countries beginning with the letter A.


SELECT
C.country_name,
E.employee_id,
E.first_name,
E.last_name
FROM
Employees E JOIN Departments D
ON E.department_id = D.department_id
JOIN Location L
ON D.location_id = L,location.id
JOIN countries C
ON L.country_id = C.country_id
WHERE (C.country_name LIKE ‘A%’);

11. List all employees that had worked as a "SALES REPRESENTATIVE" previously (not
including the current job).
SELECT
Employee_id,
E.first_name,
E.last_name,
JH.start_date,
JH.end_date,
FROM Employees E JOIN Job_history JH
ON Employee_id = JH employee_id
JOIN Jobs J
ON JH.job_id = J.job_id
WHERE (UPPER(J.job_title = ‘SALES REPRESENTATIVE’);

12. Show the salary details for the IT department.


SELECT
J.job_title,
J.min_salary,
J.mas_salary
FROM
Jobs J JOIN Employees E
ON J.job_id = E.job_id
JOIN Departments D
ON E.department_id = D.department_id
WHERE (D.department_name = ‘IT’);

13. List all employees and their manager (must show manager's name)
SELECT
E.employee_id,
E.first_name,
E.last_name,
E.manager_id,
mgr.first_name
FROM
Employees E
JOIN Employees mgr
ON E.manager
ORDER BY E.employee_id;

14. List all employees that work in the same country.


SELECT
E.employee_id,
E.first_name,
E.last_name,
C.country_id,
C.country_name,
L.location_id
FROM Employees E JOIN Departments D
ON.E.department_id = D.department_id
JOIN Location L
ON D.location_id + L.location_id
JOIN countries C
ON L.country_id = C.country_id
ORDER BY C.country_id,L.location_id;

Database Development and Application Tutorial 3-5

Aggregate functions
15. How many employees had been a Stock Clerk previously?
SELECT COUNT(*) AS No_of_Stock_Clerk_Previously
ON E.employee_id = JH employee_id
JOIN Jobs J
ON JH.job_id = J.job_id
WHERE(J.job_title = ‘Stock Clerk’);

16. How many current Stock Clerks are there?


SELECT COUNT(*) AS No_of_Stovk_Clerk
FROM Employees E JOIN Jobs J
ON E.job_id = J.job_id
WHERE (J.job_title = ‘Stock_Clerk’);

17. What is the total salary of all employees in the Marketing department?
SELECT SUM(E.salary)AS Total_Salary
FROM
Employees E JOIN Departments D
ON E.department_id = D.department_id
WHERE (D.department_name = ‘Marketing’);

18. What is the average salary of all the Purchasing Clerk?


SELECT AVG(E.salary) AS Average_Salary_Purchasing_Clerk
FROM
Employees E JOIN Jobs J
ON E.job_id = J.job_id
WHERE (J.job_title = ‘Purchasing Clerk’);
19. How many employees are there in Singapore?
SELECT COUNT(*) AS No_of_employee_Singapore
FROM
Employees E JOIN Departments D
ON E department_id = D.department_id
JOIN Locations L
On D.location_id = L.location_id
JOIN countries C
ON L.country_id = C.country_id
WHERE (C.country_name = ‘Singapore’);

20. What is the total salary of each department located in Singapore?


SELECT
D.department_id,
D.department_name,
SUM(E.salary) AS Total_Dept_salary
FROM
Employees E JOIN Departments D
ON E.department_id = D.deartment_id
JOIN Location L
ON D.location_id = L.location_id
JOIN contries C
ON L.country_id = C.country_id
WHERE(C.Country_name = ‘Singapore’)
GROUP BY D.department_id, D.department_name;

21. What are the departments in Australia that have at least 5 employees?
SELECT
D.department_id,
D.department_name,
COUNT(*) AS No_of_dept_employee
FROM
Employees E JOIN Department D
ON E.department_id = D.department_id
JOIN Location L
ON D.locaation_id = L.location_id
JOIN Countries C
ON L.country_id = C.country_id
WHERE (C.country_name = ‘Australia’)
GROUP BY D.department_id, D.department_name
HAVING COUNT(*) >= 5;

22. How many employees earn more salary than John Russel?
SELECT COUNT (*) AS “No. of employee earned more than John Russel!”
FROM Employee
WHERE salary >
(SELECT salary
FROM Employee
WHERE (first_name = ‘John’)
AND (last_name = ‘RUSSEL!));

Sub-query
23. Identify the employee with the highest salary.
SELECT
employee_id,
first_name,
last_name
FROM Employees
WHERE
salary = (SELECT max(salary)
FROM Employees);

24. Identify the employee for the job that has the highest salary. Include relevant
information.

SELECT
E.emplouyee_id,
E.first_name,
E.last_name,
J.job_id,
J.job_title
FROM
Employee E JOIN Jobs J
ON E.job_id = J.job_id
WHERE E.job_id IN
(SELECT job_id
FROM Jobs
WHERE max_salary = (SELECT MAX(max-salary)FROM JOBS));

You might also like