How to insert data into Redshift tables.

Below is the syntax to insert a row into redshift table using sql statement. The list of values should be in the same order as that of columns within the table and the number of values should match the number or columns in the table.

Insert into schemaname.tablename values (valcol1,valcol2,valcol3,valcol4,...);

You can use ‘default’ key word to insert default values into columns as shown below

Insert into schemaname.tablename values (valcol1,default,default,valcol4,...);

If all the not null columns in a table have default values defined, below insert statement can be used. It inserts default values into the columns that have default values defined and inserts null into the columns that do not have not null constraint.

Insert into schemaname.tablename default values;

If there are any not null columns in the table without a default value, above insert statement will throw an error.

To insert the result set of a sql query into a table, use the below syntax. The ordering of columns in the select statement should be same as that of the column list of the table.

Insert into schemaname.tablename (select statement);

Below is the syntax to insert multiple rows with one insert statement:

insert into schemaname.tablename values
(val1col1, default, val1col3, default),
(val2col1, default, val2col3, val2col4),
(val3col1, val3col2, val3col3, val3col4);
See also  Redshift - RANK Vs DENSE_RANK Window functions