SQL UPDATE: Modify Existing Data in a Table By Examples (2024)

Summary: in this tutorial, you will learn how to use the SQL UPDATE statement to modify data of the existing rows a table.

Introduction to the SQL UPDATE statement

To change existing data in a table, you use the UPDATE statement. The following shows the syntax of the UPDATE statement:

UPDATE table_nameSET column1 = value1, column2 = value2WHEREcondition;Code language: SQL (Structured Query Language) (sql)

In this syntax:

  • First, indicate the table that you want to update in the UPDATE clause.
  • Second, specify the columns that you want to modify in the SET clause. The columns that are not listed in the SET clause will retain their original values.
  • Third, specify which rows to update in the WHERE clause.

The UPDATE statement affects one or more rows in a table based on the condition in the WHERE clause. For example, if the WHERE clause contains a primary key expression, the UPDATE statement changes one row only.

However, any row that causes the condition in theWHEREto evaluate to true will be modified. Because the WHERE clause is optional, therefore, if you omit it, the all the rows in the table will be affected.

SQL UPDATE statement examples

We will use the employees and dependents table to demonstrate the UPDATE statement.

SQL UPDATE: Modify Existing Data in a Table By Examples (1)

SQL UPDATE one row example

Suppose the employee id 192 Sarah Bell changed her last name from Bell to Lopez and you need to update her record in the employees table.

SQL UPDATE: Modify Existing Data in a Table By Examples (2)

To update Sarah’s last name from Bell to Lopez, you use the following UPDATE statement:

UPDATE employees SET last_name = 'Lopez'WHERE employee_id = 192;Code language: SQL (Structured Query Language) (sql)

Try It

The database system updated value in the last_name column and the row with employee_id192.

You can verify it by using the following SELECT statement.

SELECTemployee_id,first_name,last_nameFROMemployeesWHEREemployee_id = 192;Code language: SQL (Structured Query Language) (sql)

Try It

SQL UPDATE: Modify Existing Data in a Table By Examples (3)

SQL UPDATE multiple rows example

Now, Nancy wants to change all her children’s last names from Bell to Lopez. In this case, you need to update all Nancy’s dependents in the dependents table.

Before updating the data, let’s check the dependents of Nancy.

SELECT*FROMdependentsWHEREemployee_id = 192;Code language: SQL (Structured Query Language) (sql)

Try It

SQL UPDATE: Modify Existing Data in a Table By Examples (4)

To update the last names of Nancy’s dependents, you use the following UPDATE statement.

UPDATE dependents SET last_name = 'Lopez'WHERE employee_id = 192;Code language: SQL (Structured Query Language) (sql)

Try It

SQL UPDATE: Modify Existing Data in a Table By Examples (5)

SQL UPDATE with subquery example

Sometimes when employees change their last names, you update the employees table only without updating the dependents table.

To make sure that the last names of children are always matched with the last name of parents in the employees table, you use the following statement:

UPDATE dependentsSET last_name = (SELECTlast_nameFROMemployeesWHEREemployee_id = dependents.employee_id);Code language: SQL (Structured Query Language) (sql)

Try It

Because the WHERE clause is omitted, the UPDATE statement updated all rows in the dependents table.

In the SET clause, instead of using the literal values, we used a subquery to get the corresponding last name value from the employees table.

In this tutorial, we have shown you how to use the SQL UPDATE statement to modify existing data in a table.

Was this tutorial helpful ?

SQL UPDATE: Modify Existing Data in a Table By Examples (2024)
Top Articles
5 Warren Buffett Stocks You Can Buy and Hold for the Next Decade | The Motley Fool
Stock market trading hours & holidays
Washu Parking
No Limit Telegram Channel
Myexperience Login Northwell
OSRS Fishing Training Guide: Quick Methods To Reach Level 99 - Rune Fanatics
Boggle Brain Busters Bonus Answers
Here's how eating according to your blood type could help you keep healthy
Lesson 1 Homework 5.5 Answer Key
Bubbles Hair Salon Woodbridge Va
shopping.drugsourceinc.com/imperial | Imperial Health TX AZ
Tight Tiny Teen Scouts 5
Dusk
4302024447
Mephisto Summoners War
Classic Lotto Payout Calculator
Samsung Galaxy S24 Ultra Negru dual-sim, 256 GB, 12 GB RAM - Telefon mobil la pret avantajos - Abonament - In rate | Digi Romania S.A.
Mbta Commuter Rail Lowell Line Schedule
Skyward Login Jennings County
Classic | Cyclone RakeAmerica's #1 Lawn and Leaf Vacuum
Grayling Purnell Net Worth
Wausau Obits Legacy
Carson Municipal Code
The Pretty Kitty Tanglewood
Team C Lakewood
How to Grow and Care for Four O'Clock Plants
Talk To Me Showtimes Near Marcus Valley Grand Cinema
Thick Ebony Trans
Koninklijk Theater Tuschinski
Powerschool Mcvsd
Watson 853 White Oval
10 Best Places to Go and Things to Know for a Trip to the Hickory M...
CVS Health’s MinuteClinic Introduces New Virtual Care Offering
By.association.only - Watsonville - Book Online - Prices, Reviews, Photos
Uncovering the Enigmatic Trish Stratus: From Net Worth to Personal Life
Albertville Memorial Funeral Home Obituaries
Babydepot Registry
Craigs List Tallahassee
Beaver Saddle Ark
Song That Goes Yeah Yeah Yeah Yeah Sounds Like Mgmt
Vip Lounge Odu
Can You Buy Pedialyte On Food Stamps
Metro Pcs Forest City Iowa
2023 Nickstory
Lake Kingdom Moon 31
Setx Sports
2132815089
Below Five Store Near Me
Hk Jockey Club Result
Joblink Maine
De boeken van Val McDermid op volgorde
Jeep Forum Cj
Latest Posts
Article information

Author: Frankie Dare

Last Updated:

Views: 6009

Rating: 4.2 / 5 (53 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Frankie Dare

Birthday: 2000-01-27

Address: Suite 313 45115 Caridad Freeway, Port Barabaraville, MS 66713

Phone: +3769542039359

Job: Sales Manager

Hobby: Baton twirling, Stand-up comedy, Leather crafting, Rugby, tabletop games, Jigsaw puzzles, Air sports

Introduction: My name is Frankie Dare, I am a funny, beautiful, proud, fair, pleasant, cheerful, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.