{"id":8417,"date":"2021-01-25T04:54:00","date_gmt":"2021-01-25T04:54:00","guid":{"rendered":"https:\/\/ubiq.co\/tech-blog\/?p=8417"},"modified":"2025-09-01T05:09:16","modified_gmt":"2025-09-01T05:09:16","slug":"how-to-write-case-statement-in-mysql","status":"publish","type":"post","link":"https:\/\/ubiq.co\/tech-blog\/how-to-write-case-statement-in-mysql\/","title":{"rendered":"How to Use CASE Statement in MySQL"},"content":{"rendered":"\n<p>Sometimes you may need to use conditional values in your SQL queries. You may need these values to be generated based on one or more conditions. In such cases, you can use CASE or IF-ELSE statements. MySQL Case statement allows you to check a value for multiple conditions in an SQL query. In this article we will look at how to use case statement in MySQL. We will also briefly compare it with IF-ELSE statement.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-write-case-statement-in-mysql\/#What_is_CASE_Statement\" >What is CASE Statement<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-write-case-statement-in-mysql\/#CASE_vs_IF_ELSE\" >CASE vs IF ELSE<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-write-case-statement-in-mysql\/#MySQL_Case_examples\" >MySQL Case examples<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-write-case-statement-in-mysql\/#Example_1\" >Example 1<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-write-case-statement-in-mysql\/#Example_2\" >Example 2<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-write-case-statement-in-mysql\/#Example_3\" >Example 3<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-write-case-statement-in-mysql\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-write-case-statement-in-mysql\/#Using_Case_in_Ubiq\" >Using Case in Ubiq<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_is_CASE_Statement\"><\/span>What is CASE Statement<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>MySQL CASE statement evaluates a set of conditions and returns an output when the first condition is met. If no condition is met, then it returns the value specified in else statement. If no else statement is mentioned then it returns null. Here is the syntax for MySQL Case statement.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">select <br>case <br>    when condition1 then value1<br>    when condition2 then value2<br>    ...<br>    else result<br>end,<br>column2, column3, ...<br>from table_name<\/pre>\n\n\n\n<p>In the above query, you need to specify the table name. Also you need to mention each condition that you want to check a given column for, within case&#8230; end statement. You also need to mention the value to be assigned to the column if each condition is true, that is, for each case.<\/p>\n\n\n\n<p>Here are some of the key benefits of CASE statement:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>They can be used in SELECT, WHERE and ORDER BY clauses of your SQL queries.<\/li>\n\n\n\n<li>They allow you to implement complex logical conditions reducing dependency on outside programming.<\/li>\n\n\n\n<li>They can accommodate a large number of conditions and results in a compact manner. <\/li>\n\n\n\n<li>You don&#8217;t need to do nesting, like in case of IF-ELSE statements.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"CASE_vs_IF_ELSE\"><\/span>CASE vs IF ELSE<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Both CASE and IF ELSE statements evaluate a sequence of conditions and stop when there is first match. We have already mentioned the syntax of CASE statement above. Here is the syntax of IF statement.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">IF(condition, value_if_true, value_if_false)<\/pre>\n\n\n\n<p>The main advantage of using CASE is that it is more flexible than IF statement. It can handle more output branches whereas IF statement supports only 2 branches, one when the condition is true and the other when it is false. Here is an example where single CASE statement has 3 output branches.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">select case <br>       when username = 'jim' then 'high' <br>       when username = 'john' then 'medium' <br>       else 'low' <br>       end as height<\/pre>\n\n\n\n<p>CASE statement allows you to build a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Switch_statement\" target=\"_blank\" rel=\"noreferrer noopener\">switch<\/a> like mechanism where you have a large number of different conditions and return a different output for different condition. Here is an example to illustrate it.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">select case username <br>       when 'john' then 'high' <br>       when 'jim' then 'medium' <br>       else 'low' <br>       end as height<\/pre>\n\n\n\n<p>Implementing both the above statements will require you to do nested IF-ELSE statements.<\/p>\n\n\n\n<p>Also it is more intuitive than IF. Look at the two equivalent statements using IF and CASE statement.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">select if(username = 'john', 'high', 'low') as grades<br>select case when username = 'john' then 'high' else 'low' end as grades<\/pre>\n\n\n\n<ol class=\"wp-block-list\">\n<li><\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"MySQL_Case_examples\"><\/span>MySQL Case examples<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Let us look at some examples of MySQL Case function below. Let us say you have the following table <em>sales(id, order_date, amount)<\/em>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt; create table sales(id int, order_date date, amount int);\n\nmysql&gt; insert into sales(id, order_date, amount)\n     values(1, '2021-01-01', 150),\n     (1, '2021-01-02', 250),\n     (1, '2021-01-03', 100),\n     (1, '2021-01-04', 150),\n     (1, '2021-01-05', 350);\n\nmysql&gt; select * from sales;\n+------+------------+--------+\n| id   | order_date | amount |\n+------+------------+--------+\n|    1 | 2021-01-01 |    150 |\n|    1 | 2021-01-02 |    250 |\n|    1 | 2021-01-03 |    100 |\n|    1 | 2021-01-04 |    150 |\n|    1 | 2021-01-05 |    350 |\n+------+------------+--------+<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Example_1\"><\/span>Example 1<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Here&#8217;s the SQL query to group the <em>amount<\/em> values into 3 buckets &#8211; less than equal to 100, 100-300, and more than 300. In this query, CASE will evaluate the amount column for each row, for each of the &#8216;when&#8217; conditions, one after the other, and assign the text after &#8216;then&#8217; part. For each row, it will stop evaluating after the first match.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt; select id, order_date,<br>     case when amount&lt;=100 then 'less than equal to 100'<br>          when amount&gt;100 and amount&lt;300 then '101 to 300'<br>          when amount&gt;=300 then 'greater than 300'<br>     end as bucket<br>     from sales;<br>+------+------------+------------------------+<br>| id   | order_date | bucket                 |<br>+------+------------+------------------------+<br>|    1 | 2021-01-01 | 101 to 300             |<br>|    1 | 2021-01-02 | 101 to 300             |<br>|    1 | 2021-01-03 | less than equal to 100 |<br>|    1 | 2021-01-04 | 101 to 300             |<br>|    1 | 2021-01-05 | greater than 300       |<br>+------+------------+------------------------+<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Example_2\"><\/span>Example 2<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Please note, if none of the case statements are satisfied for a value, then the CASE statement will return NULL. Here there is no case matching amount=100 so it returns NULL.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt; select id, order_date,\n     case when amount&lt;100 then 'less than 100'\n          when amount&gt;100 and amount&lt;300 then '100 to 300'\n     when amount&gt;300 then 'greater than 300'\n     end as bucket\n     from sales;\n+------+------------+------------------+\n| id   | order_date | bucket           |\n+------+------------+------------------+\n|    1 | 2021-01-01 | 100 to 300       |\n|    1 | 2021-01-02 | 100 to 300       |\n|    1 | 2021-01-03 | <strong>NULL <\/strong>            |\n|    1 | 2021-01-04 | 100 to 300       |\n|    1 | 2021-01-05 | greater than 300 |\n+------+------------+------------------+\n<\/pre>\n\n\n\n<p>If you do not want NULL in your output, then include an ELSE statement after all WHEN statements. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt; select id, order_date,<br>     case when amount&lt;100 then 'less than 100'<br>          when amount&gt;100 and amount&lt;300 then '100 to 300'<br>          when amount&gt;300 then 'greater than 300'<br><strong>          else 'not available'<\/strong><br>     end as bucket<br>     from sales;<br>+------+------------+------------------+<br>| id   | order_date | bucket           |<br>+------+------------+------------------+<br>|    1 | 2021-01-01 | 100 to 300       |<br>|    1 | 2021-01-02 | 100 to 300       |<br>|    1 | 2021-01-03 | <strong>Not available<\/strong>    |<br>|    1 | 2021-01-04 | 100 to 300       |<br>|    1 | 2021-01-05 | greater than 300 |<br>+------+------------+------------------+<\/pre>\n\n\n\n<p>In the above example, the CASE statement returns NULL for 100, since it does not satisfy any of the conditions.<\/p>\n\n\n\n<p>MySQL Case is useful for creating frequency distributions and grouping values.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Example_3\"><\/span>Example 3<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>You can also use WHERE condition to apply case statement on a subset of rows. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">select id, order_date,\n     case when amount&lt;=100 then 'less than equal to 100'\n          when amount&gt;100 and amount&lt;300 then '101 to 300'\n          when amount&gt;=300 then 'greater than 300'\n     end as bucket\n     from sales\n     <strong>WHERE &lt;condition&gt;<\/strong>;<\/pre>\n\n\n\n<p>Here the WHERE clause will first be applied to filter required rows and only then CASE will be evaluated. This is useful if you have a large table and do not want to apply CASE on all rows. It saves a lot of time.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In this article, we have learnt what is CASE in MySQL, how to use it, differences between CASE and IF-ELSE. CASE is a versatile feature that allows you to easily generate conditional values in MySQL and other databases. It is very flexible since you can do obtain multi-pronged result for your column. It is more compact and intuitive than using IF statement, which requires nesting for complex use cases. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Using_Case_in_Ubiq\"><\/span>Using Case in Ubiq<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><a href=\"https:\/\/ubiq.co\/\">Ubiq<\/a>&nbsp;Reporting tool supports all the above SQL queries and makes it easy to visualize SQL results in different ways. Here is the CASE SQL query mentioned above, in Ubiq.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"730\" height=\"487\" src=\"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2021\/01\/mysql-case-ubiq.webp?resize=730%2C487&#038;ssl=1\" alt=\"\" class=\"wp-image-8420\"\/><\/figure>\n<\/div>\n\n\n<p>Need a reporting tool for MySQL?&nbsp;<a href=\"http:\/\/ubiq.co\/\">Ubiq<\/a>&nbsp;makes it easy to visualize data in minutes, and monitor in real-time dashboards.&nbsp;<a href=\"http:\/\/ubiq.co\/accounts\/register\">Try it<\/a>&nbsp;Today!<\/p>\n\n\n\n<p>Also read:<br><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-use-coalesce-in-mysql\/\">How to Use Coalesce In MySQL<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-query-json-column-in-mysql\/\">How to Query JSON Column in MySQL<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-avoid-inserting-duplicate-records-in-mysql\/\">How to Avoid Inserting Duplicate Records in MySQL<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Case statements help you use multiple if conditions in SQL queries. Here is how to write case statement in MySQL.<\/p>\n","protected":false},"author":1,"featured_media":8419,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[324],"tags":[447],"class_list":["post-8417","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mysql","tag-case-statement"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Use CASE Statement in MySQL - Ubiq BI<\/title>\n<meta name=\"description\" content=\"MySQL CASE statement helps you use multiple if conditions in SQL queries. Here is how to write CASE statement in MySQL.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-write-case-statement-in-mysql\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Use CASE Statement in MySQL - Ubiq BI\" \/>\n<meta property=\"og:description\" content=\"MySQL CASE statement helps you use multiple if conditions in SQL queries. Here is how to write CASE statement in MySQL.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/ubiq.co\/tech-blog\/how-to-write-case-statement-in-mysql\/\" \/>\n<meta property=\"og:site_name\" content=\"Ubiq BI\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ubiqbi\" \/>\n<meta property=\"article:published_time\" content=\"2021-01-25T04:54:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-09-01T05:09:16+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2021\/01\/mysql-case-statement.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"730\" \/>\n\t<meta property=\"og:image:height\" content=\"410\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"Sreeram Sreenivasan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@UbiqBI\" \/>\n<meta name=\"twitter:site\" content=\"@UbiqBI\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Sreeram Sreenivasan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-write-case-statement-in-mysql\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-write-case-statement-in-mysql\\\/\"},\"author\":{\"name\":\"Sreeram Sreenivasan\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\"},\"headline\":\"How to Use CASE Statement in MySQL\",\"datePublished\":\"2021-01-25T04:54:00+00:00\",\"dateModified\":\"2025-09-01T05:09:16+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-write-case-statement-in-mysql\\\/\"},\"wordCount\":792,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-write-case-statement-in-mysql\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2021\\\/01\\\/mysql-case-statement.webp?fit=730%2C410&ssl=1\",\"keywords\":[\"case statement\"],\"articleSection\":[\"MySQL\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-write-case-statement-in-mysql\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-write-case-statement-in-mysql\\\/\",\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-write-case-statement-in-mysql\\\/\",\"name\":\"How to Use CASE Statement in MySQL - Ubiq BI\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-write-case-statement-in-mysql\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-write-case-statement-in-mysql\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2021\\\/01\\\/mysql-case-statement.webp?fit=730%2C410&ssl=1\",\"datePublished\":\"2021-01-25T04:54:00+00:00\",\"dateModified\":\"2025-09-01T05:09:16+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\"},\"description\":\"MySQL CASE statement helps you use multiple if conditions in SQL queries. Here is how to write CASE statement in MySQL.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-write-case-statement-in-mysql\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-write-case-statement-in-mysql\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-write-case-statement-in-mysql\\\/#primaryimage\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2021\\\/01\\\/mysql-case-statement.webp?fit=730%2C410&ssl=1\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2021\\\/01\\\/mysql-case-statement.webp?fit=730%2C410&ssl=1\",\"width\":730,\"height\":410,\"caption\":\"mysql case statement\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-write-case-statement-in-mysql\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Use CASE Statement in MySQL\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#website\",\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/\",\"name\":\"Ubiq BI\",\"description\":\"Build dashboards &amp; reports in minutes\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\",\"name\":\"Sreeram Sreenivasan\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"caption\":\"Sreeram Sreenivasan\"},\"description\":\"Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI &amp; software development.\",\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/sreeram-sreenivasan\\\/\"],\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/author\\\/wordpress\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Use CASE Statement in MySQL - Ubiq BI","description":"MySQL CASE statement helps you use multiple if conditions in SQL queries. Here is how to write CASE statement in MySQL.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/ubiq.co\/tech-blog\/how-to-write-case-statement-in-mysql\/","og_locale":"en_US","og_type":"article","og_title":"How to Use CASE Statement in MySQL - Ubiq BI","og_description":"MySQL CASE statement helps you use multiple if conditions in SQL queries. Here is how to write CASE statement in MySQL.","og_url":"https:\/\/ubiq.co\/tech-blog\/how-to-write-case-statement-in-mysql\/","og_site_name":"Ubiq BI","article_publisher":"https:\/\/www.facebook.com\/ubiqbi","article_published_time":"2021-01-25T04:54:00+00:00","article_modified_time":"2025-09-01T05:09:16+00:00","og_image":[{"width":730,"height":410,"url":"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2021\/01\/mysql-case-statement.webp","type":"image\/webp"}],"author":"Sreeram Sreenivasan","twitter_card":"summary_large_image","twitter_creator":"@UbiqBI","twitter_site":"@UbiqBI","twitter_misc":{"Written by":"Sreeram Sreenivasan","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-write-case-statement-in-mysql\/#article","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-write-case-statement-in-mysql\/"},"author":{"name":"Sreeram Sreenivasan","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"headline":"How to Use CASE Statement in MySQL","datePublished":"2021-01-25T04:54:00+00:00","dateModified":"2025-09-01T05:09:16+00:00","mainEntityOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-write-case-statement-in-mysql\/"},"wordCount":792,"commentCount":0,"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-write-case-statement-in-mysql\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2021\/01\/mysql-case-statement.webp?fit=730%2C410&ssl=1","keywords":["case statement"],"articleSection":["MySQL"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/ubiq.co\/tech-blog\/how-to-write-case-statement-in-mysql\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-write-case-statement-in-mysql\/","url":"https:\/\/ubiq.co\/tech-blog\/how-to-write-case-statement-in-mysql\/","name":"How to Use CASE Statement in MySQL - Ubiq BI","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-write-case-statement-in-mysql\/#primaryimage"},"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-write-case-statement-in-mysql\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2021\/01\/mysql-case-statement.webp?fit=730%2C410&ssl=1","datePublished":"2021-01-25T04:54:00+00:00","dateModified":"2025-09-01T05:09:16+00:00","author":{"@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"description":"MySQL CASE statement helps you use multiple if conditions in SQL queries. Here is how to write CASE statement in MySQL.","breadcrumb":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-write-case-statement-in-mysql\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/ubiq.co\/tech-blog\/how-to-write-case-statement-in-mysql\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-write-case-statement-in-mysql\/#primaryimage","url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2021\/01\/mysql-case-statement.webp?fit=730%2C410&ssl=1","contentUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2021\/01\/mysql-case-statement.webp?fit=730%2C410&ssl=1","width":730,"height":410,"caption":"mysql case statement"},{"@type":"BreadcrumbList","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-write-case-statement-in-mysql\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/ubiq.co\/tech-blog\/"},{"@type":"ListItem","position":2,"name":"How to Use CASE Statement in MySQL"}]},{"@type":"WebSite","@id":"https:\/\/ubiq.co\/tech-blog\/#website","url":"https:\/\/ubiq.co\/tech-blog\/","name":"Ubiq BI","description":"Build dashboards &amp; reports in minutes","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ubiq.co\/tech-blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc","name":"Sreeram Sreenivasan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","caption":"Sreeram Sreenivasan"},"description":"Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI &amp; software development.","sameAs":["https:\/\/www.linkedin.com\/in\/sreeram-sreenivasan\/"],"url":"https:\/\/ubiq.co\/tech-blog\/author\/wordpress\/"}]}},"jetpack_featured_media_url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2021\/01\/mysql-case-statement.webp?fit=730%2C410&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/pbGGTT-2bL","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/8417","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/comments?post=8417"}],"version-history":[{"count":3,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/8417\/revisions"}],"predecessor-version":[{"id":9387,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/8417\/revisions\/9387"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media\/8419"}],"wp:attachment":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media?parent=8417"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/categories?post=8417"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/tags?post=8417"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}