insert data into redshift

How To Insert Data into Redshift Table

Sometimes you may need to insert data into Redshift table. In this article, we will look at how to insert data into redshift table. You can use it to insert multiple rows and even insert into identity column.


How To Insert Data into Redshift Table

Here are the steps to insert data into Redshift table. Let us say we have the following table in redshift employees(id, first_name, last_name)

postgres-# create table employees(id int,
   first_name varchar(10),
   last_name varchar(10));

Here is the syntax to insert data into redshift table

insert into table_name(column_list)
values(value_list1),
(value_list2),
...

In the above query, you need to specify table name, comma-separated list of columns, and comma-separated list of values for each row that you want to insert. Although mentioning column list after table name is optional, it is recommended to avoid errors due to mismatch in number of columns and column names.

Also read : How to Alter column from NULL to NOT NULL

Here is the SQL query to insert one row into employees table.

postgres-# insert into employees(id, first_name, last_name)
           values(1, 'John','Doe');
postgres-# select * from employees;
 id | first_name | last_name
----+------------+-----------
  1 | John       | Doe

Also read : How to Calculate Running Total in Redshift


Insert Multiple Rows in Redshift

If you want to enter multiple rows, you just need to provide multiple set of values, enclosed in round brackets ‘( )’. Here is an example to insert multiple rows in redshift.

postgres-# insert into employees(id, first_name, last_name)
           values(2, 'Jim','Doe'),
                 (3, 'Tim','Doe');

postgres-# select * from employees;
 id | first_name | last_name
----+------------+-----------
  1 | John       | Doe
  2 | Jim        | Doe
  3 | Tim        | Doe

Also read : How to Calculate Moving Average in Redshift

Leave a Reply

Your email address will not be published. Required fields are marked *