Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
ALTER and INSERT Operations | DDL and DML in SQL
Intermediate SQL
course content

Course Content

Intermediate SQL

Intermediate SQL

1. Grouping
2. Nested Subqueries
3. Joining Tables
4. DDL and DML in SQL

bookALTER and INSERT Operations

In the previous chapter, we learned how to create tables.

But let's imagine a situation where we need to add a column to an existing table. It would be quite foolish to delete the table (especially if it already contains some data) and then create a new table, filling it again with data.

Therefore, in this chapter, we will look at the ALTER operation.

Let's see how to use this operation:

As you can see, this is the script for creating a table from the previous chapter.

Next, there are two ALTER operations. The first operation adds a price column to the table, setting the default value to 300 for this column. The second operation removes this column.

The syntax is extremely simple:

The syntax is actually quite simple.

Note

Using the ALTER statement, you can perform various schema-level operations on a table, such as adding or removing constraints, renaming, changing data types, and adding or dropping indexes.

Let's move on to another operation, namely the insertion operation.

INSERT

Data insertion in SQL can be done using the INSERT statement.

To use INSERT, we need to specify into which columns we want to add values.

Here's what the syntax of this statement looks like:

You might have correctly noticed that this is a snippet of the script from the previous chapter, where data is inserted into the library table.

Let's break down what's happening here:

  1. First, the keywords INSERT INTO are written, followed by the table_name into which the data will be inserted;
  2. Then, parentheses are opened, and the column names into which the data will be inserted are specified; in our case, there are 4 columns;
  3. After that, the keyword VALUES is written, and parentheses are opened where the data will be written;
  4. The data should be written in the same order as the column names were specified, and the data types must be observed. You cannot insert an integer value into a column with the VARCHAR data type;
  5. The parentheses are closed, and a comma is placed, thus filling one row. You can fill as many rows as you deem necessary using this method.

In summary, the generic syntax of the INSERT statement looks like this:

Don't forget about the semicolon in the end!

Let's move on to the task!

Task

There is an empty table called employees with the following columns:

Yes, it's the same table as in the previous sections, but now this table doesn't contain any data (rows) at all.

Your task is to:

  1. Add a column country to this table, which will contain information about the country where the employee resides;
  2. Insert 2 rows of data into the table, which will look like this:
    • id=1, first_name=Emily, last_name=Torres, department=Operations, salary=80000, country=United Kingdom;
    • id=2, first_name=David, last_name=Bobr, department=Engineering, salary=95000, country=Poland.

To accomplish this task, use ALTER TABLE for the first subtask and INSERT for the second subtask.

Note

On the right side of the code editor, some code will already be written. Please do not delete or edit this code, as it is necessary to check the correctness of your solution.

Also, don't forget to use semicolons after each part!

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 2
toggle bottom row

bookALTER and INSERT Operations

In the previous chapter, we learned how to create tables.

But let's imagine a situation where we need to add a column to an existing table. It would be quite foolish to delete the table (especially if it already contains some data) and then create a new table, filling it again with data.

Therefore, in this chapter, we will look at the ALTER operation.

Let's see how to use this operation:

As you can see, this is the script for creating a table from the previous chapter.

Next, there are two ALTER operations. The first operation adds a price column to the table, setting the default value to 300 for this column. The second operation removes this column.

The syntax is extremely simple:

The syntax is actually quite simple.

Note

Using the ALTER statement, you can perform various schema-level operations on a table, such as adding or removing constraints, renaming, changing data types, and adding or dropping indexes.

Let's move on to another operation, namely the insertion operation.

INSERT

Data insertion in SQL can be done using the INSERT statement.

To use INSERT, we need to specify into which columns we want to add values.

Here's what the syntax of this statement looks like:

You might have correctly noticed that this is a snippet of the script from the previous chapter, where data is inserted into the library table.

Let's break down what's happening here:

  1. First, the keywords INSERT INTO are written, followed by the table_name into which the data will be inserted;
  2. Then, parentheses are opened, and the column names into which the data will be inserted are specified; in our case, there are 4 columns;
  3. After that, the keyword VALUES is written, and parentheses are opened where the data will be written;
  4. The data should be written in the same order as the column names were specified, and the data types must be observed. You cannot insert an integer value into a column with the VARCHAR data type;
  5. The parentheses are closed, and a comma is placed, thus filling one row. You can fill as many rows as you deem necessary using this method.

In summary, the generic syntax of the INSERT statement looks like this:

Don't forget about the semicolon in the end!

Let's move on to the task!

Task

There is an empty table called employees with the following columns:

Yes, it's the same table as in the previous sections, but now this table doesn't contain any data (rows) at all.

Your task is to:

  1. Add a column country to this table, which will contain information about the country where the employee resides;
  2. Insert 2 rows of data into the table, which will look like this:
    • id=1, first_name=Emily, last_name=Torres, department=Operations, salary=80000, country=United Kingdom;
    • id=2, first_name=David, last_name=Bobr, department=Engineering, salary=95000, country=Poland.

To accomplish this task, use ALTER TABLE for the first subtask and INSERT for the second subtask.

Note

