INNER JOIN - (PostgreSQL Join)
The INNER JOIN operation is used in PostgreSQL to combine rows from two or more tables based on a related column between them. In this tutorial, we'll discuss the syntax, example, output, explanation, use, important points, and summary of the INNER JOIN operation in PostgreSQL.
Syntax
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
column_name(s)
: The column name or names to select from one or both tables.table1
andtable2
: The names of the tables to join.column_name
: The name of the column that is present in both tables and used as the basis for the join.
Example
Let's take an example of using the INNER JOIN operation to combine data from two tables.
SELECT orders.order_id, customers.customer_name, orders.order_date
FROM orders
INNER JOIN customers
ON orders.customer_id = customers.customer_id;
The output of this query would look something like this:
order_id | customer_name | order_date
---------+------------------+------------
10248 | Alfreds Futterk. | 1996-07-04
10249 | Ana Trujillo Emp. | 1996-07-05
10250 | Antonio Moreno | 1996-07-08
10251 | Around the Horn | 1996-07-08
10252 | Berglunds snabbk. | 1996-07-09
10253 | Blauer See Delikatessen | 1996-07-10
. . .
. . .
. . .
Explanation
In the above example, we used the INNER JOIN operation to combine data from two tables: orders
and customers
. We selected the order_id
and order_date
columns from the orders
table and the customer_name
column from the customers
table.
We then joined the two tables on the customer_id
column, which was present in both tables.
Use
The INNER JOIN operation is used to combine data from two or more related tables into a single result set. This is useful when you need to retrieve data that is spread across multiple tables.
Important Points
- The column used to join the tables should be present in both tables.
- The INNER JOIN operation only returns rows that have matching values in both tables.
- It is possible to join more than two tables using the INNER JOIN operation.
Summary
In this tutorial, we discussed the INNER JOIN operation in PostgreSQL. We covered the syntax, example, output, explanation, use, and important points of the INNER JOIN operation. With this knowledge, you can now use the INNER JOIN operation to combine data from two or more tables into a single result set in your PostgreSQL queries.