SQL SQL Tutorial SQL Database



SQL Update

SQL Update is a command used to modify the existing data in a table. It is one of the most commonly used commands in SQL. The Update command is used to change the values of one or more columns in a table. It is used to update the data in a table based on certain conditions.

The syntax for the Update command is as follows:

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

The Update command consists of three parts:

  • table_name: The name of the table that you want to update.
  • column1, column2, ...: The columns that you want to update.
  • value1, value2, ...: The new values that you want to set for the columns.
  • condition: The condition that must be met in order for the update to take place.

Let's take a look at some examples of how to use the Update command.

Example 1:

Suppose we have a table called "employees" with the following data:

id name age salary
1 John 25 50000
2 Jane 30 60000
3 Bob 35 70000

We want to update the salary of employee with id 2 to 65000. The SQL code for this would be:

UPDATE employees
SET salary = 65000
WHERE id = 2;

After running this command, the "employees" table would look like this:

id name age salary
1 John 25 50000
2 Jane 30 65000
3 Bob 35 70000

Example 2:

Suppose we have a table called "students" with the following data:

id name age grade
1 John 18 A
2 Jane 19 B
3 Bob 20 C

We want to update the grade of all students who are older than 18 to "B". The SQL code for this would be:

UPDATE students
SET grade = 'B'
WHERE age > 18;

After running this command, the "students" table would look like this:

id name age grade
1 John 18 A
2 Jane 19 B
3 Bob 20 B

As you can see, the Update command is a powerful tool for modifying data in a table. It allows you to change the values of one or more columns based on certain conditions. By using the Update command, you can easily update large amounts of data in a table with just a few lines of code.

References:

Activity