It's Autumn! Prices fall like leaves! - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Using CTEs with data-modifying statements
6. INSERT statement
Summary

Instruction

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.

Exercise

We have a new customer: Anne Rogers from San Diego. She is 28 years old, has an ID equal to 15, and a discount equal to 0. Add her details to the database.

Stuck? Here's a hint!

Use INSERT INTO ... VALUES ...