INSERT

SQL · Modifying Data

Theory

INSERT adds new rows to a table. The usual form names the columns and then gives one set of values per row: INSERT INTO employees (name, salary) VALUES ('Asha Rao', 65000). Columns you leave out get their DEFAULT, an identity value, or NULL. Several rows can be added in one statement by separating value lists with commas, and INSERT INTO target (cols) SELECT ... copies rows from a query.

Every constraint is checked: a duplicate primary key, a NULL in a NOT NULL column or a failed CHECK makes the whole statement fail, and in a multi-row insert none of the rows are kept. Values are matched to columns by position, so always list the columns; without the list you must supply every column in table order, which breaks when the table changes. Engines also offer upsert forms such as MERGE, INSERT ... ON CONFLICT (PostgreSQL, SQLite) or ON DUPLICATE KEY UPDATE (MySQL).

Why we use it

Tables start empty and keep growing as new customers sign up and new orders arrive. INSERT is how every new row gets into the database, whether typed by hand, sent by an app or copied from another table.

When to use it

Use INSERT ... VALUES for a few rows and INSERT ... SELECT to copy or transform data in bulk. For very large file loads, use the engine's bulk loader (COPY, BULK INSERT, LOAD DATA). Use MERGE or upsert when a row may already exist.

How it works

  1. INSERT INTO employees (cols) VALUES (...)
  2. Match each value to its column by position
  3. Fill omitted columns with DEFAULT, identity or NULL
  4. Do all constraints pass? Yes: Add the row to the table. No: Reject the whole statement.
  5. Table has new rows

Syntax

INSERT INTO table (col1, col2)
VALUES (val1, val2);

Example

INSERT INTO employees (name, salary)
VALUES ('Asha Rao', 65000);

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic