cswork / libraryOpen practice workspace

Coding questions · Meta

Write SQL for hashtag source and safety rates

This question evaluates SQL data-manipulation competencies such as deduplication, date filtering, aggregations and ranking, handling missing foreign-key references, and calculating percentages from event and reference tables.

Write SQL for the two tasks below. Assume the schema and sample data as given, and that “today” is 2025‑09‑01. Deduplicate exact duplicates by (date, user_id, hashtag_id, source). If a hashtag_id is missing from the hashtag table, exclude it only from task (2); include it in task (1). Use UTC dates. Schema:

Table: following_behavior(date DATE, user_id INT, hashtag_id INT, source VARCHAR) -- each row is a follow event Table: hashtag(hashtag_id INT, safety VARCHAR) -- safety in {'safety','violating'}

Sample rows — following_behavior:

2025-09-01 | 1 | 100 | hashtag page
2025-09-01 | 1 | 100 | hashtag page -- duplicate
2025-09-01 | 1 | 101 | feed
2025-09-01 | 2 | 100 | feed
2025-09-01 | 2 | 102 | hashtag page
2025-09-01 | 3 | 103 | hashtag page
2025-08-31 | 4 | 100 | hashtag page -- not today
2025-09-01 | 5 | 104 | feed
2025-09-01 | 6 | 105 | hashtag page
2025-09-01 | 6 | 105 | feed
2025-09-01 | 7 | 106 | hashtag page
2025-09-01 | 8 | 107 | feed
2025-09-01 | 9 | 108 | hashtag page
2025-09-01 | 10 | 109 | feed
2025-09-01 | 11 | 110 | hashtag page
2025-09-01 | 12 | 999 | hashtag page -- hashtag 999 missing from hashtag table

Sample rows — hashtag:

100 | safety
101 | violating
102 | safety
103 | violating
104 | safety
105 | safety
106 | violating
107 | safety
108 | violating
109 | safety
110 | safety

Tasks: (1) Which source ('hashtag page' vs 'feed') has the most follows today? Return: source, follows_today, and rank (1=most). Break ties by alphabetical source. (2) What percent of today’s follows from source='hashtag page' are on violating hashtags? Return a single row with pct_violating (0–100 with two decimals).

Rank follow sources by number of follows on a specific date

Using the tables below, write a SQL query to find which source ('hashtag page' vs 'feed') has the most follows on the UTC date 2025-06-01 (treated as "today").

Rules:

  • First, deduplicate exact duplicate follow events: rows are considered duplicates if all of (date, user_id, hashtag_id, source) are identical. Count each such event only once.
  • Include all follows regardless of whether the hashtag_id exists in the hashtag table (do NOT join to the hashtag table for this task).

Return the following columns:

  • source
  • follows_today: the number of deduplicated follows from that source on 2025-06-01
  • rank: 1 for the source with the most follows, 2 for the next, etc. If there is a tie in follows_today, the source that is alphabetically earlier should get the better (smaller) rank.

Use only the rows from date = '2025-06-01' (UTC).

Table following_behavior

columntypeconstraints
dateDATENOT NULL
user_idINTNOT NULL
hashtag_idINTNOT NULL
sourceVARCHAR(50)NOT NULL

Sample data:

datehashtag_idsourceuser_id
2025-06-01100hashtag page1
2025-06-01100hashtag page1
2025-06-01101feed1
2025-06-01100feed2
2025-06-01102hashtag page2
2025-06-01103hashtag page3
2025-05-31100hashtag page4
2025-06-01104feed5
2025-06-01105hashtag page6
2025-06-01105feed6
2025-06-01106hashtag page7
2025-06-01107feed8
2025-06-01108hashtag page9
2025-06-01109feed10
2025-06-01110hashtag page11
2025-06-01999hashtag page12

Table hashtag

columntypeconstraints
hashtag_idINTPRIMARY KEY, NOT NULL
safetyVARCHAR(20)NOT NULL

Sample data:

hashtag_idsafety
100safety
101violating
102safety
103violating
104safety
105safety
106violating
107safety
108violating
109safety
110safety

Expected output

One row per source showing the number of deduplicated follows on 2025-06-01 and the rank of the source by that count (1 = most follows).

follows_todayranksource
81hashtag page
62feed

Hints

  1. Put a SELECT DISTINCT over (date, user_id, hashtag_id, source) in a CTE to deduplicate events before counting.
  2. Use a window function like RANK() ordered by follows_today DESC and source ASC to assign ranks with alphabetical tie-breaking.

Percent of violating hashtag follows from the hashtag page source

Using the same tables, write a SQL query to compute what percentage of today's follows from source = 'hashtag page' are on violating hashtags, for the UTC date 2025-06-01 (treated as "today").

Rules:

  • First, deduplicate exact duplicate follow events: rows are considered duplicates if all of (date, user_id, hashtag_id, source) are identical. Count each such event only once.
  • Restrict to deduplicated rows where date = '2025-06-01' and source = 'hashtag page'.
  • Join to the hashtag table to get the safety label.
  • If a follow's hashtag_id does not exist in the hashtag table, EXCLUDE that follow entirely from both the numerator and denominator.

Return a single row with:

  • pct_violating: the percentage (0–100) of these follows whose hashtag has safety = 'violating', rounded to two decimal places.

Table following_behavior

columntypeconstraints
dateDATENOT NULL
user_idINTNOT NULL
hashtag_idINTNOT NULL
sourceVARCHAR(50)NOT NULL

Sample data:

datehashtag_idsourceuser_id
2025-06-01100hashtag page1
2025-06-01100hashtag page1
2025-06-01101feed1
2025-06-01100feed2
2025-06-01102hashtag page2
2025-06-01103hashtag page3
2025-05-31100hashtag page4
2025-06-01104feed5
2025-06-01105hashtag page6
2025-06-01105feed6
2025-06-01106hashtag page7
2025-06-01107feed8
2025-06-01108hashtag page9
2025-06-01109feed10
2025-06-01110hashtag page11
2025-06-01999hashtag page12

Table hashtag

columntypeconstraints
hashtag_idINTPRIMARY KEY, NOT NULL
safetyVARCHAR(20)NOT NULL

Sample data:

hashtag_idsafety
100safety
101violating
102safety
103violating
104safety
105safety
106violating
107safety
108violating
109safety
110safety

Expected output

A single row with the percentage (0–100) of deduplicated follows on 2025-06-01 from source = 'hashtag page' that are on violating hashtags, excluding follows whose hashtag_id is missing from the hashtag table.

pct_violating
42.86

Hints

  1. Deduplicate in a CTE, then filter to date = '2025-06-01' and source = 'hashtag page' before joining.
  2. Use an INNER JOIN to the hashtag table so that missing hashtag_ids are excluded, and compute the percentage with SUM(CASE ...) over COUNT(*), then ROUND to two decimals.