SELECT select_list FROM table_1 [INNER] JOIN table_2 ON join_condition_1 [[INNER] JOIN table_3 ON...

15
Oracle SQL Inner Joins (aka equijoins)

Transcript of SELECT select_list FROM table_1 [INNER] JOIN table_2 ON join_condition_1 [[INNER] JOIN table_3 ON...

Page 1: SELECT select_list FROM table_1 [INNER] JOIN table_2 ON join_condition_1 [[INNER] JOIN table_3 ON join_condition_3] …

Oracle SQL Inner Joins (aka equijoins)

Page 2: SELECT select_list FROM table_1 [INNER] JOIN table_2 ON join_condition_1 [[INNER] JOIN table_3 ON join_condition_3] …

Inner joins

So far we have written SQL statements against a single table

Oracle is a relational database management system

Relational means the ability to create and maintain relationships between the data (tables)

By “joining” two or more tables, we can exploit these relationships to gain a deeper understanding of the data

An inner join is a type of join based on the equality of values in one or more columns

An inner join is by far the most common form of join

Another term used for inner join is equijoin

Page 3: SELECT select_list FROM table_1 [INNER] JOIN table_2 ON join_condition_1 [[INNER] JOIN table_3 ON join_condition_3] …

The Two-Table Join

We wish to list the course number, course description, section number, location and instructor ID for each section

This will give us a list that shows all sections for each course as well as the instructor for that section and where that particular section is taught (location)

We could

Query each table separately and place them in excel or word, then match them (copy and paste)

Or we could write an SQL statement that would match these tables and provide us with the results

I think we’ll use this approach

Page 4: SELECT select_list FROM table_1 [INNER] JOIN table_2 ON join_condition_1 [[INNER] JOIN table_3 ON join_condition_3] …

Steps to Formulate the SQL Statement

1) First choose the columns you want to include in the result

2) Next, determine the tables to which the columns belong

3) Identify the common columns between the tables

1) Joins are typically used to join between the primary key and the foreign key

2) Joining tables related through a many-to-many relationship yields a Cartesian product

Page 5: SELECT select_list FROM table_1 [INNER] JOIN table_2 ON join_condition_1 [[INNER] JOIN table_3 ON join_condition_3] …

Syntax for the “Explicit” Inner Join

SELECT select_listFROM table_1 [INNER] JOIN table_2 ON join_condition_1 [[INNER] JOIN table_3 ON join_condition_3] …

Page 6: SELECT select_list FROM table_1 [INNER] JOIN table_2 ON join_condition_1 [[INNER] JOIN table_3 ON join_condition_3] …

Syntax for the “Implicit” Inner Join

SELECT select_listFROM table_1, table_2, [, table_3] …WHERE join_condition_1 [AND join_condition_2] …

Page 7: SELECT select_list FROM table_1 [INNER] JOIN table_2 ON join_condition_1 [[INNER] JOIN table_3 ON join_condition_3] …

The SQL StatementExplicit versus Implicit

-- ExplicitSELECT c.course_no, section_no, description, location, instructor_idFROM student.course c INNER JOIN student.section s ON c.course_no = s.course_no -- ImplicitSELECT c.course_no, section_no, description, location, instructor_idFROM student.course c, student.section sWhere c.course_no = s.course_no

Page 8: SELECT select_list FROM table_1 [INNER] JOIN table_2 ON join_condition_1 [[INNER] JOIN table_3 ON join_condition_3] …

The SQL StatementExplain the previous two statements. What are they doing?Execute both of them. What happened?

On either statement, remove the “c” from the Select c.course_no. What happened?Add the “c” back.

Remove the “c” and “s” from the FROM clause. What happened?Fix it without replacing the “c” and “s”. What did you do?

The “c” and “s” are called aliasesSame idea as column name aliases

Qualify all column names with the table alias. Why?

Page 9: SELECT select_list FROM table_1 [INNER] JOIN table_2 ON join_condition_1 [[INNER] JOIN table_3 ON join_condition_3] …

Narrowing Down your Result Table

SELECT c.course_no, section_no, description, location, instructor_idFROM student.course c INNER JOIN student.section s ON c.course_no = s.course_no

Change the above Select statement to return only those courses and their respective sections where the Description column starts with the text “Intro to”

Show me.

Page 10: SELECT select_list FROM table_1 [INNER] JOIN table_2 ON join_condition_1 [[INNER] JOIN table_3 ON join_condition_3] …

Nulls and Joins

In an inner join,

A null value in the common column has the effect of not including the row in the result

Why?

Remember that a null value is not equal to any other value, including another null value

Page 11: SELECT select_list FROM table_1 [INNER] JOIN table_2 ON join_condition_1 [[INNER] JOIN table_3 ON join_condition_3] …

Nulls and Joins

Select instructor_id, zip, last_name, first_nameFrom student.instructor

What did you get? Why?

Select t.instructor_id, t.zip, t.last_name, t.first_nameFrom student.instructor t INNER JOIN student.zipcode z ON t.zip = z.zip

What did you get? Why?

What is the difference between the two queries?

Page 12: SELECT select_list FROM table_1 [INNER] JOIN table_2 ON join_condition_1 [[INNER] JOIN table_3 ON join_condition_3] …

Additional Join Concepts

An inner join (equijoin) only returns rows in which the inner join condition is true

If nulls are found within the column value, this row is not returned

To return rows that contain null values within the column value, you use an outer join

There are various outer joins

Left, right, full

To return all rows of all tables joined, use a Cartesian product

If you do not provide a Where clause to join the tables, the result table will be a Cartesian product

Not usually what you want (can be a bad thing)

Page 13: SELECT select_list FROM table_1 [INNER] JOIN table_2 ON join_condition_1 [[INNER] JOIN table_3 ON join_condition_3] …

Additional Join Concepts We have joined two tables

Can we join three or more tables? Of course!

Using an implicit join, how many Where clauses does it take to create a two table join condition?

One

Where c.course_no = s.course_no

How many Where clauses does it take to create a three table join condition?

Two

Where c.course_no = s.course_no AND s.instructor_id = i.instructor_id

How about 4? 5? 6? Etc?

Page 14: SELECT select_list FROM table_1 [INNER] JOIN table_2 ON join_condition_1 [[INNER] JOIN table_3 ON join_condition_3] …

Practice

Create the SQL to list all students with their complete address (street, city, state, zip)

Include the student’s first and last name

You will need to determine where the data exists (in which table(s) ) and which column(s) you may need to join the table(s) on, if necessary

Create the SQL to list all male instructors by salutation, first name, last name

Create the SQL to list the most expensive course(s)

Do not hard-code the $1595 cost

Use the SQL to find the highest cost dynamically

Page 15: SELECT select_list FROM table_1 [INNER] JOIN table_2 ON join_condition_1 [[INNER] JOIN table_3 ON join_condition_3] …

For March 31st

Subqueries

Quiz

April 2nd

Project #12 due

Review for Exam #2

April 7th

Exam #2, computer-based