{"id":701,"date":"2024-12-07T10:17:43","date_gmt":"2024-12-07T03:17:43","guid":{"rendered":"https:\/\/www.pgtutorial.com\/?page_id=701"},"modified":"2025-01-02T20:43:29","modified_gmt":"2025-01-02T13:43:29","slug":"postgresql-case","status":"publish","type":"page","link":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-case\/","title":{"rendered":"PostgreSQL CASE Expression"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the PostgreSQL <code>CASE<\/code> expression to perform conditional logic within queries.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='getting-started-with-the-postgresql-case-expression'>Getting Started with the PostgreSQL CASE Expression <a href=\"#getting-started-with-the-postgresql-case-expression\" class=\"anchor\" id=\"getting-started-with-the-postgresql-case-expression\" title=\"Anchor for Getting Started with the PostgreSQL CASE Expression\">#<\/a><\/h2>\n\n\n\n<p>In PostgreSQL, a <code>CASE<\/code> expression is a powerful tool, allowing you to perform conditional logic within your <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-select\/\">queries<\/a>.<\/p>\n\n\n\n<p class=\"note\">The <code>CASE<\/code> expression works like an if-else statement in other programming languages.<\/p>\n\n\n\n<p>Here&#8217;s the syntax of the <code>CASE<\/code> expression:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CASE<\/span>\u00a0\u00a0\n   <span class=\"hljs-keyword\">WHEN<\/span> condition1 <span class=\"hljs-keyword\">THEN<\/span> result1\n   <span class=\"hljs-keyword\">WHEN<\/span> condition2 <span class=\"hljs-keyword\">THEN<\/span> result2\n   <span class=\"hljs-keyword\">WHEN<\/span> condition3 <span class=\"hljs-keyword\">THEN<\/span> result3\n   ...\n   <span class=\"hljs-keyword\">ELSE<\/span> result\n<span class=\"hljs-keyword\">END<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax:<\/p>\n\n\n\n<p>Each condition (<code>condition1<\/code>, <code>condition2<\/code>, <code>condition3 <\/code>&#8230;) is a <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-boolean\/\">Boolean<\/a> expression that returns <code>true<\/code> or <code>false<\/code>. The <code>CASE<\/code> expression evaluates conditions in sequence from top to bottom.<\/p>\n\n\n\n<p>If a condition is true, the <code>CASE<\/code> expression returns the corresponding result in that follow the <code>THEN<\/code>  and stops evaluating subsequent conditions.<\/p>\n\n\n\n<p>For example, if <code>condition1<\/code> is true, the <code>CASE<\/code> expression returns <code>result1<\/code> and does not evaluate <code>condition2<\/code> and <code>condition3<\/code>.<\/p>\n\n\n\n<p>If no condition is <code>true<\/code>, the <code>CASE<\/code> expression returns the <code>result<\/code> of the <code>ELSE<\/code> clause. The <code>ELSE<\/code> clause is optional. If you omit it and no condition returns true, then the <code>CASE<\/code> expression returns <code>NULL<\/code>.<\/p>\n\n\n\n<p>You can use the <code>CASE<\/code> expression within the queries where you use an expression. For example, you can use the <code>CASE<\/code> expression in the <code><code><a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-select\/\">SELECT<\/a><\/code><\/code> and <code><a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-where\/\">WHERE<\/a><\/code> clause of the <code><code>SELECT<\/code><\/code> statement.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='postgresql-case-expression-example'>PostgreSQL CASE expression example <a href=\"#postgresql-case-expression-example\" class=\"anchor\" id=\"postgresql-case-expression-example\" title=\"Anchor for PostgreSQL CASE expression example\">#<\/a><\/h2>\n\n\n\n<p>The following statement returns the product name and its total quantity in the inventory:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  product_name,\n  SUM(quantity) total_quantity\n<span class=\"hljs-keyword\">FROM<\/span>\n  inventories\n  <span class=\"hljs-keyword\">JOIN<\/span> products <span class=\"hljs-keyword\">USING<\/span> (product_id)\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  product_name\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  total_quantity;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=U0VMRUNUIHByb2R1Y3RfbmFtZSwgU1VNKHF1YW50aXR5KSB0b3RhbF9xdWFudGl0eSBGUk9NIGludmVudG9yaWVzIEpPSU4gcHJvZHVjdHMgVVNJTkcgKHByb2R1Y3RfaWQpIEdST1VQIEJZIHByb2R1Y3RfbmFtZSBPUkRFUiBCWSB0b3RhbF9xdWFudGl0eTs%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">        product_name        | total_quantity\n----------------------------+----------------\n Samsung Galaxy Z Fold 5    |            110\n Xiaomi Mi 14               |            120\n Sony Xperia 1 VI           |            130\n Apple iPhone 15 Pro Max    |            140\n Apple iPhone 15            |            150\n Samsung Galaxy Tab S9      |            160\n Apple iPad Pro 12.9        |            170\n Apple AirPods Pro 3        |            180\n Samsung Galaxy Buds Pro 2  |            190\n...<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To assign an inventory level to each product, you can use the <code>CASE<\/code> expression.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  product_name,\n  SUM(quantity) total_quantity,\n  <span class=\"hljs-keyword\">CASE<\/span>\n    <span class=\"hljs-keyword\">WHEN<\/span> SUM(quantity) &lt;= <span class=\"hljs-number\">120<\/span> <span class=\"hljs-keyword\">THEN<\/span> <span class=\"hljs-string\">'Low'<\/span>\n    <span class=\"hljs-keyword\">WHEN<\/span> SUM(quantity) &gt; <span class=\"hljs-number\">120<\/span>  <span class=\"hljs-keyword\">AND<\/span> SUM(quantity) &lt;= <span class=\"hljs-number\">150<\/span> <span class=\"hljs-keyword\">THEN<\/span> <span class=\"hljs-string\">'Medium'<\/span>\n    <span class=\"hljs-keyword\">WHEN<\/span> SUM(quantity) &gt; <span class=\"hljs-number\">150<\/span> <span class=\"hljs-keyword\">THEN<\/span> <span class=\"hljs-string\">'High'<\/span>\n  <span class=\"hljs-keyword\">END<\/span> inventory_level\n<span class=\"hljs-keyword\">FROM<\/span>\n  inventories\n  <span class=\"hljs-keyword\">JOIN<\/span> products <span class=\"hljs-keyword\">USING<\/span> (product_id)\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  product_name\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  total_quantity;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=U0VMRUNUIHByb2R1Y3RfbmFtZSwgU1VNKHF1YW50aXR5KSB0b3RhbF9xdWFudGl0eSwgQ0FTRSBXSEVOIFNVTShxdWFudGl0eSkgPD0gMTIwIFRIRU4gJ0xvdycgV0hFTiBTVU0ocXVhbnRpdHkpID4gMTIwIEFORCBTVU0ocXVhbnRpdHkpIDw9IDE1MCBUSEVOICdNZWRpdW0nIFdIRU4gU1VNKHF1YW50aXR5KSA%2BIDE1MCBUSEVOICdIaWdoJyBFTkQgaW52ZW50b3J5X2xldmVsIEZST00gaW52ZW50b3JpZXMgSk9JTiBwcm9kdWN0cyBVU0lORyAocHJvZHVjdF9pZCkgR1JPVVAgQlkgcHJvZHVjdF9uYW1lIE9SREVSIEJZIHRvdGFsX3F1YW50aXR5Ow%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">        product_name        | total_quantity | inventory_level\n----------------------------+----------------+-----------------\n Samsung Galaxy Z Fold 5    |            110 | Low\n Xiaomi Mi 14               |            120 | Low\n Sony Xperia 1 VI           |            130 | Medium\n Apple iPhone 15 Pro Max    |            140 | Medium\n Apple iPhone 15            |            150 | Medium\n Samsung Galaxy Tab S9      |            160 | High\n Apple iPad Pro 12.9        |            170 | High\n Apple AirPods Pro 3        |            180 | High\n Samsung Galaxy Buds Pro 2  |            190 | High\n...<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this statement, we assign an inventory level to each product based on its total quantity.<\/p>\n\n\n\n<p>If the total quantity is less than or equal to 120, its inventory level is low.<\/p>\n\n\n\n<p>Its inventory level is medium if the total quantity is greater than 120 and less than or equal to 150.<\/p>\n\n\n\n<p>If the total quantity exceeds 150, then the inventory is high.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='simple-postgresql-case-expression'>Simple PostgreSQL CASE expression <a href=\"#simple-postgresql-case-expression\" class=\"anchor\" id=\"simple-postgresql-case-expression\" title=\"Anchor for Simple PostgreSQL CASE expression\">#<\/a><\/h2>\n\n\n\n<p>If you have one condition to evaluate, then you can use an alternative <code>CASE<\/code> expression:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CASE<\/span> expression\n  <span class=\"hljs-keyword\">WHEN<\/span> value1 <span class=\"hljs-keyword\">THEN<\/span> result1\n  <span class=\"hljs-keyword\">WHEN<\/span> value2 <span class=\"hljs-keyword\">THEN<\/span> result2\n  <span class=\"hljs-keyword\">WHEN<\/span> value3 <span class=\"hljs-keyword\">THEN<\/span> result3\n  ...\n  <span class=\"hljs-keyword\">ELSE<\/span> result\n<span class=\"hljs-keyword\">END<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax, the <code>CASE<\/code> evaluates the expression, compares its result with <code>value1<\/code>, <code>value2<\/code>, <code>value3<\/code> &#8230;, and returns the corresponding result.<\/p>\n\n\n\n<p>If the result of the expression does not match any value, then the <code>CASE<\/code> expression returns the <code>result<\/code> in the <code>ELSE<\/code> clause.<\/p>\n\n\n\n<p>The <code><code>ELSE<\/code><\/code> clause is optional. If you omit the <code><code>ELSE<\/code><\/code> clause and the expression does not match any value (<code>value1<\/code>, <code>value2<\/code>, <code>value3<\/code>, &#8230;), then the <code>CASE<\/code> expression returns <code>NULL<\/code>.<\/p>\n\n\n\n<p>The following example uses the simple <code>CASE<\/code> expression to make the inventory transaction more clear:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  transaction_id,\n  <span class=\"hljs-keyword\">type<\/span>,\n  <span class=\"hljs-keyword\">CASE<\/span> <span class=\"hljs-keyword\">type<\/span>\n    <span class=\"hljs-keyword\">WHEN<\/span> <span class=\"hljs-string\">'issue'<\/span> <span class=\"hljs-keyword\">THEN<\/span> <span class=\"hljs-string\">'Goods Issue'<\/span>\n    <span class=\"hljs-keyword\">WHEN<\/span> <span class=\"hljs-string\">'receipt'<\/span> <span class=\"hljs-keyword\">THEN<\/span> <span class=\"hljs-string\">'Goods Receipt'<\/span>\n  <span class=\"hljs-keyword\">END<\/span> transaction_type\n<span class=\"hljs-keyword\">FROM<\/span>\n  transactions;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=U0VMRUNUIHRyYW5zYWN0aW9uX2lkLCBDQVNFIHR5cGUgV0hFTiAnaXNzdWUnIFRIRU4gJ0dvb2RzIElzc3VlJyBXSEVOICdyZWNlaXB0JyBUSEVOICdHb29kcyBSZWNlaXB0JyBFTkQgdHJhbnNhY3Rpb25fdHlwZSBGUk9NIHRyYW5zYWN0aW9uczs%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\"> transaction_id |  type   | transaction_type\n----------------+---------+------------------\n              1 | issue   | Goods Issue\n              2 | issue   | Goods Issue\n              3 | issue   | Goods Issue\n              4 | receipt | Goods Receipt\n              5 | receipt | Goods Receipt\n...<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, the <code>CASE<\/code> expression evaluates the values in the <code>type<\/code> column of the <code>transactions<\/code> table. If the value is <code>'issue'<\/code> , it returns <code>'Goods Issue'<\/code> . If the <code>value<\/code> is <code>'receipt'<\/code>, it returns &#8216;<code>Goods Receipt<\/code>&#8216;.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the <code>CASE<\/code> expression to add conditional logic to your queries.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=case\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\n\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"701\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-case\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL CASE Expression\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"701\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-case\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL CASE Expression\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn how to use the PostgreSQL CASE expression to perform conditional logic within queries.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":13,"menu_order":61,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-701","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PostgreSQL CASE Expression<\/title>\n<meta name=\"description\" content=\"In this tutorial, you&#039;ll learn how to use the PostgreSQL CASE expression to perform conditional logic within queries.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-case\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PostgreSQL CASE Expression\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you&#039;ll learn how to use the PostgreSQL CASE expression to perform conditional logic within queries.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-case\/\" \/>\n<meta property=\"og:site_name\" content=\"PostgreSQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2025-01-02T13:43:29+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-case\\\/\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-case\\\/\",\"name\":\"PostgreSQL CASE Expression\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\"},\"datePublished\":\"2024-12-07T03:17:43+00:00\",\"dateModified\":\"2025-01-02T13:43:29+00:00\",\"description\":\"In this tutorial, you'll learn how to use the PostgreSQL CASE expression to perform conditional logic within queries.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-case\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-case\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-case\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PostgreSQL Tutorial\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"PostgreSQL CASE Expression\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/\",\"name\":\"PostgreSQL Tutorial\",\"description\":\"Learn PostgreSQL from Scratch\",\"alternateName\":\"PostgreSQL\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.pgtutorial.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PostgreSQL CASE Expression","description":"In this tutorial, you'll learn how to use the PostgreSQL CASE expression to perform conditional logic within queries.","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:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-case\/","og_locale":"en_US","og_type":"article","og_title":"PostgreSQL CASE Expression","og_description":"In this tutorial, you'll learn how to use the PostgreSQL CASE expression to perform conditional logic within queries.","og_url":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-case\/","og_site_name":"PostgreSQL Tutorial","article_modified_time":"2025-01-02T13:43:29+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-case\/","url":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-case\/","name":"PostgreSQL CASE Expression","isPartOf":{"@id":"https:\/\/www.pgtutorial.com\/#website"},"datePublished":"2024-12-07T03:17:43+00:00","dateModified":"2025-01-02T13:43:29+00:00","description":"In this tutorial, you'll learn how to use the PostgreSQL CASE expression to perform conditional logic within queries.","breadcrumb":{"@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-case\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-case\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-case\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pgtutorial.com\/"},{"@type":"ListItem","position":2,"name":"PostgreSQL Tutorial","item":"https:\/\/www.pgtutorial.com\/"},{"@type":"ListItem","position":3,"name":"PostgreSQL CASE Expression"}]},{"@type":"WebSite","@id":"https:\/\/www.pgtutorial.com\/#website","url":"https:\/\/www.pgtutorial.com\/","name":"PostgreSQL Tutorial","description":"Learn PostgreSQL from Scratch","alternateName":"PostgreSQL","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pgtutorial.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/701","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/comments?post=701"}],"version-history":[{"count":8,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/701\/revisions"}],"predecessor-version":[{"id":1389,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/701\/revisions\/1389"}],"up":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/13"}],"wp:attachment":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/media?parent=701"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}