Joining Tables and Avoiding Double Counting
🎯 Introduction
By the end of this article, you will be able to query article authors, include users without articles, and count articles correctly.
📝 Prepare article data
The original query_user user table contains:
| id | username | age | city | created_at |
|---|---|---|---|---|
| 1 | Xiao Ming | 25 | Beijing | 2026-10-01 09:00:00 |
| 2 | Xiao Hong | 30 | Shanghai | 2026-10-02 09:00:00 |
| 3 | Xiao Gang | 22 | Beijing | 2026-10-03 09:00:00 |
| 4 | Xiao Lin | 30 | Shanghai | 2026-10-04 09:00:00 |
| 5 | Xiao An | NULL | Hangzhou | 2026-10-05 09:00:00 |
For the table creation and insertion statements, see Query practice data. Next, create an article table in blog_demo:
CREATE TABLE article_sample (
id INT PRIMARY KEY,
user_id INT NOT NULL,
title VARCHAR(100) NOT NULL
);
user_id stores the author's ID in query_user, avoiding a repeated copy of the name in each article.
INSERT INTO article_sample VALUES
(1, 1, 'Learning SQL'), (2, 1, 'Understanding Queries'), (3, 2, 'Database Notes');
After insertion, the complete article_sample article table contains:
| id | user_id | title |
|---|---|---|
| 1 | 1 | Learning SQL |
| 2 | 1 | Understanding Queries |
| 3 | 2 | Database Notes |
Compare the tables: the two articles with user_id 1 belong to Xiao Ming, and the article with user_id 2 belongs to Xiao Hong. The other users have no articles. This article focuses on queries; the data modeling article covers constraints on related data.
🔗 Query articles and authors with INNER JOIN
SELECT a.title, u.username
FROM article_sample AS a
INNER JOIN query_user AS u ON a.user_id = u.id
ORDER BY a.id;
Query result:
| title | username |
|---|---|
| Learning SQL | Xiao Ming |
| Understanding Queries | Xiao Ming |
| Database Notes | Xiao Hong |
AS a and AS u give the tables short aliases; ON specifies which rows match. The results are “Learning SQL / Xiao Ming”, “Understanding Queries / Xiao Ming”, and “Database Notes / Xiao Hong”.
INNER JOIN keeps only rows that match on both sides. If an article's user_id has no matching user, that article is absent from the result.
👤 Include users without articles with LEFT JOIN
SELECT u.username, a.title
FROM query_user AS u
LEFT JOIN article_sample AS a ON a.user_id = u.id
ORDER BY u.id, a.id;
Query result:
| username | title |
|---|---|
| Xiao Ming | Learning SQL |
| Xiao Ming | Understanding Queries |
| Xiao Hong | Database Notes |
| Xiao Gang | NULL |
| Xiao Lin | NULL |
| Xiao An | NULL |
query_user is on the left, so all five users are retained. Xiao Ming matches two article rows, Xiao Hong matches one, and each of the other three users has one row with a NULL title, producing six rows in total.
The number of joined rows is not necessarily the number of users: a one-to-many relationship produces a row for each matching combination.
🔢 Count each user's articles
SELECT u.id, u.username, COUNT(a.id) AS article_count
FROM query_user AS u
LEFT JOIN article_sample AS a ON a.user_id = u.id
GROUP BY u.id, u.username ORDER BY u.id;
Query result:
| id | username | article_count |
|---|---|---|
| 1 | Xiao Ming | 2 |
| 2 | Xiao Hong | 1 |
| 3 | Xiao Gang | 0 |
| 4 | Xiao Lin | 0 |
| 5 | Xiao An | 0 |
Use COUNT(a.id): when there is no article, a.id is NULL and is not counted. COUNT(*) would count the placeholder row for a user without articles as 1.
🪤 Where you put right-table conditions changes the result
Suppose you want to list every user but show only articles titled “Learning SQL”:
SELECT u.username, a.title FROM query_user AS u
LEFT JOIN article_sample AS a
ON a.user_id = u.id AND a.title = 'Learning SQL'
ORDER BY u.id;
Query result:
| username | title |
|---|---|
| Xiao Ming | Learning SQL |
| Xiao Hong | NULL |
| Xiao Gang | NULL |
| Xiao Lin | NULL |
| Xiao An | NULL |
All five users remain, and only Xiao Ming has a non-NULL title. Placing the condition in ON determines which articles can match.
Putting the same title condition in WHERE also removes the NULL rows after the join, leaving only Xiao Ming. Before choosing where to put the condition, decide whether to retain users without matching articles.
❓ Questions and Answers
Q1: Both tables have an id column. How do I avoid an ambiguous column error?
Write a.id or u.id to identify the source table.
Q2: Do two rows for Xiao Ming mean the database stores duplicate users?
No. One user matches two articles, so the query returns two rows. The user table still contains only one record for Xiao Ming.
Q3: What happens without a join condition?
You may get every combination of rows from both tables. Check that ON correctly connects the author ID to the user's primary key.
🧪 Exercise
Query each article's title and its author's city. Then use LEFT JOIN to find users with zero articles. Expect Xiao Gang, Xiao Lin, and Xiao An.
-- Write your SQL or record your verification results here
