Pls help wit sql quistion

Suppose we have a table called ““hashtags”” and another one called ““photo_tags””.

The ““hashtags”” table has two columns only which are:

  • id which is an INT PRIMARY KEY AUTO_INCREMENT
  • hashtag_name which is a VARCHAR.

The ““photo_tags”” table has two columns also which are:

  • id which is an INT PRIMARY KEY AUTO_INCREMENT
  • 'hashtag_id which is a FOREIGN KEY referring to the id` column in the ““hashtags”” table.

Which SQL query will help us identify the top 5 used hashtags? *

SELECT hashtags.hashtag_name, 
       Count(*) AS total 
FROM   hashtags 
       LEFT JOIN photo_tags 
               ON photo_tags.hashtag_id = hashtags.id 
GROUP  BY photo_tags.hashtag_id
LIMIT 5;
SELECT hashtags.hashtag_name, 
       Count(*) AS total 
FROM   hashtags 
       INNER JOIN photo_tags 
               ON photo_tags.hashtag_id = hashtags.id 
GROUP  BY hashtags.id 
ORDER  BY total DESC 
LIMIT  5;
SELECT hashtags.hashtag_name, hashtags.hashtag_id 
FROM   hashtags 
       RIGHT JOIN photo_tags 
               ON photo_tags.hashtag_id = hashtags.id 
GROUP  BY hashtags.id 
LIMIT  5;
SELECT hashtags.hashtag_name, hashtags.hashtag_id, COUNT(*) AS total 
FROM   hashtags WHERE photo_tags.hashtag_id = hashtags.id 
GROUP  BY hashtags.id
ORDER BY total DESC
LIMIT  5;

Hey Hassan,

nice to meet you! :wave:

So what is your solution and why?

Why not just create those tables and run the queries and see which one is correct?