By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
World of SoftwareWorld of SoftwareWorld of Software
  • News
  • Software
  • Mobile
  • Computing
  • Gaming
  • Videos
  • More
    • Gadget
    • Web Stories
    • Trending
    • Press Release
Search
  • Privacy
  • Terms
  • Advertise
  • Contact
Copyright © All Rights Reserved. World of Software.
Reading: How to Master SQL Joins – Inner, Outer, Cross, and Self-Joins With Examples | HackerNoon
Share
Sign In
Notification Show More
Font ResizerAa
World of SoftwareWorld of Software
Font ResizerAa
  • Software
  • Mobile
  • Computing
  • Gadget
  • Gaming
  • Videos
Search
  • News
  • Software
  • Mobile
  • Computing
  • Gaming
  • Videos
  • More
    • Gadget
    • Web Stories
    • Trending
    • Press Release
Have an existing account? Sign In
Follow US
  • Privacy
  • Terms
  • Advertise
  • Contact
Copyright © All Rights Reserved. World of Software.
World of Software > Computing > How to Master SQL Joins – Inner, Outer, Cross, and Self-Joins With Examples | HackerNoon
Computing

How to Master SQL Joins – Inner, Outer, Cross, and Self-Joins With Examples | HackerNoon

News Room
Last updated: 2025/05/03 at 7:35 PM
News Room Published 3 May 2025
Share
SHARE

Introdution

SQL joins are a fundamental feature for combining data from multiple tables based on a related column. Understanding the different types of joins and their applications is essential for working with relational databases effectively. In this article, we’ll explore various SQL join types with clear explanations and practical examples.

Sample Tables

We’ll use the following Customers and Orders tables for examples:

Customers Table:

CustomerID

Name

Country

1

Alice

USA

2

Bob

Canada

3

Charlie

UK

4

Diana

Germany

Orders Table:

OrderID

CustomerID

Product

Quantity

101

1

Laptop

2

102

1

Mouse

5

103

2

Keyboard

3

104

3

Monitor

1

105

5

Smartphone

2

1. INNER JOIN: Combining Matching Data

INNER JOIN retrieves rows that have matching values in both tables. It is the most commonly used join type.

Example: Combining Customers and Their Orders

Query:

SELECT Customers.Name, Orders.Product, Orders.Quantity
FROM Customers
INNER JOIN Orders
ON Customers.CustomerID = Orders.CustomerID;

Result:

Name

Product

Quantity

Alice

Laptop

2

Alice

Mouse

5

Bob

Keyboard

3

Charlie

Monitor

1

Explanation:

  • Only customers with orders appear in the result.
  • Customer Diana and order 105 are excluded because they don’t have matching records in both tables.

2. LEFT JOIN: Including All Records From the Left Table

LEFT JOIN returns all rows from the left table (Customers), along with matching rows from the right table (Orders). Rows with no match in the right table will have NULL values.

Example: Including Customers Without Orders

Query:

SELECT Customers.Name, Orders.Product, Orders.Quantity
FROM Customers
LEFT JOIN Orders
ON Customers.CustomerID = Orders.CustomerID;

Result:

Name

Product

Quantity

Alice

Laptop

2

Alice

Mouse

5

Bob

Keyboard

3

Charlie

Monitor

1

Diana

NULL

NULL

Explanation:

  • All customers are included, even if they don’t have orders.
  • Diana appears with NULL for Product and Quantity.

3. RIGHT JOIN: Including All Records From the Right Table

RIGHT JOIN is the opposite of LEFT JOIN. It includes all rows from the right table (Orders) and matching rows from the left table (Customers). Rows with no match in the left table will have NULL values.

Example: Including Orders Without Customers

Query:

SELECT Customers.Name, Orders.Product, Orders.Quantity
FROM Customers
RIGHT JOIN Orders
ON Customers.CustomerID = Orders.CustomerID;

Result:

Name

Product

Quantity

Alice

Laptop

2

Alice

Mouse

5

Bob

Keyboard

3

Charlie

Monitor

1

NULL

Smartphone

2

Explanation:

  • All orders are included, even if they don’t have matching customers.
  • Order 105 appears with NULL for Name because CustomerID = 5 is not in the Customers table.

4. FULL OUTER JOIN: Including All Records from Both Tables

FULL OUTER JOIN combines the results of LEFT JOIN and RIGHT JOIN, returning all rows from both tables. Rows with no match will have NULL values for the missing columns.

Example: Combining All Customers and Orders

