What is Inner Join in SQL?

Inner join returns rows when there is at least one match of rows between the tables. INNER JOIN keyword joins the matching records from two tables.

In SQL, an INNER JOIN is a type of join that combines rows from two or more tables based on a related column between them. The result includes only the rows where there is a match in the specified columns of both tables.

The basic syntax for an INNER JOIN is as follows:

SELECT columns
FROM table1
INNER JOIN table2 ON table1.column_name = table2.column_name;

Here:

  • SELECT columns specifies the columns you want to retrieve.
  • FROM table1 specifies the first table.
  • INNER JOIN table2 specifies the second table and the type of join.
  • ON table1.column_name = table2.column_name specifies the condition for the join, indicating which columns should match.

For example:

sql
SELECT orders.order_id, customers.customer_name
FROM orders
INNER JOIN customers ON orders.customer_id = customers.customer_id;

This query retrieves the order_id from the “orders” table and the customer_name from the “customers” table for rows where the customer_id matches in both tables.

In summary, INNER JOIN in SQL is used to retrieve rows from two or more tables based on a specified condition, and it returns only the rows where there is a match in the columns being compared.