Joining Tables - SQL Training

This is the Joining Tables tutorial of the SQL Training course offered by Simplilearn. In this tutorial, we will learn about Table Aliases and various types of Joins.

Table Aliases

Table aliases are an important feature when it comes to joining multiple tables. It is not relevant if there is only one table, but works only with multiple tables.

Column Aliases

Column aliases have the idea of giving a column, especially a calculated column a sensible, easy, and readable name.

The table aliases have a slightly different idea, which is similar to the idea of simplifying but with a different focus. When we work with multiple tables, we will very quickly have to repeat table names because we have conflicting column names and we want to abbreviate it. So the table aliases typically help to abbreviate this.

Let's have a quick look at our diagram to understand better.

DEMO

https://www.simplilearn.com/ice9/free_resources_article_thumb/table-aliases-examples.JPG

We have a Person table and a Contact table. For person table, we have person ID and for contact table, we have contact pid as our relationship.

If we run the following code:

Select * from Person

Select * from Contact

https://www.simplilearn.com/ice9/free_resources_article_thumb/initial-code-run.JPG

Now, if we introduce a join below,

Select *

From person, contact

Where person.id=contact.pid

https://www.simplilearn.com/ice9/free_resources_article_thumb/inner-joins-example.JPG

We will be introducing Table aliases now and abbreviate the names to reduce the complexity.

https://www.simplilearn.com/ice9/free_resources_article_thumb/intoducing-table-aliases-examples.JPG

Also now we can be more significant in our search. For example, If we just need the first name and the last name from the results.

https://www.simplilearn.com/ice9/free_resources_article_thumb/table-aliases-first-last-name.JPG

We understood table aliases and its usage with examples above. Now we will look into various kinds of Joins.

Looking to know more about SQL Training? Click here!

Inner Joins

One of the most important features in SQL is Inner Joins. We will go back to the same example to understand it better.

DEMO

We will be joining 2 tables first, and further, we will add more tables. Let us join the contact table and the person table.

Before we go ahead, here is an example of Cross Join that we always need to avoid. Cross joins always gives out mixed data or wrongly mapped data.

https://www.simplilearn.com/ice9/free_resources_article_thumb/cross-join-example.JPG

Also, since we have used table aliases above, we will be doing the same with the inner joins example. Not to forget, we can also be more specific in searching out results like first name and last name.

https://www.simplilearn.com/ice9/free_resources_article_thumb/inner-joins-with-table-aliases.JPG

Also, if we want, we can sort out a column in the table above in ascending order. Even the column can be filtered out with only names starting with a particular alphabet.

An explicit version of Inner Joins is shown below.

https://www.simplilearn.com/ice9/free_resources_article_thumb/explicit-version-inner-joins.JPG

https://www.simplilearn.com/ice9/free_resources_article_thumb/video-preview-banner-sql-database-training.jpg

Outer Joins

Just like the Inner joins, Outer joins allows us to consolidate data from multiple tables related to each other into one big report. It brings back together the related data in a way that they're actually readable for the user.

Difference between Inner Joins and Outer Joins

Inner joins show only those records where all elements are present whereas Outer joins show incomplete records as well.

DEMO

We have a person table and a project person table. We will be joining these two tables and want to look at the results that show persons involved in respective projects.

In the case of Inner joins, we can only look at people who have been assigned at least one project. But, in the case of outer joins, we can look at all the people names regardless of a project assigned or not. In such type of cases, outer joins are always helpful.

Let us look at the same example.

We will look at person table, project person table and project table below. We will be using the explicit version of outer joins below.

If we look at the explicit inner joins first, we get results where there is a missing person from the total search results.

https://www.simplilearn.com/ice9/free_resources_article_thumb/inner-explicit-difference.JPG

Now, if we use the outer join below, we will get the result as shown below.

https://www.simplilearn.com/ice9/free_resources_article_thumb/outer-join-explicit-difference.JPG

We can notice that the excluded data also shows up in the results. Also, we can customize our search results using significant codes.

So, we covered Left Join in the example above. Even if we use Right Join, the only difference that shows up is the placement of columns in the search results.

https://www.simplilearn.com/ice9/free_resources_article_thumb/right-join-table-aliases.JPG

There is also Full Join that is a combination of left and right joins and only useful if there are at least 3 tables.

Keen on learning more about the SQL course? Click for the course overview!

Self Joins

Self-joins are inner or outer joins but unlike normal inner and outer joins where we join two or more tables with each other, in a Self Join, we are joining a table with itself.

DEMO:

In cases, where we want a detailed search report from the table itself, we use self-joins. For example, if we want to display different manager names whom every single person reports to within the company, we use Self Joins.

https://www.simplilearn.com/ice9/free_resources_article_thumb/self-join-example.JPG

Also, we can filter the results out by column names as an when required.

https://www.simplilearn.com/ice9/free_resources_article_thumb/joins-using-preferences-self-join.JPG

Self Joins are always helpful, especially when there are hierarchical scenarios in the database.

Complex Joins

In this section, we will look at some more complex joins. We will be joining more tables and we will be performing the inner as well as outer joins.

DEMO

We will extend the inner joins beyond 2 tables. We will look at the contact table, the person table, and the contact type table.

Looking at the same example. We will always be using table aliases in the process. Joining the first two tables looks like below.

https://www.simplilearn.com/ice9/free_resources_article_thumb/complex-joins-example.JPG

Now after we join the third table as well, which is the contact Type.

https://www.simplilearn.com/ice9/free_resources_article_thumb/complex-joins-example-three-tables.JPG

We first joined the person and contact table and then we joined the contact and contact type table. We can add as many tables as possible and the key thing to remember is to add a ‘where’ clause whenever we add one extra table. For example, we need to add 4 ‘where’ clause to add 5 tables.

https://www.simplilearn.com/ice9/free_resources_article_thumb/adding-five-tables-complex-joins.JPG

Also, we can filter out columns as we want by adding codes and removing data that we do not want. We will be adding the project person table as well as the project table to the above tables.

https://www.simplilearn.com/ice9/free_resources_article_thumb/filtering-sorting-table-aliases.JPG

From the above result, we found out that we have duplicate columns that we need to distinguish and so we will be using column aliases.

https://www.simplilearn.com/ice9/free_resources_article_thumb/removing-duplication-data.JPG

Next, we look into using complex outer joins.

https://www.simplilearn.com/ice9/free_resources_article_thumb/outer-complex-join.JPG

Next, we will extend it to more than 2 tables.

https://www.simplilearn.com/ice9/free_resources_article_thumb/outer-complex-join-more-tables.JPG

So we see that we can use all left joins or all right joins or mixed left and right joins and experiment with the results. Now you can add and built as many queries as possible.

Conclusion

With this, we come to an end to the SQL - Joining Tables tutorial.

  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.

Request more information

For individuals
For business
Name*
Email*
Phone Number*
Your Message (Optional)
We are looking into your query.
Our consultants will get in touch with you soon.

A Simplilearn representative will get back to you in one business day.

First Name*
Last Name*
Email*
Phone Number*
Company*
Job Title*