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 Update/Delete Constraints
00:00
5 left

SQL Update/Delete Constraints

HardSQL · PostgreSQL

Problem

Explain how you would delete or update rows in Salesforce using only SELECT and INSERT operations.

Provide a PostgreSQL statement that reads the Salesforce contact snapshot and inserts a change plan representing required updates or deletions. Do not directly modify the snapshot table.

Output

  1. One row per eligible contact, with plan_id, contact_id, operation, replacement_status, and replacement_phone.
  2. Include inactive contacts, contacts with missing phone numbers, and deleted contacts. Sort by contact_id ascending.

Output details

  • Treat a contact as eligible when is_deleted is true, status is Inactive, or phone is NULL.
  • Set operation to DELETE for deleted contacts and UPDATE otherwise.
  • Set deleted contacts' replacement_status and replacement_phone to NULL.
  • Set inactive contacts' replacement_status to Archived; otherwise preserve the existing status.
  • Set missing nondeleted phones to UNKNOWN; otherwise preserve the existing phone.
  • Assign plan_id from the existing maximum, defaulting to 0, plus the contact's ascending sequential position, and insert into salesforce_contact_change_plan.

Schema

salesforce_contact_snapshot
ColumnTypeDescription
contact_idPKINTSalesforce Contact identifier
statusVARCHAR(20)Current Contact status
phoneVARCHAR(20)Current phone number
is_deletedBOOLEANWhether Salesforce marked the Contact as deleted
salesforce_contact_change_plan
ColumnTypeDescription
plan_idPKINTChange plan identifier
contact_idINTReferenced Salesforce Contact identifier
operationVARCHAR(10)Planned operation, UPDATE or DELETE
replacement_statusVARCHAR(20)Status to apply for an update
replacement_phoneVARCHAR(20)Phone number to apply for an update
Tablessalesforce_contact_snapshotsalesforce_contact_change_plan
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.