×

SQL SET Keyword

This article will provide you a good understanding of the Set keyword in Structured Query Language.

What is the SET keyword?

The SET keyword is used to specify values for the variables. This keyword in SQL is basically used with UPDATE Keyword for specifying the columns that are to be modified or changed in a specific table.

Syntax:

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

Examples of SQL Set Keyword

Example 1: Let us understand the working of the SET keyword in MYSQL with the help of the this example.

In this example, we create a table called Employee which holds basic details of the Employee’s name, salary, age, etc.

CREATE TABLE employee( emp_id INT AUTO_INCREMENT,
                                          emp_name VARCHAR (100),
                                          city VARCHAR(200),
                                          age INT(100)
                                          salary INT(50)
                                    );
INSERT INTO employee VALUES(‘Suresh’, ‘Hyderabad’,30,20000 ),
(‘Shreya Singh’, ‘Lucknow’,27, 25000),
(‘Kavitha Kulkarni ’, ‘Delhi’ , 33, 60000),
(‘Raj Pawar’, ‘Amritsar’, 30, 50000),
(‘Jay Sharma’, ‘Bangalore’, 25, 40000),
(‘Sravan Kumar’, ‘Chennai’, 30, 20000),
(‘Ranjith Kumar’, ‘Cochin, 45, 50000),
(‘Harish’, ‘Vizag’, 37, 35000),
(‘Rajnish’, ‘Mumbai’, 25, 30000),
(‘Pritam Sharma’, ‘Pune’, 32, 45000);
SELECT * FROM employee;
emp_idemp_namecityagesalary
1SureshHyderabad3020000
2Shreya SinghLucknow2725000
3Kavitha KulkarniDelhi3360000
4Raj PawarAmritsar3050000
5Jay SharmaBangalore2540000
6Sravan KumarChennai3020000
7Ranjith KumarCochin4550000
8HarishVizag3735000
9RajnishMumbai2530000
10Pritam SharmaPune3245000

Now, we are going to apply the update statement with the SET statement.

UPDATE employee SET city = ‘Bhopal’, salary = 65000 WHERE emp_id = 7;

Now, run the following query to see the modifications in the table:

SELECT * FROM employee;

Output:

emp_idemp_namecityagesalary
1SureshHyderabad3020000
2Shreya SinghLucknow2725000
3Kavitha KulkarniDelhi3360000
4Raj PawarAmritsar3050000
5Jay SharmaBangalore2540000
6Sravan KumarChennai3020000
7Ranjith KumarBhopal4565000
8HarishVizag3735000
9RajnishMumbai2530000
10Pritam SharmaPune3245000

In the above table, we have seen that the city and salary of the employee with id 7 have been successfully updated.

Example 2:

