Thursday , September 28 2023

PostgreSQL Inner Join

In today’s article, we will explain the Use of Inner Join, which allows us to see the intersecting data of two tables.

Join and Inner Join mean the same thing, we can get the same result by typing both ways.

Before using Inner Join, we need to find common columns to join the tables.

As seen in the picture below, the common intersection tables are the id and cityid columns, and we can combine them by equating them with each other.

General usage;

Let’s create a database for the inner join below and insert data into it.

We have created our Cities and MeshurYiyecekler tables, now let’s write the plgpsql sentence that will show which dishes are well prepared in which cities.

In the example above, we combined the two tables and wrote the columns of these tables that we want to be seen between the words SELECT and FROM, and we saw which products are famous for which dishes.

If we add a record to the Cities table and do not add the famous food of this city to the MeshurYiyecekler table, this record will not come up during the join process.

For testing, let’s add a record to the Cities table, but not add the famous food of the relevant city to the MeshurYiyecekler table.

As seen above, it does not appear despite adding the province of Gaziantep.

This is because there is no equivalent in the MeshurYiyecekler table.

As you can see, when we ran the INNER JOIN script that we just ran, there was no record of Gaziantep.

The “s” at the end of the Cities table and the “my” at the end of the MeshurYiyecekler table are called alias.

In this way, you can write your scripts in a more understandable way.

As you can see in the script below, if we have defined alias, we need to draw the columns we need by using alias in the select part.

If there is a column with the same name in both tables and you write the name of the column in the select section without defining alias, you will get an error like the one below.

When we query without using Alias like the query above, we get an error when we try to pull the ID column with the same name.

Msg 209, Level 16, State 1, Line 2

Ambiguous column name ‘ID’.


About Faruk Erdem

Leave a Reply

Your email address will not be published. Required fields are marked *