DDA Tutorial 3-5
DDA Tutorial 3-5
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
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);
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;
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’);
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;
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’);
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’);
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));