Hashtag Trends

ASKED IN INTERVIEW
4pts
Twitter

Problem Statement

Twitter analytics team needs to display the Trending Now section on the user dashboard. A hashtag is considered trending based on how many times it has been used in tweets within a rolling 6-hour window.

Write an SQL query to find the top 3 hashtags used in the last 6 hours.

Rules:

  • The time window is exactly 6 hours prior to the latest tweet timestamp in the dataset.
  • If two hashtags have the same count, order them alphabetically.
  • Output columns: hashtag_name, usage_count.
  • Return the results in descending order of usage_count.

Table Schema:

  • Tweets: Contains tweet metadata.
  • Hashtags: Contains the hashtag strings.
  • Tweet_Hashtags: A mapping table linking tweets to hashtags.
Tests your understanding of
Aggregation, Window Functions, Filtering and DateTime

Input Tables

Tweets
tweet_id(INTEGER)user_id(INTEGER)tweet_timestamp(TIMESTAMP)
1102026-02-10 08:00:00
2112026-02-10 09:30:00
3122026-02-10 10:15:00
4132026-02-10 12:00:00
5142026-02-10 13:45:00
6152026-02-10 13:55:00
Hashtags
hashtag_id(INTEGER)hashtag_name(VARCHAR)
1#SQL
2#Tech
3#Twitter
4#Data
Tweet_Hashtags
tweet_id(INTEGER)hashtag_id(INTEGER)
11
21
32
41
52
63

Expected Output

hashtag_name(VARCHAR)usage_count(INTEGER)
#SQL3
#Tech2
#Twitter1

Tags

MediumASKED IN INTERVIEWAggregationWindow FunctionsFilteringDateTime
15-20 min
54%

Hints