Let's get started using CTEs with data-modifying statements like INSERT
and UPDATE
. First, let's review the syntax of the INSERT
statement. This statement is used to add data to the table. For example:
INSERT INTO grocery_store (id, name, city)
VALUES (9, 'Small Grocery Store', 'Washington');
The statement above inserts a new row into the table grocery_store
.
The statement starts with INSERT INTO
, followed by the table name, and then the column names in brackets. Following these is the keyword VALUES
, and in brackets is the list of values for the new row. Notice that column names and values are separated by commas. The values must be in exactly the same order as the column names.
There is also a shorter version of the INSERT
statement that you can use if you're putting data into all of the table columns. Take a look:
INSERT INTO grocery_store
VALUES (9, 'Small Grocery Store', 'Washington');
In this variation, you omit the list of column names and enter only the values. In this case, you must list the values in exactly the same order as the columns appear in the table.