The UPDATE statement could be used in order to modify the records that already exist in any table. The UPDATE statement is basically an SQL (Structured Query Language) statement that can be used to update or change the values in a table. This statement is usually suffixed with the WHERE clause in order to restrict the changes occurring on a set of values meeting a specific set of criteria.
In this article, we will dive deeper into the UPDATE Statement in SQL according to the GATE Syllabus for (Computer Science Engineering) CSE. Keep reading ahead to learn more.
Table of Contents
What is an UPDATE Statement in SQL?
In the SQL language, the UPDATE statement can be used to update the data in an existing table of the database. We can use the UPDATE statement in order to update single or numerous columns on the basis of our needs.
Syntax
UPDATE name_of_table SET column_A = value_A, column_B = value_B,…
WHERE condition;
name_of_table: the name of the table
column_A: the name of the first column
value_A: the new value for the first column
column_B: the name of the second column
value_B: the new value for the second column
condition: The condition is used to select the rows for which the column values need to be updated.
NOTE: The SET statement can be used in the above query in order to assign new values to a specific column, and the WHERE clause is used to select the rows for which the columns need to be updated. If we don’t utilise the WHERE clause, then all of the columns present in the rows will be updated. Hence, the WHERE clause is utilised to select certain rows.
ID_NO | NAME | ADDRESS | MOBILE | Age |
D1 | RAM | DELHI | xxxxxxxxxx | 28 |
D2 | RAMESH | GURGAON | xxxxxxxxxx | 28 |
D3 | SUJIT | ROHTAK | xxxxxxxxxx | 30 |
D4 | SURESH | DELHI | xxxxxxxxxx | 28 |
D3 | PRATIK | ROHTAK | xxxxxxxxxx | 30 |
D2 | RAMESH | GURGAON | xxxxxxxxxx | 28 |
Example Queries
Updating a Single Column
If we want to update the column NAME by setting the value to ‘PRATIK’ in all of the rows where the Age is 30, we will do this:
UPDATE Employee SET NAME = ‘PRATIK’ WHERE Age = 20;
Output:
This query would update two of the rows (the third and the fifth row). Thus, the Employee table will now look like this:
ID_NO | NAME | ADDRESS | MOBILE | Age |
D1 | RAM | DELHI | xxxxxxxxxx | 28 |
D2 | RAMESH | GURGAON | xxxxxxxxxx | 28 |
D3 | PRATIK | ROHTAK | xxxxxxxxxx | 30 |
D4 | SURESH | DELHI | xxxxxxxxxx | 28 |
D3 | PRATIK | ROHTAK | xxxxxxxxxx | 30 |
D2 | RAMESH | GURGAON | xxxxxxxxxx | 28 |
Updating Multiple Columns
If we want to update the column’s NAME to ‘PRATIK’ and the ADDRESS to ‘MANIPAL’ where ID_NO is 1, we will do this:
UPDATE Employee SET NAME = ‘PRATIK’, ADDRESS = ‘MANIPAL’ WHERE ID_NO = 1;
Output:
The query given above would update two of the columns in the first row. Thus, the Employee table will now look like this:
ID_NO | NAME | ADDRESS | MOBILE | Age |
D1 | PRATIK | MANIPAL | xxxxxxxxxx | 28 |
D2 | RAMESH | GURGAON | xxxxxxxxxx | 28 |
D3 | PRATIK | ROHTAK | xxxxxxxxxx | 30 |
D4 | SURESH | DELHI | xxxxxxxxxx | 28 |
D3 | PRATIK | ROHTAK | xxxxxxxxxx | 30 |
D2 | RAMESH | GURGAON | xxxxxxxxxx | 28 |
Note: Since we wanted to update multiple columns, we have utilised a comma(,) to separate the values and names of two columns.
Omitting WHERE Clause
In case we ultimately omit the WHERE clause present in the update query, all the rows will be updated.
UPDATE Employee SET NAME = ‘PRATIK’;
Output:
The Employee table would now look like this:
ID_NO | NAME | ADDRESS | MOBILE | Age |
D1 | PRATIK | DELHI | xxxxxxxxxx | 28 |
D2 | PRATIK | GURGAON | xxxxxxxxxx | 28 |
D3 | PRATIK | ROHTAK | xxxxxxxxxx | 30 |
D4 | PRATIK | DELHI | xxxxxxxxxx | 28 |
D3 | PRATIK | ROHTAK | xxxxxxxxxx | 30 |
D2 | PRATIK | GURGAON | xxxxxxxxxx | 28 |
Keep learning and stay tuned to get the latest updates on the GATE Exam along with Eligibility Criteria, GATE Syllabus for CSE (Computer Science Engineering), GATE CSE Notes, GATE CSE Question Paper, and more.
Also Explore,
Comments