CREATE TABLE Player_info(
	ID INT,
	First_Name VARCHAR(255),
	Last_Name VARCHAR(255),
	Year_Of_Birth INT,
	DayOfYear_Of_Birth INT,
	Place_Of_Birth VARCHAR(255),
	Country VARCHAR(255),
	PRIMARY KEY (ID)
);	
INSERT INTO Player_info VALUES(1, 'Sanju', 'Samson', 1981, 340, 'Kerala', 'India'),
INSERT INTO Player_info VALUES(2, 'Jonathan', 'Trott', 1981, 114, 'CapeTown', 'SouthAfrica'),
INSERT INTO Player_info VALUES(3, 'Lasith', 'Malinga', 1977, 303, 'Matale', 'Srilanka'),
INSERT INTO Player_info VALUES(4, 'Virat', 'Kohli', 1988, 310, 'Delhi', 'India'),
INSERT INTO Player_info VALUES(5, 'Karn', 'Sharma’, 1987, 126, 'Haryana', 'India'),
INSERT INTO Player_info VALUES(6, 'Ravindra', 'Jadeja', 1988, 341, 'Nagpur', 'India'),
INSERT INTO Player_info VALUES(7, 'Stuart', 'Broad’, 1982, 186, 'Manchester', 'England');

Now, we run the following command to see the data of the table:

SELECT * FROM Player_info;
IDFirst_NameLast_NameYear_Of_BirthDayOfYear_Of_BirthPlace_Of_BirthCountry
1SanjuSamson1981340KeralaIndia
2JonathanTrott1981114CapetownSouth Africa
3LasithMalinga1977303MataleSrilanka
4ViratKohli1988310DelhiIndia
5KarnSharma1987126HaryanaIndia
6RavindraJadeja1988341NagpurIndia
7StuartBoard1982186ManchesterEngland

Now, we update the year_Of_Birth, Place_Of_Birth, of the player whose ID is 5.

UPDATE Player_info SET Year_Of_Birth = 1990, Place_Of_Birth= ”Uttar Pradesh”  WHERE ID = 5;

Now, run the following query to see the modifications in the table:

SELECT * FROM Player_info;

Output:

IDFirst_NameLast_NameYear_Of_BirthDayOfYear_Of_BirthPlace_Of_BirthCountry
1SanjuSamson1981340KeralaIndia
2JonathanTrott1981114CapetownSouth Africa
3LasithMalinga1977303MataleSrilanka
4ViratKohli1988310DelhiIndia
5KarnSharma1990126Uttar PradeshIndia
6RavindraJadeja1988341NagpurIndia
7StuartBoard1982186ManchesterEngland

Example 3: In this example, we create a table called Saletime which consists of customer time, product time, and the time at which the sale happened.

CREATE TABLE Saletime(
	ProductName VARCHAR(205),
	CustomerName VARCHAR(205),
	DispatchTimeStamp VARCHAR(205),
	Price INT,
	Location VARCHAR(205)
);
INSERT INTO Saletime VALUES
('Mouse', 'Basheer', TIMESTAMP('2020-04-04', '12:02:25.247552'), 1000, 'Nizamabad'),
 ('Laptop’, 'Manoj', TIMESTAMP('2019-08-06', '04:33:22.721521'), 60000, 'Hyderabad'),
 ('Wireless_keyboard', 'Arjun', TIMESTAMP('2016-02-27', '04:20:27.263420'), 2000, 'Vijayawada'),
 ('Mobile', 'Vanitha', TIMESTAMP ('2018-05-22', '10:20:25.265563'), 10000, 'Cochin’),
('Headset', 'Raju', TIMESTAMP('2022-02-20', '11:39:37.522321'), 5000, 'Delhi'),
('Earphones', 'Santhosh', TIMESTAMP('2021-11-29', '09:19:23.432356'), 2000, 'Lucknow'),
 ('Harddisk', 'Priyanka', TIMESTAMP('2019-12-30', '12:54:25.122386'), 7000, 'Bangalore'),

Now, we run the following command to see the data of the table:

SELECT * FROM Saletime;

Output:

ProductNameCustomerNameDispatchTimeStampPriceLocation
MouseBasheer2020-04-04 12:02:25.2475521000Nizamabad
LaptopManoj2019-08-06 04:33:22.72152160000Hyderabad
Wireless_keyboardArjun2016-02-27 04:20:27.2634202000Vijayawada
MobileVanitha2018-05-22 10:20:25.26556310000Cochin
HeadsetRaju2022-02-20 11:39:37.5223215000Delhi
EarphonesSanthosh2021-11-29 09:19:23.4323562000Lucknow
HarddiskPriyanka2019-12-30 12:54:25.1223867000Bangalore

Now, we will update the values of the Price and Location columns whose customer’s name is Vanitha.

UPDATE Saletime SET Price = 18000, Location= “Chennai” WHERE CustomerName = Vanitha;

Now, run the following query to see the modifications in the table:

SELECT * FROM Saletime;

Output:

ProductNameCustomerNameDispatchTimeStampPriceLocation
MouseBasheer2020-04-04 12:02:25.2475521000Nizamabad
LaptopManoj2019-08-06 04:33:22.72152160000Hyderabad
Wireless_keyboardArjun2016-02-27 04:20:27.2634202000Vijayawada
MobileVanitha2018-05-22 10:20:25.26556318000Chennai
HeadsetRaju2022-02-20 11:39:37.5223215000Delhi
EarphonesSanthosh2021-11-29 09:19:23.4323562000Lucknow
HarddiskPriyanka2019-12-30 12:54:25.1223867000Bangalore

Related Topics

Codd’s Rules in SQL

Codd’s Rules Dr. Edgar F. Codd, in 1985, laid down 13 fundamental rules after doing large-scale research on the Relational Model of databases. According to him, every database must follow these...

3 minutes read.

How to use LIKE in SQL

In this SQL article, we will learn and understand how to use LIKE to the columns in the SQL tables. What is Like? Like is an operator in the SQL. It is...

7 minutes read.

SQL Alter Table

In Structured Query Language, if you want to add columns in an existing table, then modify the table, or delete columns from the table. All these operations are allowed only...

7 minutes read.

SQL CONSTRAINTS

SQL Constraints specifies the rules/limitations/restrictions for data present in table. SQL Constraints are specified at the time of table creation or after table creation using ALTER command. There are two...

5 minutes read.

SQL Auto Increment

As we all know, In SQL for unique identification, we assign a column with the primary key. But in some tables, we find difficult to differentiate a column as a...

5 minutes read.

How to use the BETWEEN operator in SQL

In this entire SQL article, we will understand and learn about the BETWEEN operator concept and how to use it in SQL. What is the BETWEEN operator in SQL? The Between operator...

4 minutes read.

SQL Arithmetic Operators

This page contains all the information, learn about SQL Arithmetic Operators concept in the SQL table with the help of examples. The Arithmetic Operators is used to perform mathematical calculations on...

5 minutes read.

Introduction to SQL

SQL Introduction SQL is Standard Query Language. This language is used to communicate or interact with database. In other words, SQL is used to access and manage data or information...

2 minutes read.

How to Add Comments in SQL?

How to Add Comments in SQL Comments are text notes that are incorporated into the program to make the code easier to understand. In SQL, commenting is used to explain various sections...

2 minutes read.

SQL Insert multiple rows

This article will deal with a new insertion method in SQL which inserts multiple rows at a time. Multiple records can be inserted at a time into a specific table with...

4 minutes read.

How to drop a column in SQL?

How to drop a column in SQL Introduction To delete a column from an already created table, one needs to use the ALTER command along with the DROP COLUMN clause. Syntax: ALTER TABLE tablename...

4 minutes read.

SQL Data Control Language

Data Control Language decides to whom should (which user) permit access privileges. GRANT and REVOKE are the commands of DCL. GRANT: It gives privileges to user. REVOKE: It takes back privileges from granted...

1 minute read.

Update Query in SQL

Update is an SQL command that is used to modify the data that in already present in database. Update is a command of DML. DML means Data Manipulation Language. Basically,...

3 minutes read.

What is SQL Injection?

Introduction to SQL Injection SQL injection is a vulnerability or a technique that might destroy the database of a website or a web application. It is one of the most widely...

4 minutes read.

How to use COUNT in SQL?

How to use COUNT in SQL Introduction COUNT( ) is an aggregate function in SQL.This function counts the number of records in a table if the condition is not specified.If the condition...

4 minutes read.

SQL Between Operator

SQL Between operator is a logical operator in the Structured Query Language. The Between operator is used to retrieve data within the range specified in the condition in the query. The...

7 minutes read.

How to Create Temporary Table in SQL?

How to Create Temporary Table in SQL  Introduction to Temporary Tables Temporary table is a table which is used to store temporary data that can be used further in the same client...

3 minutes read.

SQL SELECT BETWEEN Operator

This SQL tutorial explains and helps us understand how to use the BETWEEN operator in the SELECT query with examples. The SQL SELECT BETWEEN operator retrieves the data from the table...

2 minutes read.

SQL SELECT IN

SQL SELECT IN is a logical operator in Structured Query Language. It is used in SQL queries to reduce the use of multiple 'OR' operators. s The IN operator in SQL...

6 minutes read.

SQL Data Manipulation Language

Data Manipulation Language manipulates/make changes in data present in a table. It only affects data/records of table, not on the schema/structure of table. INSERT, UPDATE, DELETE are the commands of DML. INSERT: Stores...

2 minutes read.