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
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess 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
Get started
Per Card Latest Transaction and Team Aggregates
00:00
5 left

Per Card Latest Transaction and Team Aggregates

MediumSQL · PostgreSQL

Problem

Write SQL queries to (1) pick the latest transaction for each credit card and compute total transactions per card, and (2) aggregate Team_A and Team_B results into total played, won, lost, and drawn.

Use the supplied tables. Rows with a NULL card number, transaction time, or match result are ignored. The combined output should distinguish card and team summaries.

Output

  1. Columns: result_type, entity, latest_transaction_id, transaction_count, played, won, lost, drawn
  2. One row per card and one row per Team_A or Team_B
  3. For equal transaction times, the greatest transaction ID is latest
  4. Order by result_type, then entity

Schema

card_transactions
ColumnTypeDescription
transaction_idPKINTEGERUnique transaction identifier
card_numberVARCHAR(30)Credit card identifier
transaction_timeTIMESTAMPTransaction timestamp
amountNUMERIC(12,2)Transaction amount
match_results
ColumnTypeDescription
match_idPKINTEGERUnique match identifier
teamVARCHAR(30)Team whose result is recorded
opponentVARCHAR(30)Opposing team
resultVARCHAR(20)Match result such as WON, LOST, DRAWN, or IN_PROGRESS
match_dateDATEDate of the match
Tablescard_transactionsmatch_results
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the two tables are in the Question tab.

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.