The Wayback Machine - https://web.archive.org/web/20240906223607/https://www.geeksforgeeks.org/postgresql-inner-join/
Open In App

PostgreSQL – INNER JOIN

Last Updated : 19 Sep, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In PostgreSQL the INNER JOIN keyword selects all rows from both the tables as long as the condition satisfies. This keyword will create the result-set by combining all rows from both the tables where the condition satisfies i.e value of the common field will be the same.

Syntax:
SELECT table1.column1, table1.column2, table2.column1, ....
FROM table1 
INNER JOIN table2
ON table1.matching_column = table2.matching_column;


table1: First table.
table2: Second table
matching_column: Column common to both the tables.

Let’s analyze the above syntax:

  • Firstly, using the SELECT statement we specify the tables from where we want the data to be selected.
  • Second, we specify the main table.
  • Third, we specify the table that the main table joins to.

The below Venn Diagram illustrates the working of PostgreSQL INNER JOIN clause:

For the sake of this article we will be using the sample DVD rental database, which is explained here .

Now, let’s look into a few examples.

Example 1:
Here we will be joining the “customer” table to “payment” table using the INNER JOIN clause.

SELECT
    customer.customer_id,
    first_name,
    last_name,
    email,
    amount,
    payment_date
FROM
    customer
INNER JOIN payment ON payment.customer_id = customer.customer_id;

Output:

Example 2:

Here we will be joining the “customer” table to “payment” table using the INNER JOIN clause and sort them with the ORDER BY clause:

SELECT
    customer.customer_id,
    first_name,
    last_name,
    email,
    amount,
    payment_date
FROM
    customer
INNER JOIN payment ON payment.customer_id = customer.customer_id
ORDER BY
    customer.customer_id;

Output:

Example 3:
Here we will be joining the “customer” table to “payment” table using the INNER JOIN clause and filter them with the WHERE clause:

SELECT
    customer.customer_id,
    first_name,
    last_name,
    email,
    amount,
    payment_date
FROM
    customer
INNER JOIN payment ON payment.customer_id = customer.customer_id
WHERE
    customer.customer_id = 15;


Output:

Example 4:
Here we will establish the relationship between three tables: staff, payment, and customer using the INNER JOIN clause.

SELECT
    customer.customer_id,
    customer.first_name customer_first_name,
    customer.last_name customer_last_name,
    customer.email,
    staff.first_name staff_first_name,
    staff.last_name staff_last_name,
    amount,
    payment_date
FROM
    customer
INNER JOIN payment ON payment.customer_id = customer.customer_id
INNER JOIN staff ON payment.staff_id = staff.staff_id;

Output:


Previous Article
Next Article

Similar Reads

