SQL INSERT INTO: A Comprehensive Guide for Beginners and Intermediates 🎯

beginner
15 min

SQL INSERT INTO: A Comprehensive Guide for Beginners and Intermediates 🎯

Welcome to the SQL INSERT INTO tutorial! This guide is designed to help you understand and master the INSERT INTO SQL command, essential for adding new records to your databases. Let's dive in! 📝

What is SQL INSERT INTO?

The INSERT INTO SQL statement is used to insert new records (rows) into a database table. This command allows you to populate your tables with data, making them useful in real-world projects. 💡

Basic Syntax of SQL INSERT INTO

The simplest form of the INSERT INTO statement is as follows:

sql
INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);

Here's a breakdown of the syntax:

  • table_name: The name of the table you want to insert data into.
  • column1, column2, column3, ... : The names of the columns (fields) in which you want to store the data.
  • value1, value2, value3, ... : The actual data that you want to insert into the table.

Example:

sql
INSERT INTO students (name, age, subject) VALUES ('John Doe', 25, 'Computer Science');

In this example, we're inserting a new student named "John Doe", who is 25 years old and studying Computer Science, into the students table. ✅

Inserting Multiple Rows at Once

You can insert multiple rows at once using the INSERT INTO statement by providing multiple sets of values within the VALUES clause.

sql
INSERT INTO students (name, age, subject) VALUES ('Alice', 23, 'Mathematics'), ('Bob', 22, 'Physics'), ('Charlie', 21, 'English');

In this example, we're adding three new students to the students table at once. ✅

Inserting Data with Default Values

When creating a table, you can define default values for columns. If you omit a column value during an INSERT INTO operation, the default value will be used.

sql
CREATE TABLE students ( id INT AUTO_INCREMENT, name VARCHAR(50) NOT NULL, age INT DEFAULT 0, subject VARCHAR(50) DEFAULT 'Unknown', PRIMARY KEY (id) );

In this example, we're creating a students table with four columns: id, name, age, and subject. The age and subject columns have default values of 0 and 'Unknown', respectively.

Example:

sql
INSERT INTO students (name) VALUES ('David');

In this example, we're inserting a new student named "David" into the students table. Since we've omitted the age and subject columns, their default values (0 and 'Unknown', respectively) will be used. ✅

Inserting Data from Another Table (SELECT Statement)

You can also use the SELECT statement to insert data from one table into another table.

sql
INSERT INTO target_table SELECT column1, column2, ... FROM source_table WHERE condition;

Here's a breakdown of the syntax:

  • target_table: The table where you want to insert the data.
  • column1, column2, ... : The names of the columns you want to copy from the source table.
  • source_table: The table you want to copy data from.
  • condition: An optional condition that filters the data to be inserted.

Example:

sql
INSERT INTO older_students SELECT * FROM students WHERE age > 20;

In this example, we're creating a new table called older_students and copying all columns from the students table where the age is greater than 20. This way, we can easily separate older students from the rest. ✅

Quiz

Quick Quiz
Question 1 of 1

What is the purpose of the SQL `INSERT INTO` statement?