SQL INNER JOIN

INNER JOIN in SQL is used to combine rows from two or more tables when there is a match in both tables based on a related column.
      • It returns only the rows that have matching values in both tables.

Key Points
      • Returns only matching rows from both tables.
      • Uses the ON condition to specify the relationship.
      • Most commonly used join in SQL queries.

Basic Syntax

SELECT table1.column_name, table2.column_name
FROM table1
INNER JOIN table2
ON table1.common_column = table2.common_column;

Example: INNER JOIN Query

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

Explanation
1. customer_id that exist in both tables, so they appear in the result.
2. customer_id that have no orders, so they are not included.
3. customer_id that exists only in the orders table, so it is also excluded.

Another INNER JOIN Example (Simpler)

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

Key Idea

Customers Table  ∩  Orders Table
        (Matching Records Only)


Simple Definition:
INNER JOIN returns only the rows where the condition matches in both tables.


Topics