top of page

Snack-Sized Data Basics
SQL - Data Insert

There are several ways to import data into a SQL table. Some examples are:

  • using a SQL INSERT query

  • uploading an Excel or CSV file

  • using a built in SQL system tool

  • running a Python script

  • using an ETL tool

  • pulling data from an API

In this lesson, we'll focus on the SQL INSERT query method.

​​

dataleaflet-practice.gif

Now that you've learned how to create a table using PostgreSQL, let's add some data. Since we're using an online database, you may need to recreate your table. If so, refer back to Table Creation to set it up again.

​

Here is a SQL INSERT example.

​​

INSERT INTO students (first_name, last_name, date_of_birth, allergies) 
VALUES
   ('Alice', 'Johnson', '2015-04-15', 'Peanuts'),
   ('David', 'Lee', '2014-01-30', 'Gluten'),
   ('Eva', 'Martinez', '2014-12-05', 'Shellfish');

​

Let's break it down.

  • INSERT INTO is the standard SQL command for adding data into a table.

  • students is the name of our table.

  • The parentheses ( ) enclose the names of the columns we’ll be inserting data into. Note that we didn’t specify the student_id column because we used SERIAL to automatically generate a unique number for each record.

  • VALUES indicates where we begin entering the actual data for the table.

  • Each row of data is enclosed in parentheses ( ), with each column’s value separated by a comma. The data provided must match the order of the columns listed.

    • For example, in the INSERT statement, we specified the columns (first_name, last_name, date_of_birth, allergies) and the first row of data is ('Alice', 'Johnson', '2015-04-15', 'Peanuts').

      • 'Alice' will be going into the first_name column

      • 'Johnson' will be going into the last_name column

      • '2015-04-15' will be going into the date_of_birth column

      • 'Peanuts' will be going into the allergies column

    • One or more records can be inserted at a time. When inserting multiple rows, a comma is used to separate each record.

  • Finally, the semi colon ; is used to terminate the query.

This is a good stopping point. In the next lesson, we’ll cover examples of incorrect data that would cause errors based on our table creation statement. For now, head over to sqliteonline.com to practice inserting data into the students table, and refer back to Getting Started and Table Creation if needed.

bottom of page