Lab 4
Lab 4
Q1)
Q2)
Q3)
Q4)
Q5)
Q6)
Q7)
Q8)
Q9)
2. Study Self Join and solve the following question:
Display the employee last name and employee id along with their manager’s last
name and manager id. Label the columns as Employee_ID, Emp_LN , Manager_ID,
and Mgr_LN.
Query:
SELECT
e.employee_id AS Employee_ID,
e.last_name AS Emp_LN,
m.employee_id AS Manager_ID,
m.last_name AS Mgr_LN
FROM HR.employees e
LEFT JOIN HR.employees m
ON e.manager_id = m.employee_id;
3. Write a query to display the last name, job, department number, and department name
for all employees who work in Toronto.
Query:
SELECT
e.last_name AS Last_Name,
j.job_title AS Job_Title,
d.department_id AS Department_Number,
d.department_name AS Department_Name
FROM HR.employees e
JOIN HR.departments d ON e.department_id = d.department_id
JOIN HR.locations l ON d.location_id = l.location_id
JOIN HR.jobs j ON e.job_id = j.job_id
WHERE l.city = 'Toronto';
Output:
4. Display the employee full name and department name for all employees who have an
i (lowercase) in their full names.
Query:
SELECT
(e.first_name || ' ' || e.last_name) AS Full_Name,
d.department_name AS Department_Name
FROM HR.employees e
JOIN HR.departments d ON e.department_id = d.department_id
WHERE e.first_name LIKE '%i%'
OR e.last_name LIKE '%i%';
Output:
5. Write a query to display the employee last name, department name, location ID, and
city of all employees who earn a commission.
Query:
SELECT
e.last_name AS Last_Name,
d.department_name AS Department_Name,
l.location_id AS Location_ID,
l.city AS City
FROM HR.employees e
JOIN HR.departments d ON e.department_id = d.department_id
JOIN HR.locations l ON d.location_id = l.location_id
WHERE e.commission_pct IS NOT NULL;
Output:
6. Display the names and hire dates for all employees who were hired before their
managers, along with their manager’s names and hire dates. Label the columns
Employee, Emp_Hired, Manager, and Mgr_Hired. Order the results in descending by
the employee number.
Query:
SELECT (e.first_name || ' ' || e.last_name) AS Employee,
e.hire_date AS Emp_Hired,
(m.first_name || ' ' || m.last_name) AS Manager,
m.hire_date AS Mgr_Hired
FROM HR.Employees e
JOIN HR.Employees m ON e.manager_id = m.employee_id
WHERE e.hire_date < m.hire_date
ORDER BY e.employee_id DESC;
Output: