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
Start free
Design an Account Schema and Balance Report
00:00
5 left

Design an Account Schema and Balance Report

HardSQL · PostgreSQL

Problem

Again, scenario of a schema for which I needed to design the whole schema?

Design a normalized relational schema for accounts, customers, account ownership, and account transactions, then write a query that validates the design by producing an account-level balance report. Include accounts without transactions and preserve accounts with no recorded owner.

Output

  1. One row per account with account_id, account_type, owner_names, balance, latest_transaction_date, and account_type_rank.
  2. Include every account, calculate credits as positive and debits or disbursements as negative, and use zero for accounts without transactions.
  3. Sort by account_type, descending balance, then account_id.

Output details

  • Sort account_type ascending, balance descending with nulls last, and account_id ascending.
  • Format balance as numeric with exactly two decimal places.
  • Set latest_transaction_date to the maximum transaction date, or NULL when no transaction exists.
  • Treat transaction types other than credit, debit, and disbursement as contributing zero.
  • Aggregate distinct owner names alphabetically, separated by ', ', and return NULL when no owner exists.
  • Assign dense ranks within each account_type, with equal balances sharing a rank.

Schema

accounts
ColumnTypeDescription
account_idPKINTUnique account identifier
account_typeVARCHAR(20)Account classification
opened_dateDATEDate the account was opened
statusVARCHAR(20)Current account status
customers
ColumnTypeDescription
customer_idPKINTUnique customer identifier
customer_nameVARCHAR(100)Customer's full name
customer_statusVARCHAR(20)Current customer status
account_holders
ColumnTypeDescription
account_idPKINTReferenced account
customer_idPKINTReferenced customer
holder_roleVARCHAR(20)Customer's ownership role
added_dateDATEDate the customer was added
account_transactions
ColumnTypeDescription
transaction_idPKINTUnique transaction identifier
account_idINTReferenced account
transaction_typeVARCHAR(20)Credit, debit, or disbursement
amountNUMERIC(14,2)Nonnegative transaction amount
transaction_dateDATEDate of the transaction
descriptionVARCHAR(100)Optional transaction description
Tablesaccountscustomersaccount_holdersaccount_transactions
Interviewer

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

Sign up free to run or submit. Log in
CodePostgreSQL
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 1
Run your query to see results here.