{"id":1397,"date":"2023-03-24T12:31:09","date_gmt":"2023-03-24T09:01:09","guid":{"rendered":"https:\/\/mshaeri.com\/blog\/?p=1397"},"modified":"2025-02-09T12:10:04","modified_gmt":"2025-02-09T08:40:04","slug":"combining-multiple-update-queries-into-single-query-for-multiple-columns-update-using-case","status":"publish","type":"post","link":"https:\/\/mshaeri.com\/blog\/combining-multiple-update-queries-into-single-query-for-multiple-columns-update-using-case\/","title":{"rendered":"Complex Update In A Single SQL Query Using CASE Statements"},"content":{"rendered":"\n<p>SQL update statement is an important area in optimizing database queries. Optimizing update queries in large tables can reduce query time and improve efficiency. In this post, I will describe how to combine multiple update queries into a single query using the CASE condition to improve the execution time and reduce workload of the database.<\/p>\n\n\n\n<p>Assume we have an <strong>&#8220;order&#8221;<\/strong> table with fields <strong>id, item, purchase_type, delivery_type, amount <\/strong>and <strong>total_price<\/strong> :<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><a href=\"https:\/\/mshaeri.com\/blog\/wp-content\/uploads\/2023\/03\/image-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"408\" src=\"https:\/\/mshaeri.com\/blog\/wp-content\/uploads\/2023\/03\/image-2-1024x408.png\" alt=\"Complex Update In A Single SQL Query Using CASE Statements\" class=\"wp-image-1407\" style=\"width:512px;height:204px\" srcset=\"https:\/\/mshaeri.com\/blog\/wp-content\/uploads\/2023\/03\/image-2-1024x408.png 1024w, https:\/\/mshaeri.com\/blog\/wp-content\/uploads\/2023\/03\/image-2-300x119.png 300w, https:\/\/mshaeri.com\/blog\/wp-content\/uploads\/2023\/03\/image-2-768x306.png 768w, https:\/\/mshaeri.com\/blog\/wp-content\/uploads\/2023\/03\/image-2-1536x612.png 1536w, https:\/\/mshaeri.com\/blog\/wp-content\/uploads\/2023\/03\/image-2.png 1760w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Table containing orders data<\/figcaption><\/figure><\/div>\n\n\n<p>And we need to accomplish following modification on the table&#8217;s records :<\/p>\n\n\n\n<p>1- Increasing <strong>total_price <\/strong>of orders which their <strong>purchase_type <\/strong>equals to &#8220;online&#8221; by 10%.<br>2- Decreasing <strong>total_price <\/strong>of orders which their <strong>purchase_type <\/strong>equals to &#8220;offline&#8221; by 20%.<br>3- Multiplying the value of <strong>amount <\/strong>field by 2 in records which have <strong>delivery_type <\/strong>equals to &#8220;express&#8221;.<\/p>\n\n\n\n<p>Accomplishing these 3 tasks with separate queries can be the simplest but most inefficient choice :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">UPDATE orders SET total_price = total_price * 1.1 WHERE purchase_type = 'online';\nUPDATE orders SET total_price = total_price * 0.8 WHERE purchase_type = 'offline';\nUPDATE orders SET amount = amount * 2 WHERE delivery_type = 'express';<\/code><\/pre>\n\n\n\n<p>The inefficiency of separate queries comes from the overhead of executing multiple queries, which can impact the overall performance of the database. Each query involves a certain amount of overhead, including parsing, planning, and executing the query, as well as any network latency or disk I\/O that may be involved.  In contrast, using a single query to perform multiple updates can reduce this overhead by minimizing the number of queries that need to be executed. Moreover, for each single query the DB engine has to traverse through all the records which impose high workload to the database.<\/p>\n\n\n\n<p>To accomplish all required modification within a  single query, we can use CASE statement, as following :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">UPDATE orders\n SET total_price =\n     CASE \n         WHEN purchase_type = 'online' THEN total_price * 1.1\n         WHEN purchase_type = 'offline' THEN total_price * 0.8\n         ELSE total_price\n     END,\n     amount =\n     CASE \n         WHEN delivery_type = 'express' THEN amount * 2\n         ELSE amount\n     END\n WHERE purchase_type IN ('online', 'offline') OR delivery_type = 'express';<\/code><\/pre>\n\n\n\n<p>In this query, we use the CASE statement to perform different actions based on the value of the <strong>purchase_type <\/strong>and <strong>delivery_type <\/strong>fields in a single traverse. Using CASE condition we easily increased the <strong>total_price<\/strong> of orders with &#8220;offline&#8221; <strong>purchase_type <\/strong>by 10% by multiplying it by 1.1 and for records with &#8220;offline&#8221; <strong>purchase_type<\/strong>, we decreased the total_price by 20% by multiplying it by 0.9, and by using ELSE we don&#8217;t make any changes to the total_price field for records where the purchase_type is neither &#8220;online&#8221; nor &#8220;offline&#8221;. In the mean while, for records where the <strong>delivery_type<\/strong> is &#8220;express&#8221;, we multiply the <strong>amount <\/strong>field by 2. <\/p>\n\n\n\n<p>It&#8217;s important to use a WHERE clause in the UPDATE query not only to ensure that we only modify the records that we intend to and avoid accidentally modifying any other records in the table, but also to eliminate any records that do not need to be updated, which can help minimizing the amount of data that needs to be processed by the query. By minimizing the amount of data that needs to be processed, we can reduce the workload of the database and minimize the amount of time and resources required to complete the query. This can be particularly important for large datasets or complex queries, where the overhead of processing unnecessary data can significantly impact the performance of the database. However, it&#8217;s important to note that the efficiency of the WHERE clause also depends on the indexing and optimization strategies used by the database management system.<\/p>\n\n\n\n<p>I hope you found this post informative and helpful. If you have any thoughts or questions, feel free to leave a comment below\u270c.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>SQL update statement is an important area in optimizing database queries. Optimizing update queries in large tables can reduce query time and improve efficiency. In &hellip; <\/p>\n","protected":false},"author":1,"featured_media":1411,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,215,123,216,35,214],"tags":[212,213,280,40,131,114,211,45,210],"_links":{"self":[{"href":"https:\/\/mshaeri.com\/blog\/wp-json\/wp\/v2\/posts\/1397"}],"collection":[{"href":"https:\/\/mshaeri.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mshaeri.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mshaeri.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mshaeri.com\/blog\/wp-json\/wp\/v2\/comments?post=1397"}],"version-history":[{"count":2,"href":"https:\/\/mshaeri.com\/blog\/wp-json\/wp\/v2\/posts\/1397\/revisions"}],"predecessor-version":[{"id":1769,"href":"https:\/\/mshaeri.com\/blog\/wp-json\/wp\/v2\/posts\/1397\/revisions\/1769"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/mshaeri.com\/blog\/wp-json\/wp\/v2\/media\/1411"}],"wp:attachment":[{"href":"https:\/\/mshaeri.com\/blog\/wp-json\/wp\/v2\/media?parent=1397"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mshaeri.com\/blog\/wp-json\/wp\/v2\/categories?post=1397"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mshaeri.com\/blog\/wp-json\/wp\/v2\/tags?post=1397"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}