Harsharan Kaur
Section 6-1
October 17, 2020
Section 6-1 # 1, 2
1. Create a cross-join that displays the last name and department name from the
employees and departments tables.
SELECT last_name, department_name
FROM employees cross join departments;
2. Create a query that uses a natural join to join the departments table and the
locations table. Display the department id, department name, location id, and city.
SELECT department_id, department_name, location_id, city
FROM departments natural join locations;
Section 6-2 # 1,2,7,8
1. Join the Oracle database locations and department stable using the
location_id column. Limit the results to location 1400 only.
SELECT department_id, department_name, location_id, city
FROM departments JOIN locations USING (location_id)
WHERE location_id = 1400;
2. Join DJs on Demand d_play_list_items, d_track_listings, and d_cds tables with
JOIN USING syntax. Include the song ID, CD number, title, and comments in the
output.
SELECT song_id, cd_number, title, comments
FROM d_play_list_items JOIN d_track_listings USING (song_id) JOIN d_cds USING
(cd_number);
7. Write a statement that displays the employee ID, first name, last name,
manager ID, manager first name, and manager last name for every employee in
the employees table. Hint: this is a self-join.
SELECT e.employee_id, e.first_name, e.last_name, m.manager_id, m.first_name,
m.last_name AS "Manager last name"
FROM employees e JOIN employees m ON (e.manager_id = m.employee_id);
8. Use JOIN ON syntax to query and display the location, city, and department
name for all Canadian locations.
SELECT location_id, city, department_name, country_name
FROM locations JOIN departments USING (location_id) JOIN countries USING
(country_id)
WHERE country_name LIKE 'Canada';
Section 6-3 # 1, 2
1. Return the first name, last name, and department name for all employees
including those employees not assigned to a department.
SELECT e.first_name, e.last_name, d.department_name
FROM employees e left outer join departments d on (e.department_id =
d.department_id);
2. Return the first name, last name, and department name for all employees
including those departments that do not have an employee assigned to
them.
SELECT e.first_name, e.last_name, d.department_name
FROM employees e right outer join departments d on (e.department_id =
d.department_id);
Section 6-4 # 6
6. Create a report that shows the organization chart for the entire employee table.
Write the report so that each level will indent each employee 2 spaces. Since
Oracle Application Express cannot display the spaces in front of the column, use
- (minus) instead.
SELECT LPAD(last_name, LENGTH(last_name)+(LEVEL*2)-2, '-') AS "Org_Chart"
FROM employees
START WITH last_name = 'King'
CONNECT BY PRIOR employee_id = manager_id;
Section 7-1 # 3, 4
3. Write a query to display the title, type, description, and artist from the DJs on
Demand database.
SELECT d_songs.title, d_songs.type_code, d_types.description, d_songs.artist
FROM d_songs, d_types
WHERE d_songs.type_code = d_types.code;
4. Rewrite the query in question 3 to select only those titles with an ID of 47 or 48.
SELECT d_songs.title, d_songs.type_code, d_types.description, d_songs.artist
FROM d_songs, d_types
WHERE d_songs.type_code = d_types.code and d_songs.id in (47, 48);
Section 7-2 # 8, 9
8. Create a query of the Oracle database that shows employee last name,
department IDs, and department names. Include all employees even if they are
not assigned to a department.
SELECT employees.last_name, employees.department_id,
departments.department_name
FROM employees, departments
WHERE employees.department_id = departments.department_id(+);
9. Modify the query in problem 8 to return all the department IDs even if no
employees are assigned to them.
SELECT employees.last_name, employees.department_id,
departments.department_name
FROM employees, departments
WHERE employees.department_id(+) = departments.department_id;