- 01Getting Started
- 02SELECT Statement Basics
- 03Filtering Rows with WHERE
- 04Sorting Results with ORDER BY
- 05Combining Tables with JOIN
- 06Aggregating Data with GROUP BY
- 07Fuzzy Matching with LIKE
- 08Modifying Data with INSERT, UPDATE, and DELETE
- 09Introduction to Subqueries
- 10Creating a VIEW
- 11Ranking Rows with Window Functions
- 12Understanding Transactions
- 13Writing Comments in SQL
- 14Logical Operators: AND, OR, NOT
- 15Working with NULL Values
- 16INNER JOIN vs. LEFT JOIN
- 17Filtering Aggregates with HAVING
- 18Removing Duplicates with DISTINCT
- 19Combining Results with UNION
- 20Conditional Values with CASE
- 21Checking Existence with EXISTS
- 22Changing Table Structure with ALTER TABLE
- 23Self Joins
- 24Speeding Up Searches with INDEX
- 25GROUP BY with Multiple Columns
- 26Pagination with LIMIT and OFFSET
- 27Pattern Matching with GLOB
- 28Date Calculations with the date() Function
- 29Joining Three Tables
- 30Matching Multiple Values with IN
- 31Derived Tables: Subqueries in FROM
- 32The Basics of TRIGGER
- 33Readable Queries with WITH (CTEs)
- 34UPSERT: Insert or Update in One Statement
- 35Combining MIN, MAX, and AVG
- 36Restricting Input with CHECK Constraints
- 37Auto-Numbering with AUTOINCREMENT
- 38Inspecting Query Plans with EXPLAIN QUERY PLAN
- 39ORDER BY with Multiple Columns
- 40Conditional Aggregation with CASE
- 41Removing Objects with DROP TABLE and DROP VIEW
- 42Combining Multiple CTEs
- 43Date Range Search with BETWEEN
- 44Finding What's Missing with NOT EXISTS
- 45Substituting NULL with COALESCE
- 46String Functions: UPPER, LENGTH, and SUBSTR
- 47Converting Types with CAST
- 48Merging Grouped Values with GROUP_CONCAT
- 49Enforcing Integrity with FOREIGN KEY
- 50Capstone: Build a Mini Library Management System
UPSERT: Insert or Update in One Statement
This lesson covers UPSERT — doing "insert if missing, update if present" in one statement — for efficient data registration. It's for anyone searching "SQL UPSERT tutorial."
INSERT ... ON CONFLICT DO UPDATE performs "insert if it doesn't exist, update if it does" as a single statement (this pattern is called UPSERT) — handy for something like restocking inventory, where you want to add to an existing quantity.
The example tries to insert a product with id = 1, which already exists, and ON CONFLICT(id) DO UPDATE SET stock = stock + excluded.stock; handles the conflict by adding to the existing stock instead. excluded is a special keyword referring to "the value that was just about to be inserted."
A common early mistake is forgetting that the column named in ON CONFLICT (here, id) needs a uniqueness constraint like PRIMARY KEY already defined — without it, there's no way for a "conflict" to even be detected. Doing this in one step, rather than "check if it exists, then decide whether to insert or update," is not just more concise — it also closes the window where another process could sneak in a change between your check and your action.
💡 The SQL engine may take a few seconds to load the first time you run code.
