How to Update Table using Subqueries in SQL

In this article I am going to explain how to update table using subqueries in SQL.
  • 2056

Introduction

In this article I am going to explain how to update table using subqueries in SQL. A select statement known as subquery if it is codded within another SQL statement. Subquery can return a single value or a result set. This result set may contain a single column or more than one column.

There are four way to create subquery  in a SELECT statement.

  • In WHERE Clause
  • In HAVING Clause
  • In FROM Clause
  • In SELECT Clause

We can update data in a table using subqueries in SQL. To update a table we can write subquery in the SET, FROM and WHERE Clause.

Data Stored in table before update operation

Clipboard01.jpg

Statement that update data stored in table using subquery.

UPDATE dbo.mcninvoices

SET creadittotal = creadittotal+100,

paymenttotal = (SELECT MAX(paymenttotal) FROM dbo.mcninvoices)

WHERE dbo.mcninvoices.invoiceid=1

Data stored in table after update operation

Clipboard02.jpg


© 2020 DotNetHeaven. All rights reserved.