0% found this document useful (0 votes)
724 views2 pages

DP6 Practice Activities - Answers

This document contains practice activities for database joins and hierarchical queries. It includes examples of different types of joins like cross joins, natural joins, inner joins, and outer joins. It also demonstrates self-joins and hierarchical queries using START WITH and CONNECT BY to show employee manager reporting structures. The examples select and filter data from tables like employees, departments, and locations to return related data.

Uploaded by

gery sumual
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
724 views2 pages

DP6 Practice Activities - Answers

This document contains practice activities for database joins and hierarchical queries. It includes examples of different types of joins like cross joins, natural joins, inner joins, and outer joins. It also demonstrates self-joins and hierarchical queries using START WITH and CONNECT BY to show employee manager reporting structures. The examples select and filter data from tables like employees, departments, and locations to return related data.

Uploaded by

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

Practice Activity for DP 6.1 – 6.

4
Gery J. Sumual (01123574291-53)

6.1 Cross Joins and Natural Joins


Vocabulary: CROSS JOIN, NATURAL JOIN
Try It / Solve It:
1. SELECT last_name, department_name
FROM employees CROSS JOIN departments;
2. SELECT department_id, department_name, location_id, city
FROM departments NATURAL JOIN locations;
3. SELECT department_id, department_name, location_id, city
FROM departments NATURAL JOIN locations
WHERE department_ID IN (20,50);
6.2 Join Clauses
Vocabulary: ON clause, USING clause
Try It / Solve It:
1. SELECT*
FROM departments JOIN locations USING (location_id)
WHERE location_id = 1400;
2. SELECT song_id, cd_number, title, comments
FROM d_play_list_items NATURAL JOIN d_track_listings NATURAL JOIN d_cds;
3. SELECT city, department_name, location_id, department_id
FROM departments NATURAL JOIN locations
WHERE department_id IN (10,20,30) AND LOWER(city) = 'seattle';
4. SELECT country_name, region_id, region_name
FROM countries NATURAL JOIN regions
WHERE LOWER(region_name) = 'americas';
5. SELECT first_name, last_name, job_id, job_title, max_salary
FROM employees JOIN jobs USING (job_id)
WHERE max_salary>12000;
6. SELECT job_title, first_name, last_name, email
FROM employees JOIN jobs USING (job_id)
WHERE LOWER(job_title) = 'stock clerk';
7. SELECT e.employee_id, e.first_name, e.last_name, e.manager_id, m.first_name
manager_first_name, m.last_name manager_last_name
FROM employees e JOIN employees m ON (e.manager_id = m.employee_id)
8. SELECT D.location_id, L.city, D.department_name
FROM departments D JOIN locations L ON (D.location_id = L.location_id)
WHERE country_id = 'CA';
9. SELECT epe.manager_id, department_id, department_name, first_name, last_name
FROM employees epe JOIN departments dpt USING(department_id)
WHERE department_id IN (80,90,110,190);
10. SELECT employee_id, last_name, department_id, department_name, hire_date
FROM employees JOIN departments USING(department_id)
WHERE hire_date = '7-Jun-1994';
6.3 Inner versus Outer Joins
Vocabulary: full outer join, outer join, left outer join, right outer join, inner join
Try It / Solve It:
1. SELECT first_name, last_name, department_name
FROM employees LEFT OUTER JOIN departments USING(department_id);
2. SELECT first_name, last_name, department_name
FROM employees RIGHT OUTER JOIN departments USING(department_id)
ORDER BY first_name DESC;
3. SELECT first_name, last_name, department_name
FROM employees FULL OUTER JOIN departments USING(department_id);
4. SELECT first_name, last_name, event_date, description
FROM d_clients LEFT OUTER JOIN d_events USING(client_number);
5. SELECT description, shift_assign_date
FROM f_shifts NATURAL LEFT OUTER JOIN f_shift_assignments;
6.4 Self Joins and Hierarchical Queries
Vocabulary: self-join, hierarchical queries, LEVEL, START WITH, CONNECT BY PRIOR
Try It / Solve It:
1. SELECT epe.last_name "Employee", epe.employee_id "Emp#", mgr.last_name "Manager",
mgr.employee_id "Mgr#"
FROM employees epe JOIN employees mgr ON (epe.manager_id = mgr.employee_id);
2. SELECT epe.last_name "Employee", epe.employee_id "Emp#", mgr.last_name "Manager",
mgr.employee_id "Mgr#"
FROM employees epe LEFT OUTER JOIN employees mgr ON (epe.manager_id =
mgr.employee_id)
ORDER BY epe.last_name DESC;
3. SELECT epe.first_name||' '||epe.last_name AS "Employee", epe.hire_date AS "Emp Hired",
mgr.first_name||' '||mgr.last_name AS "Manager", mgr.hire_date AS "Mgr_Hired"
FROM employees epe LEFT OUTER JOIN employees mgr ON (epe.manager_id =
mgr.employee_id)
WHERE epe.hire_date < mgr.hire_date;
4. SELECT last_name, salary, department_id
FROM employees
START WITH LOWER(first_name||' '||last_name) = 'lex de haan'
CONNECT BY PRIOR employee_id = employees.manager_id;
5. The tree-walk start with employee with the last name King who is in the top of the
oraganizational hierarchy that he’s the president, but the hierarchy clause, CONNECT BY
PRIOR manager_id = employee_id, implied that it’s a bottom up walk which in King’s case
would end up yielding no hierarchy structure at all.
6. SELECT LPAD(last_name, LENGTH(last_name) + 2*(LEVEL-1), '-') "ORGANIZATION CHART"
FROM employees
START WITH last_name = 'King'
CONNECT BY PRIOR employee_id = manager_id;
7. SELECT LPAD(last_name, LENGTH(last_name) + 2*(LEVEL-1), '-') "ORGANIZATION CHART"
FROM employees
START WITH last_name = 'King'
CONNECT BY PRIOR employee_id = manager_id and last_name <> 'De Haan';

You might also like