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
SQL Duplicate Accounts with Windows
00:00
5 left

SQL Duplicate Accounts with Windows

HardSQL · PostgreSQL

Problem

AllianceBernstein's client data is consolidated from multiple customer systems, so the same investor may appear under different capitalization, spacing, or phone formatting. Write a PostgreSQL query to identify active investment accounts belonging to duplicate customer profiles.

Requirements

  1. Join customer profiles to investment accounts.
  2. Normalize email addresses and phone numbers before comparing profiles.
  3. Use window functions to return every active account in a duplicate group, including the duplicate count and a deterministic rank.
  4. Exclude records with missing matching fields, inactive accounts, and profiles that do not belong to a duplicate group.

Output details

  • Return normalized_email, normalized_phone, account_id, account_number, legal_name, duplicate_account_count, and duplicate_rank in that order.
  • Normalize email by trimming, removing all whitespace, and lowercasing; normalize phone by removing every non-digit character.
  • Treat an account as active only when its status equals ACTIVE.
  • Exclude only profiles whose email or phone is NULL; retain non-NULL values that normalize to empty strings.
  • Assign duplicate_rank by ascending opened_on, breaking ties with ascending account_id.
  • Order results by ascending normalized_email, ascending normalized_phone, then ascending duplicate_rank.

Schema

customer_profiles
ColumnTypeDescription
profile_idPKINTCustomer profile identifier
legal_nameVARCHAR(255)Customer's legal name
emailVARCHAR(255)Customer email address
phoneVARCHAR(40)Customer phone number
source_systemVARCHAR(50)Originating customer system
investment_accounts
ColumnTypeDescription
account_idPKINTInvestment account identifier
profile_idINTRelated customer profile
account_numberVARCHAR(30)Account number
account_typeVARCHAR(30)Account classification
statusVARCHAR(20)Account status
opened_onDATEAccount opening date
Tablescustomer_profilesinvestment_accounts
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.