SQL INNER JOIN with Examples


 Introduction


The INNER JOIN is the most common type of SQL JOIN. It retrieves rows that have matching values in both tables. If no match is found, the row will not appear in the result set.


Step 1: Basic Syntax


SELECT table1.column, table2.column

FROM table1

INNER JOIN table2

ON table1.common_column = table2.common_column;


➡️ common_column is the field that exists in both tables and links them together.


Step 2: Example with Customers and Orders


Imagine you have two tables:


customers


customer_id name


1 Ali

2 Mona

3 Samir


orders


order_id customer_id product


101 1 Laptop

102 2 Phone

103 1 Keyboard


Query:


SELECT customers.name, orders.product

FROM customers

INNER JOIN orders

ON customers.customer_id = orders.customer_id;


Result:


name product


Ali Laptop

Mona Phone

Ali Keyboard


Step 3: Why Use INNER JOIN?


To get only related data between two (or more) tables.


Reduces clutter by excluding non-matching rows.


Very useful in reporting and analysis.


Conclusion


The INNER JOIN is your go-to when you only want records that exist in both tables. With practice, you can extend INNER JOIN to multiple tables for more complex queries.

Comments