INSERT (Insert Rows)

Last updated: 2026-11-04

INSERT (Insert Rows)

INSERT adds new rows to a table. It accepts multiple rows in a single statement — a multi-row VALUES list is far faster than repeated single-row inserts — and it can also copy rows from a query with INSERT ... SELECT. The RETURNING clause hands back the freshly inserted rows, while ON CONFLICT handles unique-key collisions.

Kindcommand
Example LanguageSQL

📝 Syntax

INSERT INTO <table> [(<column>, ...)]
VALUES (<value>, ...), (<value>, ...), ...
[ON CONFLICT (<conflict target>) DO NOTHING | DO UPDATE SET ...]
[RETURNING <column or expression>]

⚙️ Parameters

INTOThe table rows are inserted into
VALUESThe data to insert; may contain several tuples
ON CONFLICTWhat to do when a unique key conflicts
RETURNINGReturns the inserted rows

Returns:The number of affected rows; with RETURNING, the inserted rows are returned as well.

▶ Example

Runs real PostgreSQL 18 right in your browser (PGlite WASM, nothing to install). Press ▶ Run and the result appears in the panel below.

Output:

DROP TABLE
CREATE TABLE
id |        name         | price
---+---------------------+------
 1 | Mechanical Keyboard | 79.99
 2 | USB-C Cable         | 12.50
 3 | Monitor Stand       | 49.00

📌 Key Points

More case studies coming soon.

📚 Related Reference

❓ FAQ

QWhat is the difference between INSERT and INSERT ... SELECT?
AThe former inserts literal VALUES, the latter copies an entire query result — handy for moving data between tables.
QHow do I get the generated id right after inserting?
AUse RETURNING id: the statement hands the new row straight back, and with ON CONFLICT it returns the row after conflict handling.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