INSERT

SQL command to insert rows in a given table. Rows to be inserted can be expressed via a VALUES clause or via a dedicated query.

Syntax

INSERT INTO tableName ( columnName , ) VALUES ( literal , ) , query

Parameters

tableName

The name of the target table

columnName

The name of one of the target column in the target table

literal

A literal whose type must be compatible with the target column

query

A query whose result can be used to insert into the target table

Examples

Insert a single row

SELECT * FROM T;

A

B

C

1

"one"

1.0

2

"two"

2.0

INSERT INTO T VALUES (3, 'three', 3.0);

SELECT * FROM T;

A

B

C

1

"one"

1.0

2

"two"

2.0

3

"three"

3.0

Insert multiple rows

SELECT * FROM T;

A

B

C

1

"one"

1.0

2

"two"

2.0

INSERT INTO T VALUES (3, 'three', 3.0), (4, 'four', 4.0);

SELECT * FROM T;

A

B

C

1

"one"

1.0

2

"two"

2.0

3

"three"

3.0

4

"four"

4.0

Insert new rows without specifying all columns

SELECT * FROM T;

A

B

C

1

"one"

1.0

2

"two"

2.0

INSERT INTO T(A, B) VALUES (3, 'three'), (4, 'four');

SELECT * FROM T;

A

B

C

1

"one"

1.0

2

"two"

2.0

3

"three"

NULL

4

"four"

NULL

Insert rows in a table with a STRUCT column

SELECT * FROM T;

A

B

C

1

"one"

{ "S1": 10 , "S2": 100 }

2

"two"

{ "S1": 20 , "S2": 200 }

INSERT INTO T VALUES (3, 'three', (30, 300)), (4, 'four', (40, 400));

SELECT * FROM T;

A

B

C

1

"one"

{ "S1": 10 , "S2": 100 }

2

"two"

{ "S1": 20 , "S2": 200 }

3

"three"

{ "S1": 30 , "S2": 300 }

4

"four"

{ "S1": 40 , "S2": 400 }

Insert rows in a table with an ARRAY column

SELECT * FROM T;

A

B

C

1

"one"

[ 1 ]

2

"two"

[2, 20]

INSERT INTO T VALUES (3, 'three', [30, 300, 3000]), (4, 'four', [40, 400, 4000, 40000]);

SELECT * FROM T;

A

B

C

1

"one"

[ 1 ]

2

"two"

[2, 20]

3

"three"

[3, 30, 300]

4

"four"

[4, 40, 400, 4000]

Insert from query

SELECT * FROM T;

A

B

C

1

"one"

{ "S1": 10 , "S2": 100 }

2

"two"

{ "S1": 20 , "S2": 200 }

INSERT INTO T SELECT 3, B, C FROM T WHERE C.S1 = 20

SELECT * FROM T;

A

B

C

1

"one"

{ "S1": 10 , "S2": 100 }

2

"two"

{ "S1": 20 , "S2": 200 }

3

"two"

{ "S1": 20 , "S2": 200 }