Joining Tables and Avoiding Double Counting

Query article authors, include users without articles, and count articles correctly

🎯 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:

idusernameagecitycreated_at
1Xiao Ming25Beijing2026-10-01 09:00:00
2Xiao Hong30Shanghai2026-10-02 09:00:00
3Xiao Gang22Beijing2026-10-03 09:00:00
4Xiao Lin30Shanghai2026-10-04 09:00:00
5Xiao AnNULLHangzhou2026-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:

iduser_idtitle
11Learning SQL
21Understanding Queries
32Database 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:

titleusername
Learning SQLXiao Ming
Understanding QueriesXiao Ming
Database NotesXiao 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:

usernametitle
Xiao MingLearning SQL
Xiao MingUnderstanding Queries
Xiao HongDatabase Notes
Xiao GangNULL
Xiao LinNULL
Xiao AnNULL

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:

idusernamearticle_count
1Xiao Ming2
2Xiao Hong1
3Xiao Gang0
4Xiao Lin0
5Xiao An0

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:

usernametitle
Xiao MingLearning SQL
Xiao HongNULL
Xiao GangNULL
Xiao LinNULL
Xiao AnNULL

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