You are on page 1of 4

LAB EXERCISE 6

Q1: By using natural join, display employees’ last name and their department’s name.

SQL:

SELECT employees.last_name, departments.department_name

FROM employees

NATURAL JOIN departments;

Q2:

For each of this question, use USING and ON clause.

I. Find the first name and department name for all employees in department 60.
II. Find the country’s name and city for each location.
III. Find employees’ name that have salary more than 5000 and display together their job
title.

i.SQL:

SELECT first_name, department_name

FROM employees

JOIN departments USING (department_id)

WHERE department_id = 60
SELECT first_name, department_name

FROM employees

JOIN departments ON employees.department_id = departments.department_id

WHERE departments.department_id = 60;

ii. SQL:

SELECT country_name, city

FROM countries

JOIN locations USING (country_id);

SQL:

SELECT country_name, city

FROM countries c

JOIN locations l ON c.country_id = l.country_id;

iii. SQL:

SELECT first_name, job_title

FROM employees

JOIN jobs USING (job_id)

WHERE salary > 5000;


SQL:

SELECT first_name, job_title

FROM employees e

JOIN jobs j ON e.job_id = j.job_id

WHERE salary > 5000;

Q3:

Find the employees’ last name and their hire date if they were hired after Ernst.

SQL:

SELECT last_name, hire_date

FROM employees

WHERE hire_date > (

SELECT hire_date

FROM employees

WHERE last_name = 'Ernst');

Q4:

Find the manager’s name and their subordinate name.


SQL:

SELECT

CONCAT(m.first_name, CONCAT(' ', m.last_name)) AS manager_name,

CONCAT(s.first_name, CONCAT(' ', s.last_name)) AS subordinate_name

FROM employees m
JOIN employees s ON m.employee_id = s.manager_id

where m.employee_id = s.manager_id;

Q5: Find the employees name, even though they do not have any department. Display together the
department’s name if they have it.

SQL:

SELECT e.first_name, e.last_name, NVL(d.department_name, ' ') as department

FROM employees e

LEFT JOIN departments d ON e.department_id = d.department_id

You might also like