0% found this document useful (0 votes)
68 views13 pages

Harsharan Kaur Section 6-1 October 17, 2020

The document contains SQL queries from different sections of a class. The queries join tables from sample databases like Oracle and DJs on Demand to return data like employee and department details. Natural joins, outer joins, and self joins are used to retrieve related data across multiple tables.

Uploaded by

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

Harsharan Kaur Section 6-1 October 17, 2020

The document contains SQL queries from different sections of a class. The queries join tables from sample databases like Oracle and DJs on Demand to return data like employee and department details. Natural joins, outer joins, and self joins are used to retrieve related data across multiple tables.

Uploaded by

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

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;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy