Medium Writes & indexes Free

SQL interview question: Skip Duplicates With INSERT OR IGNORE

Idempotent Customer Import

A CSV import can contain customers you already have. The import must never crash on duplicates - existing rows win.

Write a single INSERT OR IGNORE statement adding these two customers:

  • customer_id 105, name 'Duplicate Dan', email '[email protected]', city 'Boston', signup_date '2024-06-01' (already exists - must be skipped)
  • customer_id 113, name 'Nadia Flores', email '[email protected]', city 'Austin', signup_date '2024-06-01' (new)

Note: Your change is verified automatically and rolled back afterwards.

This challenge changes data. Your statement runs on a private copy of the database, then we check the resulting table.

Tables

customers 12 rows
  • customer_id integer PK
  • name text
  • email text
  • city text
  • signup_date text
customer_idnameemailcitysignup_date
101Alice Mitchell[email protected]San Francisco2024-01-15
102Bob Chen[email protected]New York2024-02-20
103Carol Davis[email protected]Los Angeles2024-03-10

Keep going