Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL: Unique Words Between Messages

MediumSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is SQL: Unique Words Between Messages. Start with the requirements and the two tables on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Discord wants to compare two messages from a channel and identify words that appear in only one of them. Write a PostgreSQL query that tokenizes the selected messages, removes words shared by both messages, and returns each remaining word once.

Requirements

  1. Read the two target messages from discord_messages, joining discord_channels to verify they belong to the requested Discord channel.
  2. Split message content into words using PostgreSQL string functions and preserve each word's message order.
  3. Treat exact spellings as separate tokens when determining whether a word is shared. After identifying words unique to one message, deduplicate case-insensitively while preserving the first spelling encountered.
  4. Return the words in their first-occurrence order across the two messages.

Schema

discord_channels
ColumnTypeDescription
channel_idPKINTEGERDiscord channel identifier
channel_nameVARCHAR(100)Human-readable Discord channel name
discord_messages
ColumnTypeDescription
message_idPKINTEGERDiscord message identifier
channel_idINTEGERChannel containing the message
contentTEXTMessage text
Tablesdiscord_channelsdiscord_messages
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results