On the right side of the code editor, some code will already be written. Please do not delete or edit this code, as it is necessary to check the correctness of your solution.

Also, don't forget to use semicolons after each part!

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 2
toggle bottom row

bookALTER and INSERT Operations

In the previous chapter, we learned how to create tables.

But let's imagine a situation where we need to add a column to an existing table. It would be quite foolish to delete the table (especially if it already contains some data) and then create a new table, filling it again with data.

Therefore, in this chapter, we will look at the ALTER operation.

Let's see how to use this operation:

As you can see, this is the script for creating a table from the previous chapter.

Next, there are two ALTER operations. The first operation adds a price column to the table, setting the default value to 300 for this column. The second operation removes this column.

The syntax is extremely simple:

The syntax is actually quite simple.

Note

Using the ALTER statement, you can perform various schema-level operations on a table, such as adding or removing constraints, renaming, changing data types, and adding or dropping indexes.

Let's move on to another operation, namely the insertion operation.

INSERT

Data insertion in SQL can be done using the INSERT statement.

To use INSERT, we need to specify into which columns we want to add values.

Here's what the syntax of this statement looks like:

You might have correctly noticed that this is a snippet of the script from the previous chapter, where data is inserted into the library table.

Let's break down what's happening here:

  1. First, the keywords INSERT INTO are written, followed by the table_name into which the data will be inserted;
  2. Then, parentheses are opened, and the column names into which the data will be inserted are specified; in our case, there are 4 columns;
  3. After that, the keyword VALUES is written, and parentheses are opened where the data will be written;
  4. The data should be written in the same order as the column names were specified, and the data types must be observed. You cannot insert an integer value into a column with the VARCHAR data type;
  5. The parentheses are closed, and a comma is placed, thus filling one row. You can fill as many rows as you deem necessary using this method.

In summary, the generic syntax of the INSERT statement looks like this:

Don't forget about the semicolon in the end!

Let's move on to the task!

Task

There is an empty table called employees with the following columns:

Yes, it's the same table as in the previous sections, but now this table doesn't contain any data (rows) at all.

Your task is to:

  1. Add a column country to this table, which will contain information about the country where the employee resides;
  2. Insert 2 rows of data into the table, which will look like this:
    • id=1, first_name=Emily, last_name=Torres, department=Operations, salary=80000, country=United Kingdom;
    • id=2, first_name=David, last_name=Bobr, department=Engineering, salary=95000, country=Poland.

To accomplish this task, use ALTER TABLE for the first subtask and INSERT for the second subtask.

Note

On the right side of the code editor, some code will already be written. Please do not delete or edit this code, as it is necessary to check the correctness of your solution.

Also, don't forget to use semicolons after each part!

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

In the previous chapter, we learned how to create tables.

But let's imagine a situation where we need to add a column to an existing table. It would be quite foolish to delete the table (especially if it already contains some data) and then create a new table, filling it again with data.

Therefore, in this chapter, we will look at the ALTER operation.

Let's see how to use this operation:

As you can see, this is the script for creating a table from the previous chapter.

Next, there are two ALTER operations. The first operation adds a price column to the table, setting the default value to 300 for this column. The second operation removes this column.

The syntax is extremely simple:

The syntax is actually quite simple.

Note

Using the ALTER statement, you can perform various schema-level operations on a table, such as adding or removing constraints, renaming, changing data types, and adding or dropping indexes.

Let's move on to another operation, namely the insertion operation.

INSERT

Data insertion in SQL can be done using the INSERT statement.

To use INSERT, we need to specify into which columns we want to add values.

Here's what the syntax of this statement looks like:

You might have correctly noticed that this is a snippet of the script from the previous chapter, where data is inserted into the library table.

Let's break down what's happening here:

  1. First, the keywords INSERT INTO are written, followed by the table_name into which the data will be inserted;
  2. Then, parentheses are opened, and the column names into which the data will be inserted are specified; in our case, there are 4 columns;
  3. After that, the keyword VALUES is written, and parentheses are opened where the data will be written;
  4. The data should be written in the same order as the column names were specified, and the data types must be observed. You cannot insert an integer value into a column with the VARCHAR data type;
  5. The parentheses are closed, and a comma is placed, thus filling one row. You can fill as many rows as you deem necessary using this method.

In summary, the generic syntax of the INSERT statement looks like this:

Don't forget about the semicolon in the end!

Let's move on to the task!

Task

There is an empty table called employees with the following columns:

Yes, it's the same table as in the previous sections, but now this table doesn't contain any data (rows) at all.

Your task is to:

  1. Add a column country to this table, which will contain information about the country where the employee resides;
  2. Insert 2 rows of data into the table, which will look like this:
    • id=1, first_name=Emily, last_name=Torres, department=Operations, salary=80000, country=United Kingdom;
    • id=2, first_name=David, last_name=Bobr, department=Engineering, salary=95000, country=Poland.

To accomplish this task, use ALTER TABLE for the first subtask and INSERT for the second subtask.

Note

On the right side of the code editor, some code will already be written. Please do not delete or edit this code, as it is necessary to check the correctness of your solution.

Also, don't forget to use semicolons after each part!

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 4. Chapter 2
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
some-alt