INSERT Patterns: Functions
Module: Data Modification & Transactions
INSERT syntax patterns: (1) Single: INSERT INTO table (col1, col2) VALUES (val1, val2) - adds 1 row. (2) Bulk: INSERT INTO table VALUES (row1), (row2), (row3) - adds multiple rows, 10-100x faster. (3) INSERT...SELECT: INSERT INTO table SELECT * FROM source WHERE condition - copies data from query. (4) RETURNING: INSERT...RETURNING col1, col2 (PostgreSQL) - returns inserted data. (5) OUTPUT: INSERT...OUTPUT INSERTED.col1 (SQL Server) - captures inserted rows. (6) COPY: COPY table FROM file (PostgreSQL) - fastest bulk load. (7) LOAD DATA: LOAD DATA INFILE (MySQL) - fast CSV import. (8) BULK INSERT: BULK INSERT table FROM file (SQL Server) - high-speed import.
Single INSERT: INSERT INTO table (col1, col2) VALUES (val1, val2) - adds 1 row, simple but slow for many rows
Bulk INSERT: INSERT INTO table VALUES (row1), (row2), (row3) - adds multiple rows in 1 statement, 10-100x faster
INSERT...SELECT: INSERT INTO target SELECT * FROM source WHERE condition - copies data from query, 100x faster than row-by-row
RETURNING (PostgreSQL): INSERT...RETURNING col1, col2 - returns inserted data without extra SELECT
OUTPUT (SQL Server): INSERT...OUTPUT INSERTED.col1, INSERTED.col2 - captures inserted rows
LAST_INSERT_ID (MySQL): SELECT LAST_INSERT_ID() after INSERT - gets last auto-increment ID
COPY (PostgreSQL): COPY table FROM file WITH (FORMAT csv) - fastest bulk load, 100x faster than INSERT
LOAD DATA (MySQL): LOAD DATA INFILE file INTO TABLE table - fast CSV import
BULK INSERT (SQL Server): BULK INSERT table FROM file - high-speed import
RETURNING clause returns inserted data, COPY for fastest bulk load, UNLOGGED tables for fast writes, ON CONFLICT for upsert
LAST_INSERT_ID() for auto-increment, LOAD DATA INFILE for bulk load, ON DUPLICATE KEY UPDATE for upsert, max_allowed_packet limits bulk size
OUTPUT clause captures inserted rows, BULK INSERT for fast import, MERGE for upsert, 1000 row limit per VALUES clause
RETURNING INTO for PL/SQL, SQL*Loader for bulk load, MERGE for upsert, multi-table INSERT (INSERT ALL)
Core references in this topic include WHERE, =, <, >, <=, >=. Learn what each one does, when to use it, and the execution or engine rules that matter.
WHERE
Filters rows before projection and sorting. It decides which rows continue through the query pipeline.
SELECT ... FROM table WHERE condition;
Most performance issues start with a weak WHERE clause or a missing supporting index.
=
Returns rows where the left and right values are exactly equal.
column = value
Use with exact matches. Do not use = NULL.
<, >, <=, >=
Range comparison operators for less-than, greater-than, and inclusive boundary checks.
salary >= 80000
IS NULL / IS NOT NULL
Tests whether a value is missing. SQL NULL semantics require dedicated NULL predicates.
manager_id IS NULL
Never use = NULL or != NULL.
BETWEEN
Checks whether a value falls inside an inclusive lower/upper range.
order_total BETWEEN 100 AND 500
ANY / ALL
Compares one value against every or at least one value from a subquery result.
salary > ALL (SELECT salary FROM interns)
TIME
Stores a time-of-day value without any date component.
TIME '14:30:00'
TIMESTAMP