PostgreSQL - SELF JOIN
PostgreSQL has a special type of join called the SELF JOIN which is used to join a table with itself. It comes in handy when comparing the column of rows within the same table. As, using the same table name for comparison is not allowed in PostgreSQL, we use aliases to set different names of the same table during self-join. It is also important to
3 min read
PostgreSQL - FULL OUTER JOIN
PostgreSQL's FULL OUTER JOIN, also known as FULL JOIN, combines the results of both LEFT JOIN and RIGHT JOIN. This join type retrieves all rows from both tables involved, including unmatched rows where applicable, filling in NULL values for columns that do not have a match. Let us get a better understanding of the FULL OUTER JOIN in PostgreSQL from
2 min read
PostgreSQL - LEFT JOIN
The PostgreSQL LEFT JOIN, also known as LEFT OUTER JOIN, is a powerful tool for combining rows from two or more tables. It returns all rows from the table on the left side of the join and the matching rows from the table on the right side. For rows in the left table with no corresponding row in the right table, the result set will include NULL valu
3 min read
Inner reducing pattern printing
Given a number N, print the following pattern. Examples : Input : 4 Output : 4444444 4333334 4322234 4321234 4322234 4333334 4444444 Explanation: (1) Given value of n forms the outer-most rectangular box layer. (2) Value of n reduces by 1 and forms an inner rectangular box layer. (3) The step (2) is repeated until n reduces to 1. Input : 3 Output :
5 min read
Comparison of yield(), join() and sleep() in Java
Comparison table yield(), join(), sleep() propertyyield()join()sleep()purposeIf a thread wants to pass its execution to give chance to remaining threads of same priority then we should go for yield()If a thread wants to wait until completing of some other thread then we should go for join() If a thread does not want to perform any operation for a p
1 min read
PostgreSQL - ORDER BY clause
The PostgreSQL ORDER BY clause is used to sort the result query set returned by the SELECT statement. As the query set returned by the SELECT statement has no specific order, one can use the ORDER BY clause in the SELECT statement to sort the results in the desired manner. Syntax: SELECT column_1, column_2 FROM table_name ORDER BY column_1 [ASC | D
2 min read
PostgreSQL - NOT BETWEEN operator
PostgreSQL NOT BETWEEN operator is used to match all values against a range of values excluding the values in the mentioned range itself. Syntax: value NOT BETWEEN low AND high; Or, Syntax: value high; The NOT BETWEEN operator is used generally with WHERE clause with association with SELECT, INSERT, UPDATE or DELETE statement. For the sake of this
1 min read
PostgreSQL - ILIKE operator
The PostgreSQL ILIKE operator is used to query data based on pattern-matching techniques. Its result includes strings that are case-insensitive and follow the mentioned pattern. It is important to know that PostgreSQL provides 2 special wildcard characters for the purpose of patterns matching as below: Percent ( %) for matching any sequence of char
2 min read
PostgreSQL - Joins
A PostgreSQL JOIN statement is used to combine data or rows from one(self-JOIN) or more tables based on a common field between them. These common fields are generally the Primary key of the first table and the Foreign key of other tables. Let us better understand the JOINS in PostgreSQL from this article. Types of JOINSThere are 4 basic types of jo
3 min read
Basic Calculator Program Using Java
Create a simple calculator which can perform basic arithmetic operations like addition, subtraction, multiplication, or division depending upon the user input. Example: Enter the numbers: 2 2 Enter the operator (+,-,*,/) + The final result: 2.0 + 2.0 = 4.0ApproachTake two numbers using the Scanner class. The switch case branching is used to execute
2 min read
Features of C Programming Language
C is a procedural programming language. It was initially developed by Dennis Ritchie in the year 1972. It was mainly developed as a system programming language to write an operating system. The main features of C language include low-level access to memory, a simple set of keywords, and a clean style, these features make C language suitable for sys
3 min read
Java Arithmetic Operators with Examples
Operators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they provide. Here are a few types: Arithmetic Operator
6 min read
For Loop in Java
Loops in Java come into use when we need to repeatedly execute a block of statements. Java for loop provides a concise way of writing the loop structure. The for statement consumes the initialization, condition, and increment/decrement in one line thereby providing a shorter, easy-to-debug structure of looping. Let us understand Java for loop with
7 min read
Data encryption standard (DES) | Set 1
This article talks about the Data Encryption Standard (DES), a historic encryption algorithm known for its 56-bit key length. We explore its operation, key transformation, and encryption process, shedding light on its role in data security and its vulnerabilities in today's context. What is DES?Data Encryption Standard (DES) is a block cipher with
15+ min read
Introduction to Internet of Things (IoT) - Set 1
IoT stands for Internet of Things. It refers to the interconnectedness of physical devices, such as appliances and vehicles, that are embedded with software, sensors, and connectivity which enables these objects to connect and exchange data. This technology allows for the collection and sharing of data from a vast network of devices, creating oppor
9 min read
Basics of Computer and its Operations
Introduction : A computer is an electronic device that can receive, store, process, and output data. It is a machine that can perform a variety of tasks and operations, ranging from simple calculations to complex simulations and artificial intelligence. Computers consist of hardware components such as the central processing unit (CPU), memory, stor
12 min read
Block Cipher modes of Operation
Encryption algorithms are divided into two categories based on the input type, as a block cipher and stream cipher. Block cipher is an encryption algorithm that takes a fixed size of input say b bits and produces a ciphertext of b bits again. If the input is larger than b bits it can be divided further. For different applications and uses, there ar
5 min read
Carrier Sense Multiple Access (CSMA)
This method was developed to decrease the chances of collisions when two or more stations start sending their signals over the data link layer. Carrier Sense multiple access requires that each station first check the state of the medium before sending. Prerequisite - Multiple Access Protocols Vulnerable Time: Vulnerable time = Propagation time (Tp)
6 min read
vector::push_back() and vector::pop_back() in C++ STL
Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector::push_back() push_back() function is used to push elements into a vector from the back. The new value is inserted into the vector at the end, after the cur
4 min read
Carry Look-Ahead Adder
The adder produce carry propagation delay while performing other arithmetic operations like multiplication and divisions as it uses several additions or subtraction steps. This is a major problem for the adder and hence improving the speed of addition will improve the speed of all other arithmetic operations. Hence reducing the carry propagation de
5 min read
Phases of a Compiler
Prerequisite - Introduction of Compiler design We basically have two phases of compilers, namely the Analysis phase and Synthesis phase. The analysis phase creates an intermediate representation from the given source code. The synthesis phase creates an equivalent target program from the intermediate representation. A compiler is a software program
7 min read
Introduction of Compiler Design
The compiler is software that converts a program written in a high-level language (Source Language) to a low-level language (Object/Target/Machine Language/0, 1's). A translator or language processor is a program that translates an input program written in a programming language into an equivalent program in another language. The compiler is a type
9 min read
Introduction of Theory of Computation
Automata theory (also known as Theory Of Computation) is a theoretical branch of Computer Science and Mathematics, which mainly deals with the logic of computation with respect to simple machines, referred to as automata. Automata* enables scientists to understand how machines compute the functions and solve problems. The main motivation behind dev
3 min read
goto Statement in C
The C goto statement is a jump statement which is sometimes also referred to as an unconditional jump statement. The goto statement can be used to jump from anywhere to anywhere within a function. Syntax: Syntax1 | Syntax2 ---------------------------- goto label; | label: . | . . | . . | . label: | goto label; In the above syntax, the first line te
3 min read
Python Virtual Environment | Introduction
A Python Virtual Environment is an isolated space where you can work on your Python projects, separately from your system-installed Python. You can set up your own libraries and dependencies without affecting the system Python. We will use virtualenv to create a virtual environment in Python. What is a Virtual Environment?A virtual environment is a
4 min read
Dynamic Method Dispatch or Runtime Polymorphism in Java
Prerequisite: Overriding in java, Inheritance Method overriding is one of the ways in which Java supports Runtime Polymorphism. Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved at run time, rather than compile time. When an overridden method is called through a superclass reference, Java determines which
5 min read
SQL Functions (Aggregate and Scalar Functions)
SQL Functions are built-in programs that are used to perform different operations on the database. There are two types of functions in SQL: Aggregate FunctionsScalar FunctionsSQL Aggregate FunctionsSQL Aggregate Functions operate on a data group and return a singular output. They are mostly used with the GROUP BY clause to summarize data.  Some com
4 min read
Rail Fence Cipher - Encryption and Decryption
Given a plain-text message and a numeric key, cipher/de-cipher the given text using Rail Fence algorithm. The rail fence cipher (also called a zigzag cipher) is a form of transposition cipher. It derives its name from the way in which it is encoded. Examples:  EncryptionInput : "GeeksforGeeks "Key = 3Output : GsGsekfrek eoeDecryptionInput : GsGsekf
15 min read
Command Line Arguments in Java
Java command-line argument is an argument i.e. passed at the time of running the Java program. In Java, the command line arguments passed from the console can be received in the Java program and they can be used as input. The users can pass the arguments during the execution bypassing the command-line arguments inside the main() method. Working com
3 min read
Data Abstraction and Data Independence
Database systems comprise complex data structures. In order to make the system efficient in terms of retrieval of data, and reduce complexity in terms of usability of users, developers use abstraction i.e. hide irrelevant details from the users. This approach simplifies database design.  Level of Abstraction in a DBMSThere are mainly 3 levels of da
4 min read
Article Tags :
Practice Tags :