Seth Barrett

Daily Blog Post: July 21st, 2023

post

July 21st, 2023

SQL Update Statements: Modifying Data in Tables

Welcome back to our SQL blog series! In the last post, we covered how to retrieve data from a table using the SELECT command, and how to filter, sort, and limit the data retrieved from a table. In this post, we will cover how to update data in a table using the UPDATE command.

Updating Data in a Table

To update data in a table using SQL, you can use the UPDATE command. Here's an example:

UPDATE mytable SET age = 30 WHERE name = 'John Doe';

This command updates the "age" column to 30 for the row in the "mytable" table where the "name" column is "John Doe". You can also update multiple columns at once by separating them with commas. Here's an example:

UPDATE mytable SET age = 30, name = 'Jane Doe' WHERE id = 1;

This command updates the "age" column to 30 and the "name" column to "Jane Doe" for the row in the "mytable" table where the "id" column is 1.

Updating Multiple Rows

You can update multiple rows in a table using the WHERE clause. Here's an example:

UPDATE mytable SET age = 30 WHERE age > 25;

This command updates the "age" column to 30 for all rows in the "mytable" table where the "age" column is greater than 25.

Updating with Calculations

You can also update data in a table using calculations. Here's an example:

UPDATE mytable SET age = age + 5 WHERE name = 'Jane Doe';

This command updates the "age" column by adding 5 to its current value for the row in the "mytable" table where the "name" column is "Jane Doe".

Conclusion

In this post, we covered how to update data in a table using the UPDATE command. We also covered how to update multiple rows, update with calculations, and update multiple columns at once. In the next post, we will cover how to delete data from a table using the DELETE command. Stay tuned!