Need help with sql query to find things with most specified tags


Let's say I have the following tables:


id: integer
name: string


id: integer
body: text


id: integer
tag_id: integer
post_id: integer

How would I go about writing a query that selects all posts in order of the post containing the highest number of the following tags (name attribute of tags table): "Cheese", "Wine", "Paris", "Frace", "City", "Scenic", "Art"

See also: Need help with sql query to find things tagged with all specified tags (note: similar, but not a duplicate!)

Unlike your linked question, you did not specify here that you needed to match ALL tags. This query works for ANY.

SELECT, p.text, count( as TagCount
    FROM Posts p
        INNER JOIN Taggings tg
            ON = tg.post_id
        INNER JOIN Tags t
            ON tg.tag_id =
    WHERE in ('Cheese', 'Wine', 'Paris', 'Frace', 'City', 'Scenic', 'Art')
    GROUP BY, p.text
    ORDER BY TagCount DESC