0% found this document useful (0 votes)
3 views

Lesson 2 Join.pptx

This document provides an overview of the MySQL JOIN clause, detailing how to combine data from multiple tables using different types of joins: CROSS, INNER, LEFT, and RIGHT. It explains the creation of a demo database with two tables, users and posts, and illustrates how each join type operates in practice. Additionally, it discusses the impact of WHERE conditions on JOIN results and provides examples of SQL commands for various join scenarios.

Uploaded by

Hurjay Naguit
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lesson 2 Join.pptx

This document provides an overview of the MySQL JOIN clause, detailing how to combine data from multiple tables using different types of joins: CROSS, INNER, LEFT, and RIGHT. It explains the creation of a demo database with two tables, users and posts, and illustrates how each join type operates in practice. Additionally, it discusses the impact of WHERE conditions on JOIN results and provides examples of SQL commands for various join scenarios.

Uploaded by

Hurjay Naguit
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

MySQL JOIN

Lesson 2
SQL
The JOIN clause allows you to combine the data from 2 or more tables into one
result set. As we will be selecting from multiple columns, we need to include the
list of the columns we want to choose data from after the FROM clause is
separated by a comma.
In this chapter, we will go over the following JOIN types:

• CROSS Join
• INNER Join
• LEFT Join
• RIGHT Join

Before we get started, let's create a new database and two tables that we are going
to work with:
MySQL JOINS
• We are going to call the database demo_joins:

• Then, switch to the new database:

• Then, the first table will be called users, and it will only
have two columns: id and username:
MySQL JOINS
• Then, let's create a second table called posts, and to keep things
simple, we will have three two columns: id, user_id and title:

• The user_id column would be used to reference the user's ID


that the post belongs to. It is going to be a one to many
relations, e.g. one user could have many posts:
MySQL JOINS
• Now, let's add some data into the two tables first by creating a few
users:

• And finally add some posts:


CROSS JOINS
The CROSS join allows you to put the result of two tables next to each
other without specifying any WHERE conditions. This makes the CROSS
join the simplest one, but it is also not of much use in a real-life scenario.
INNER JOINS
The INNER join is used to join two tables. However, unlike the CROSS
join, by convention, it is based on a condition. By using an INNER join,
you can match the first table to the second one.
Rundown of the query:

• SELECT * FROM users: This is a standard select we've covered


many times in the previous chapters.

• INNER JOIN posts: Then, we specify the second table and


which table we want to join the result set.

• ON users.id = posts.user_id: Finally, we specify how we want


the data in these two tables to be merged. The user.id is the
id column of the user table, which is also the primary ID, and
posts.user_id is the foreign key in the email address table
referring to the ID column in the users table
LEFT JOINS
Using the LEFT OUTER join, you would get all rows from the first table
that you've specified, and if there are no associated records within the
second table, you will get a NULL value.
RIGHT JOINS
The RIGHT OUTER join is the exact opposite of the LEFT OUTER join. It
will display all of the rows from the second table and give you a NULL
value in case that it does not match with an entry from the first table.
Let's create a post that does not have a matching user id:

We specify 123 as the user ID, but we don't have such a user in our
users table.

Now, if you were to run the LEFT outer join, you would not see the
post as it has a null value for the corresponding users table.

But if you were to run a RIGHT outer join, you would see the post
but not the greisi user as it does not have any posts:
RIGHT JOINS
OUTPUT:
JOIN TABLE
Joins can also be limited with WHERE conditions. For instance, in the
preceding example, if we wanted to join the tables and then restrict to only
username bobby.
The Impact of Condition in JOIN vs. WHERE Clauses
The placement of conditions within a SQL query, specifically in the JOIN vs.
the WHERE clause, can yield different results.

Take a look at the following example, which retrieves POSTS containing the
word "SQL" along with their associated user data:
The Impact of Condition in JOIN vs. WHERE Clauses
However, by shifting the condition to the JOIN clause, all users are displayed,
but only posts with titles containing "SQL" are included:
MySQL JOIN EXAMPLE

Here, the SQL command selects


customer_id and first_name columns
(from the Customers table) and the
amount column (from the Orders table).

And, the result set will contain those rows


where there is a match between
customer_id (of the Customers table)
and customer (of the Orders table).
MySQL JOIN EXAMPLE
MySQL JOIN EXAMPLE
MySQL RIGHT JOIN EXAMPLE
Here, the SQL command selects
customer_id and first_name columns
(from the Customers table) and the
amount column (from the Orders table).

And, the result set will contain those


rows where there is a match between
customer_id (of the Customers table)
and customer (of the Orders table)
along with all the remaining rows from
the Orders table.
MySQL RIGHT JOIN EXAMPLE
MySQL RIGHT JOIN EXAMPLE
MySQL LEFT JOIN EXAMPLE
Here, the SQL command selects
customer_id and first_name columns
(from the Customers table) and the
amount column (from the Orders table).

And, the result set will contain those


rows where there is a match between
customer_id (of the Customers table)
and customer (of the Orders table)
along with all the remaining rows from
the Customers table.
MySQL LEFT JOIN EXAMPLE

You might also like