Now we are entering one of the most important concepts in SQL: JOIN This is where SQL starts to feel like real backend development. The Problem We Want to Solve We wan
MSMuhammad SufiyanSoftware Engineer · 6d ago
Backend Engineering HubT-
Now we are entering one of the most important concepts in SQL:
JOIN
This is where SQL starts to feel like real backend development.
The Problem We Want to Solve
We want to answer this question:
For each comment, show:
The content of the comment
The username of the person who wrote the comment
Example output:
contents
username
This photo is amazing
Alfred66
Wow!
Reyna
Nice work
Micah
Notice something important:
contents comes from comments table
username comes from users table
That means:
👉 We need data from two different tables.
And that is our first hint that we need a JOIN.
Step 1: Understand the Relationship
Let’s look at the structure:
comments table
id
contents
user_id (foreign key)
photo_id
users table
id
username
The key connection:
comments.user_id → users.id
This foreign key tells us:
Which user wrote each comment.
Step 2: Write the Query
Here is the SQL:
SELECT contents, username
FROM comments
JOIN users
ON users.id = comments.user_id;
That’s it.
Now let’s understand what this actually does.
What Happens Behind the Scenes?
1️⃣ FROM comments
SQL first takes all rows from the comments table.
You can imagine:
Temporary Table = comments
2️⃣ JOIN users
Now SQL says:
Combine each comment with a matching row from users.
But how?
Using this condition:
ON users.id = comments.user_id
This means:
For every comment:
Take its user_id
Go to users table
Find the user whose id matches
Attach that user’s row
Visual Mental Model
Imagine a comment:
id
contents
user_id
1
Nice pic
3
SQL sees user_id = 3.
It goes to users table:
id
username
3
Alfred66
Now SQL combines them:
contents
username
Nice pic
Alfred66
It repeats this process for every comment.
What JOIN Really Creates
Internally, SQL creates a temporary combined table like this:
contents
user_id
photo_id
username
...
...
...
...
But since we wrote:
SELECT contents, username
SQL only shows those two columns.
Everything else is ignored in the final output.
Why We Didn’t Use Aggregation Here
Notice:
The question did NOT say average
It did NOT say most
It did NOT say count
So we only needed a JOIN.
Rule for Students
When writing SQL:
If question mentions:
Multiple tables
Different types of resources
👉 Think JOIN.
If question mentions:
average
count
most
total
👉 Think Aggregation.
Bonus: Adding More Columns
If we also wanted photo_id:
SELECT contents, username, photo_id
FROM comments
JOIN users
ON users.id = comments.user_id;
Easy.
Because after the JOIN, we can access columns from both tables.
Why JOIN Is So Powerful
JOIN allows you to:
Connect users with comments
Connect comments with photos
Connect photos with authors
Build complex application queries
Every social media app, every ecommerce app, every SaaS product uses JOIN constantly.
Important Concept
JOIN does NOT permanently merge tables.
It creates a temporary result set.
Your original tables remain unchanged.
HIRINGMINE CAREER SIGNAL
This writing is proof of expertise.
Explore the author’s verified skills, projects and availability—or start a professional conversation.