Md Mominul Islam | Software and Data Enginnering | SQL Server, .NET, Power BI, Azure Blog

while(!(succeed=try()));

LinkedIn Portfolio Banner

Latest

Home Top Ad

Responsive Ads Here

Post Top Ad

Responsive Ads Here

Tuesday, August 26, 2025

Difference Between INNER JOIN and LEFT JOIN With Examples

 


Introduction to SQL Joins

SQL joins are fundamental operations that combine rows from two or more tables based on related columns. Understanding the difference between INNER JOIN and LEFT JOIN is crucial for writing accurate queries and retrieving the correct data.

INNER JOIN Explained

INNER JOIN returns only the rows that have matching values in both tables. If there's no match in either table, those rows are excluded from the result set.

Syntax:

sql
SELECT columns
FROM table1
INNER JOIN table2 ON table1.column = table2.column;

Example:

sql
-- Create sample tables
CREATE TABLE Customers (
    CustomerID INT PRIMARY KEY,
    CustomerName VARCHAR(50)
);

CREATE TABLE Orders (
    OrderID INT PRIMARY KEY,
    CustomerID INT,
    OrderDate DATE,
    Amount DECIMAL(10, 2)
);

-- Insert sample data
INSERT INTO Customers VALUES (1, 'John Doe'), (2, 'Jane Smith'), (3, 'Bob Johnson');
INSERT INTO Orders VALUES (101, 1, '2023-05-01', 150.00), 
                          (102, 1, '2023-05-15', 200.00),
                          (103, 2, '2023-05-10', 75.00);

-- INNER JOIN example
SELECT 
    c.CustomerName,
    o.OrderID,
    o.OrderDate,
    o.Amount
FROM Customers c
INNER JOIN Orders o ON c.CustomerID = o.CustomerID;

Result:

CustomerNameOrderIDOrderDateAmount
John Doe1012023-05-01150.00
John Doe1022023-05-15200.00
Jane Smith1032023-05-1075.00

Note that Bob Johnson doesn't appear in the results because he has no orders.

LEFT JOIN Explained

LEFT JOIN returns all rows from the left table (table1), and the matched rows from the right table (table2). If there's no match, NULL values are returned for columns from the right table.

Syntax:

sql
SELECT columns
FROM table1
LEFT JOIN table2 ON table1.column = table2.column;

Example:

sql
-- LEFT JOIN example
SELECT 
    c.CustomerName,
    o.OrderID,
    o.OrderDate,
    o.Amount
FROM Customers c
LEFT JOIN Orders o ON c.CustomerID = o.CustomerID;

Result:

CustomerNameOrderIDOrderDateAmount
John Doe1012023-05-01150.00
John Doe1022023-05-15200.00
Jane Smith1032023-05-1075.00
Bob JohnsonNULLNULLNULL

Note that Bob Johnson appears in the results with NULL values for order-related columns.

Key Differences

AspectINNER JOINLEFT JOIN
Result SetMatching rows onlyAll rows from left table + matching rows from right table
NULL HandlingNo NULLs in resultNULLs for non-matching right table rows
PerformanceGenerally fasterMay be slower with large datasets
Use CaseFind related dataFind all records including those without matches

Performance Considerations

  • INNER JOIN typically performs better as it returns a smaller result set

  • LEFT JOIN can be optimized with proper indexing on join columns

  • Always specify the join condition explicitly rather than using WHERE clauses for joins

Best Practices

  1. Use INNER JOIN when you only need matching records

  2. Use LEFT JOIN when you need all records from the left table regardless of matches

  3. Always specify table aliases for better readability

  4. Create indexes on join columns for better performance

  5. Be explicit about join types rather than relying on WHERE clauses

Common Use Cases

  • INNER JOIN: Finding orders with customer details, products with categories

  • LEFT JOIN: Finding customers without orders, products never purchased

Conclusion

Understanding the difference between INNER JOIN and LEFT JOIN is essential for writing accurate SQL queries. INNER JOIN filters results to only matching records, while LEFT JOIN preserves all records from the left table. Choose the appropriate join based on your data requirements to ensure correct results and optimal performance.



No comments:

Post a Comment

Thanks for your valuable comment...........
Md. Mominul Islam

Post Bottom Ad

Responsive Ads Here