w3resource

SQL update columns with sum() and group by

In this page, we are going to discuss, how to change the data of the columns with the SQL UPDATE statement using aggregate function SUM() and GROUP BY clause.

Example:

Sample table: customer1


Sample table: orders


To change the value of 'outstanding_amt' of 'customer1' table with following conditions -

1. modified value for 'outstanding_amt' is 0,

2. sum of 'ord_amount' from 'orders' table must be greater than 5000 which satisfies the condition bellow:

3. unique 'cust_code' of 'customer1' makes a group,

4. and 'cust_code' of 'customer1' and 'orders' must be same,

the following SQL statement can be used:

SQL Code:


-- This SQL code updates the 'outstanding_amt' column in the 'customer1' table to 0 for customers whose total order amount exceeds 5000.
-- UPDATE statement begins
UPDATE customer1
-- Specifies the target table 'customer1' where the data will be updated
SET outstanding_amt=0
-- Sets the value of the 'outstanding_amt' column to 0 for rows that meet the specified condition
WHERE (SELECT SUM(ord_amount) FROM orders
-- Subquery calculates the total order amount for each customer
WHERE customer1.cust_code=orders.cust_code
-- Matches customers in 'customer1' with their orders in 'orders' based on 'cust_code'
GROUP BY cust_code )>5000;
-- Specifies the condition for updating rows: only rows where the total order amount for a customer exceeds 5000 will be affected

Explanation:

  • This SQL code uses the UPDATE statement to modify existing records in the 'customer1' table.

  • The UPDATE statement specifies the target table 'customer1' where the update operation will be performed.

  • The SET clause assigns a new value of 0 to the 'outstanding_amt' column for rows that meet the specified condition.

  • The WHERE clause includes a subquery that calculates the total order amount for each customer in the 'customer1' table by joining it with the 'orders' table using the 'cust_code' column.

  • The subquery result is compared to 5000 in the outer WHERE clause to filter rows, ensuring that only customers with a total order amount exceeding 5000 will have their 'outstanding_amt' set to 0.

SQL update columns with NULL

In the following we are going to discuss, how the NULL works with the UPDATE statement.

Example:

Sample table: agent1


To update the 'agent1' table with following conditions -

1. modified value for 'phone_no' is NULL,

2. 'commission' must be more than .15,

the following SQL statement can be used :

SQL Code:


-- This SQL code updates the 'phone_no' column in the 'agent1' table to NULL for agents whose commission is greater than or equal to 0.15.
-- UPDATE statement begins
UPDATE agent1
-- Specifies the target table 'agent1' where the data will be updated
SET phone_no=NULL
-- Sets the value of the 'phone_no' column to NULL for rows that meet the specified condition
WHERE commission>=.15;
-- Specifies the condition for updating rows: only rows where the 'commission' column is greater than or equal to 0.15 will be affected

Explanation:

  • This SQL code uses the UPDATE statement to modify existing records in the 'agent1' table.

  • The UPDATE statement specifies the target table 'agent1' where the update operation will be performed.

  • The SET clause assigns a new value of NULL to the 'phone_no' column for rows that meet the specified condition.

  • The WHERE clause filters the rows to be updated, ensuring that only rows where the 'commission' column is greater than or equal to 0.15 will be affected by the update operation.

See our Model Database

Here is a new document which is a collection of questions with short and simple answers, useful for learning SQL as well as for interviews.

Check out our 1000+ SQL Exercises with solution and explanation to improve your skills.

Previous: Update columns using arithmetical expression
Next: UPDATE using subqueries



Follow us on Facebook and Twitter for latest update.