update table

How To Update Data in Table in SQL

In this SQL (structured query language) guide, you will be aware of how to update the data in the column of a table in SQL, updating multiple columns’ data, updating data with conditional keywords like WHERE.

Before starting to learn about updating data into the table’s cell wherever we want. I am assuming that you have learned the process of creating a database, creating a table & inserting data in the table’s columns.

If you have not learned them yet then first go for it.

To make you better understand, we will start from scratch. So keep getting ~

  1. Create a database
CREATE DATABASE jai_school;
  1. Create a table-
CREATE TABLE users(name VARCHAR(50),state VARCHAR(25),gender VARCHAR(15),mon_income INT(20));
  1. Insert data in the tableusers“-
INSERT INTO users(name,state,gender,mon_income)VALUES("Ram","UP","Male",50000);
INSERT INTO users(name,state,gender,mon_income)VALUES("Rahim","MP","Male",20000);
INSERT INTO users(name,state,gender,mon_income)VALUES("Shyam","UP","Male",40000);
INSERT INTO users(name,state,gender,mon_income)VALUES("Anita","UK","Female",80000);
INSERT INTO users(name,state,gender,mon_income)VALUES("Jai","RJ","Male",70000);
INSERT INTO users(name,state,gender,mon_income)VALUES("Ravina","Delhi","Female",20000);
INSERT INTO users(name,state,gender,mon_income)VALUES("Aman","RJ","Male",20000);

This table will look like-

sql_table - jaischool.com

Update a single column

Now if I want to update a single column of the users table then the query structure will be like this:-

UPDATE users SET name="Gary";

This means the name column from the users table will be updated with the new string you used in the query (Ex.- Gary).

Result-

update whole single column

See in the image above, you find that the whole column has been updated with Gary. So what when I want to only update the name of the Ram person then-


Update data in the specific cell in the single column

Now, I am resetting the table in its previous form. and going to update the name of Ram. Then you need to make use of WHERE conditional keyword.

UPDATE users SET name="GaryVee" WHERE name="Ram";

Result-

update-specific-cell-in-a-table

Or (comparison with another column) –

UPDATE users SET name="GaryVee" WHERE state="UP";
update all cells in a column with where

With the use of this above query, the name column will be updated only of those who belong to UP state.
See Image


Or (comparison with other multiple columns)

UPDATE users SET name="GaryVee" WHERE state="UP" AND gender="Male" AND mon_income="50000";
update-specific-cell-in-a-table

This above query updates the data in the name column only of those who belong to UP state with gender Male and a monthly income (mon_income) of 50000.
See Image


Update data in multiple columns with a single query

You can update multiple columns by separating them with a comma (,).

UPDATE users SET name="jaischool", state="Rajasthan" WHERE mon_income="70000";

Result-

update multiple columns by condition

or update a whole single row

UPDATE users SET name="aman kumar", state="rajsthan (RJ)", gender="male", mon_income="33260" WHERE name="aman" AND mon_income="20000";

Result –

update whole row

Leave a Comment