Query:

SELECT Customers.Name, Orders.Product, Orders.Quantity
FROM Customers
FULL OUTER JOIN Orders
ON Customers.CustomerID = Orders.CustomerID;

Result:

Name

Product

Quantity

Alice

Laptop

2

Alice

Mouse

5

Bob

Keyboard

3

Charlie

Monitor

1

Diana

NULL

NULL

NULL

Smartphone

2

Explanation:

  • All customers and orders are included.
  • Diana (no orders) and order 105 (no matching customer) appear with NULL values.

5. CROSS JOIN: Cartesian Product

CROSS JOIN returns the Cartesian product of two tables, pairing every row from the left table with every row from the right table.

Example: Pairing Customers with Products

Query:

SELECT Customers.Name, Orders.Product
FROM Customers
CROSS JOIN Orders;

Result:

Name

Product

Alice

Laptop

Alice

Mouse

Alice

Keyboard

Alice

Monitor

Alice

Smartphone

Bob

Laptop

Bob

Mouse

…

…

Explanation:

  • Every customer is paired with every product, resulting in 20 rows (4 Customers x 5 Orders).

6. SELF JOIN: Joining a Table with Itself

SELF JOIN is used to compare rows within the same table. It is useful for hierarchical or relationship data.

Example: Employee-Manager Relationship

Assume we have an Employees table:

EmployeeID

Name

ManagerID

1

Alice

3

2

Bob

3

3

Charlie

NULL

4

Diana

1

Query: Find employees and their managers.

SELECT E1.Name AS Employee, E2.Name AS Manager
FROM Employees E1
LEFT JOIN Employees E2
ON E1.ManagerID = E2.EmployeeID;

Result:

Employee

Manager

Alice

Charlie

Bob

Charlie

Charlie

NULL

Diana

Alice

Explanation:

  • The table is joined with itself using ManagerID and EmployeeID to associate employees with their managers.

Summary of Joins

Join Type

Description

Example Use Case

INNER JOIN

Matches rows in both tables.

Customers with orders.

LEFT JOIN

All rows from the left table, matching rows from the right.

Customers with or without orders.

RIGHT JOIN

All rows from the right table, matching rows from the left.

Orders with or without customers.

FULL OUTER JOIN

All rows from both tables, with NULLs for missing matches.

Complete customer and order data.

CROSS JOIN

Cartesian product of two tables.

Pairing customers with products.

SELF JOIN

Join a table with itself.

Employee-manager relationships.

Conclusion

Understanding SQL joins is key to working with relational databases. Each join type serves a unique purpose, and mastering them will help you combine and analyze data efficiently. Practice these examples to solidify your understanding!


Thank you for taking the time to explore data-related insights with me. I appreciate your engagement. If you find this information helpful, I invite you to follow me or connect with me on LinkedIn. Happy exploring!👋

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Twitter Email Print
Share
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article UK networks feel the strain under AI pressure | Computer Weekly
Next Article This is the Dolce Vita Orient Express, the Italian train of up to 21,400 euros the room for two nights
Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Stay Connected

248.1k Like
69.1k Follow
134k Pin
54.3k Follow

Latest News

Apple’s new white-knuckle wildfire drama stars Matthew McConaughey as a heroic bus driver
News
Iranian Hacker Pleads Guilty in $19 Million Robbinhood Ransomware Attack on Baltimore
Computing
iOS 19: All the rumored changes Apple could be bringing to its new operating system | News
News
How to Turn Off Meta AI Features on Facebook, Instagram or WhatsApp
News

You Might also Like

Computing

Iranian Hacker Pleads Guilty in $19 Million Robbinhood Ransomware Attack on Baltimore

3 Min Read
Computing

Box64 v0.3.6 Brings Better AVX Handling, Volatile Metadata For Windows Executables

2 Min Read
Computing

DJI to build global smart aviation headquarters in Shenzhen after acquiring plot for $315 million · TechNode

1 Min Read
Computing

3 Ways Large Brands Grow & Experiment on Social Media

3 Min Read
//

World of Software is your one-stop website for the latest tech news and updates, follow us now to get the news that matters to you.

Quick Link

  • Privacy Policy
  • Terms of use
  • Advertise
  • Contact

Topics

  • Computing
  • Software
  • Press Release
  • Trending

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

World of SoftwareWorld of Software
Follow US
Copyright © All Rights Reserved. World of Software.
Welcome Back!

Sign in to your account

Lost your password?