Section 1 - Instruction

Remember learning INSERT and UPDATE? Sometimes you need both operations: insert a row if it doesn't exist, or update it if it does.

This "insert or update" pattern is called UPSERT.

Engagement Message

When might you want to insert OR update the same row?

Section 2 - Instruction

Imagine adding a new customer to your database. But what if they already exist? A regular INSERT would fail with a duplicate key error.

The ON CONFLICT command lets you handle both scenarios gracefully in one command.

Engagement Message

What happens if an INSERT fails due to duplicates?

Section 3 - Instruction

Think of ON CONFLICT like a smart doorman. If someone's not on the guest list, add them. If they're already there, update their information.

One command handles both possibilities without errors or duplicate logic.

Engagement Message

What's one advantage of using one INSERT ... ON CONFLICT instead of separate INSERT and UPDATEs?

Section 4 - Instruction

The basic UPSERT pattern in PostgreSQL looks like:

It checks for conflicts on the specified column(s) and decides whether to insert or update.

Engagement Message

Which part of this statement determines if the row will be inserted or updated?

Section 5 - Instruction

Here's a practical example with PostgreSQL syntax:

This inserts or updates based on the id column.

Engagement Message

What happens if product id 1 already exists in the table?

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal