Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Diagnose SQL with Joins and Group By
00:00
5 left

Diagnose SQL with Joins and Group By

HardSQL · PostgreSQL

Problem

You're a Software Engineer on PlayStation's store analytics team, reviewing a query a teammate wrote for a PS Plus regional spend report. It should show one row per player in a region: how much they've spent on RPGs and how many purchases that was.

CREATE TABLE players (
    player_id BIGINT PRIMARY KEY,
    username VARCHAR(50) NOT NULL,
    region VARCHAR(30) NOT NULL
);

CREATE TABLE games (
    game_id INT PRIMARY KEY,
    title VARCHAR(150) NOT NULL,
    genre VARCHAR(50) NOT NULL
);

CREATE TABLE purchases (
    purchase_id BIGINT PRIMARY KEY,
    player_id BIGINT NOT NULL,
    game_id INT NOT NULL,
    amount_usd NUMERIC(8,2) NOT NULL,
    status VARCHAR(20) NOT NULL,
    purchased_at TIMESTAMP NOT NULL
);

SELECT
    p.region,
    p.username,
    g.title,
    SUM(pu.amount_usd) AS total_spent,
    COUNT(pu.purchase_id) AS purchase_count
FROM players p
JOIN purchases pu ON p.player_id = pu.player_id
JOIN games g ON pu.game_id = g.game_id
WHERE g.genre = 'RPG'
  AND p.region = 'EU'
GROUP BY p.region
ORDER BY total_spent DESC;

Product wants one row per EU player, including players who own zero RPGs, with their correct RPG spend. Diagnose everything wrong with this query.

Assume every purchase row is eligible because no status filter is specified.

Output

  1. One row per EU player, including players with no RPG purchases.
  2. Columns: region, username, total_spent, and purchase_count.
  3. Sort by total_spent descending, then username ascending to break ties.

Schema

players
ColumnTypeDescription
player_idPKBIGINTUnique PlayStation player identifier
usernameVARCHAR(50)Player display name
regionVARCHAR(30)Player account region
games
ColumnTypeDescription
game_idPKINTUnique game identifier
titleVARCHAR(150)Game title
genreVARCHAR(50)Primary game genre
purchases
ColumnTypeDescription
purchase_idPKBIGINTUnique purchase identifier
player_idBIGINTPlayer who made the purchase
game_idINTPurchased game
amount_usdNUMERIC(8,2)Purchase amount in US dollars
statusVARCHAR(20)Purchase processing status
purchased_atTIMESTAMPPurchase timestamp
Tablesplayersgamespurchases
Interviewer

Your question is Diagnose SQL with Joins and Group By. Start with the requirements and the three tables in the Question tab.

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.
CodePostgreSQL
Sign up free to run your codeLog inLn 1
Run your query to see results here.