Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
expand_more
expand_more
format_list_bulleted
Concept explainers
Question
My SQL WORKBENCH
**Please note that you may actually use as many tables as you need. You are not limited to use only two
tables.
Example:
SELECT *
FROM DEPARTMENTS;
SELECT *
FROM EMPLOYEES;
Now join departments and employees tables.
SELECT FIRST_NAME, DEPARTMENT_NAME
FROM EMPLOYEES, DEPARTMENTS
WHERE EMPLOYEES.DEPARTMENT_ID = DEPARTMENTS.DEPARTMENT_ID;
NOTE: If you omit the join condition, the result will be the Cartesian Product of the two tables. A Cartesian
Product joins all rows of the first table with all rows of the second table.
That is if the first table has n rows and the second table has m rows, the output will have n*m rows.
The following query will produce a Cartesian Product:
SELECT FIRST_NAME, DEPARTMENT_NAME
FROM EMPLOYEES, DEPARTMENTS;
**When table names are long, qualifying column names might be very time consuming. Usually people
prefer to use table aliases for this purpose.
Example: List the name of all employees who work for 'Purchasing' department
SELECT E.FIRST_NAME "EMPLOYEE NAME"
FROM EMPLOYEES E, DEPARTMENT D
WHERE E.DEPARTMENT_ID = D.DEPARTMENT_ID AND
UPPER(D.DEPARTMENT_NAME) = 'PURCHASING';
**Table joins can be performed on columns that are not primary or foreign keys as well.
SELECT E.FIRST_NAME
FROM EMPLOYEES A, EMPLOYEES B
WHERE A.SALARY = B.SALARY AND A.EMPLOYEE_ID != B.EMPLOYEE_ID;
tables.
Example:
SELECT *
FROM DEPARTMENTS;
SELECT *
FROM EMPLOYEES;
Now join departments and employees tables.
SELECT FIRST_NAME, DEPARTMENT_NAME
FROM EMPLOYEES, DEPARTMENTS
WHERE EMPLOYEES.DEPARTMENT_ID = DEPARTMENTS.DEPARTMENT_ID;
NOTE: If you omit the join condition, the result will be the Cartesian Product of the two tables. A Cartesian
Product joins all rows of the first table with all rows of the second table.
That is if the first table has n rows and the second table has m rows, the output will have n*m rows.
The following query will produce a Cartesian Product:
SELECT FIRST_NAME, DEPARTMENT_NAME
FROM EMPLOYEES, DEPARTMENTS;
**When table names are long, qualifying column names might be very time consuming. Usually people
prefer to use table aliases for this purpose.
Example: List the name of all employees who work for 'Purchasing' department
SELECT E.FIRST_NAME "EMPLOYEE NAME"
FROM EMPLOYEES E, DEPARTMENT D
WHERE E.DEPARTMENT_ID = D.DEPARTMENT_ID AND
UPPER(D.DEPARTMENT_NAME) = 'PURCHASING';
**Table joins can be performed on columns that are not primary or foreign keys as well.
SELECT E.FIRST_NAME
FROM EMPLOYEES A, EMPLOYEES B
WHERE A.SALARY = B.SALARY AND A.EMPLOYEE_ID != B.EMPLOYEE_ID;
**All these joins are called equijoin because they use an equality to form the join. It is also possible to form
nonequijoins. These are joins that use an operator (+) than other.
Normally if a row does not satisfy a join condition, it will be left out of the result. You want to see rows of a
table that do not satisfy the join condition as well, you have to perform an outer join. Outer join is performed
by placing a (+) sign on the right hand side of one of the attribute of join condition.
The (+) sign makes it possible to join a 'NULL' row of the table it is close to with rows from the other
table. In other words (+) will allow NULL rows on the side that is placed.
Syntax of an outer join:
SELECT table1.column11, table1.column12, ..., table2.column21, table2.column22
FROM table1, table2
WHERE table1.column11(+) = table2.column21(+);
**Please be careful, you can use only one (+) in the join condition.
Outer Examples:
Example1: Display last name of all employees with their department information (department id and department
name) including those employees who does not work for any department.
SELECT e.last_name,d.department_id,d.department_name
FROM employees e, departments d
WHERE e.department_id=d.department_id(+);
Example2: Display last name of all employees with their department information (department id and
department name) including those departments that they do not have any employee.
SELECT e.last_name,d.department_id,d.department_name
FROM employees e, departments d
WHERE e.department_id(+)=d.department_id;
SELF JOIN
Joining a Table to Itself:
Sometimes you need to join a able to itself.
To find the name of each employee's manager, you need to join the EMPLOYEES table to itself.
Example:Find the name of the managers for all employees.
SELECT e.last_name,m.last_name
FROM employees e, employees m
WHERE .manager_id=m.employee_id;
SQL> desc locations Name Null? Type
----------------------------------------- --------
----------------------------
LOCATION_ID NOT NULL NUMBER(6)
DEPARTMENT_ID NOT NULL NUMBER(6)
ADDRESS VARCHAR2(25)
CITY NOT NULL VARCHAR2(25)
STATE NOT NULL VARCHAR2(25)
COUNTRY VARCHAR2(20)
nonequijoins. These are joins that use an operator (+) than other.
Normally if a row does not satisfy a join condition, it will be left out of the result. You want to see rows of a
table that do not satisfy the join condition as well, you have to perform an outer join. Outer join is performed
by placing a (+) sign on the right hand side of one of the attribute of join condition.
The (+) sign makes it possible to join a 'NULL' row of the table it is close to with rows from the other
table. In other words (+) will allow NULL rows on the side that is placed.
Syntax of an outer join:
SELECT table1.column11, table1.column12, ..., table2.column21, table2.column22
FROM table1, table2
WHERE table1.column11(+) = table2.column21(+);
**Please be careful, you can use only one (+) in the join condition.
Outer Examples:
Example1: Display last name of all employees with their department information (department id and department
name) including those employees who does not work for any department.
SELECT e.last_name,d.department_id,d.department_name
FROM employees e, departments d
WHERE e.department_id=d.department_id(+);
Example2: Display last name of all employees with their department information (department id and
department name) including those departments that they do not have any employee.
SELECT e.last_name,d.department_id,d.department_name
FROM employees e, departments d
WHERE e.department_id(+)=d.department_id;
SELF JOIN
Joining a Table to Itself:
Sometimes you need to join a able to itself.
To find the name of each employee's manager, you need to join the EMPLOYEES table to itself.
Example:Find the name of the managers for all employees.
SELECT e.last_name,m.last_name
FROM employees e, employees m
WHERE .manager_id=m.employee_id;
SQL> desc locations Name Null? Type
----------------------------------------- --------
----------------------------
LOCATION_ID NOT NULL NUMBER(6)
DEPARTMENT_ID NOT NULL NUMBER(6)
ADDRESS VARCHAR2(25)
CITY NOT NULL VARCHAR2(25)
STATE NOT NULL VARCHAR2(25)
COUNTRY VARCHAR2(20)
EXERCISES
PART A:
1-Display the addresses of all the departments. Show locationID, street address,city,state and country name in the
output. (Use Locations table)
2-Display the last name, department number and deparment name for all employees.
3-Display the last name, job, department number, and department name for all employees work in
Toronto. (Employees,Departments,Locations)
PART A:
1-Display the addresses of all the departments. Show locationID, street address,city,state and country name in the
output. (Use Locations table)
2-Display the last name, department number and deparment name for all employees.
3-Display the last name, job, department number, and department name for all employees work in
Toronto. (Employees,Departments,Locations)
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution
Trending nowThis is a popular solution!
Step by stepSolved in 3 steps
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- Intersection table is needed when you have a relationship 1:1 M:N M:1 1:Marrow_forwardSQL based questionarrow_forwardSQL The sales person gets a bonus pay if their amount exceeds a certain tier level for each year. Write a query that shows who would be getting a bonus and for how much. 2. How many rows will it return and with what data?arrow_forward
- In SQL This sample database consists of the following tables(see image for tables and there is one more at the bottom in this text):• Customers: stores customer’s data• Products: stores a list of scale model cars• ProductLines: stores a list of product line categories• Orders: stores sales orders placed by customers• OrderDetails: stores sales order line items for each sales order• Payments: stores payments made by customers based on their accounts• Employees: stores all employee information as well as the organization structuresuch as who reports to whom• Offices: stores sales office data Write SQL code for the following:We want to add a new sale order for the customer (customerNumber = 145) in thedatabase. The steps of adding a sale order are described as follows:(1) Get latest sale order number from “orders” table, and use the next sale ordernumber as the new sale order number(2) Insert a new sale order into “orders” table for the customer (customerNumber =145). For this order, the…arrow_forwardWrite a SQL code that returns the name of aircraft (MOD_NAME in MODEL table), number of trips each mode flied (based on the CHARTER table, name this Trip_Num), and Total distance of each mode (based on the CHARTER table, name this Total_Distance).arrow_forwardWrite two SQL Queries that will show the ORDER# from both the Orderand OrderItems table using the UNION set operatorarrow_forward
arrow_back_ios
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education