Title: SQL Case Statement - Examples Java Code Geeks - 2026
Open Graph Title: SQL Case Statement - Examples Java Code Geeks - 2026
Description: Hello. In this tutorial, we will learn the SQL Case statements and will use the PostgreSQL database running on Docker.
Open Graph Description: Hello. In this tutorial, we will learn the SQL Case statements and will use the PostgreSQL database running on Docker.
Mail addresses
support@javacodegeeks.com
Opengraph URL: https://examples.javacodegeeks.com/sql-case-statements/
Generator: WordPress 7.0.2
Domain: examples.javacodegeeks.com
{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/examples.javacodegeeks.com\/sql-case-statements\/#article","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/sql-case-statements\/"},"author":{"name":"Yatin","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/9874407a37b028e8be3276e2b5960d13"},"headline":"SQL Case Statement","datePublished":"2022-08-29T08:00:00+00:00","mainEntityOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/sql-case-statements\/"},"wordCount":741,"commentCount":0,"publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/sql-case-statements\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","keywords":["PostgreSQL","sql"],"articleSection":["sql"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/examples.javacodegeeks.com\/sql-case-statements\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/sql-case-statements\/","url":"https:\/\/examples.javacodegeeks.com\/sql-case-statements\/","name":"SQL Case Statement - Examples Java Code Geeks - 2026","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/sql-case-statements\/#primaryimage"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/sql-case-statements\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","datePublished":"2022-08-29T08:00:00+00:00","description":"Hello. In this tutorial, we will learn the SQL Case statements and will use the PostgreSQL database running on Docker.","breadcrumb":{"@id":"https:\/\/examples.javacodegeeks.com\/sql-case-statements\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/examples.javacodegeeks.com\/sql-case-statements\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/sql-case-statements\/#primaryimage","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","width":150,"height":150,"caption":"Bipartite Graph"},{"@type":"BreadcrumbList","@id":"https:\/\/examples.javacodegeeks.com\/sql-case-statements\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/examples.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java Development","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/"},{"@type":"ListItem","position":3,"name":"Enterprise Java","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/enterprise-java\/"},{"@type":"ListItem","position":4,"name":"sql","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/enterprise-java\/sql-enterprise-java\/"},{"@type":"ListItem","position":5,"name":"SQL Case Statement"}]},{"@type":"WebSite","@id":"https:\/\/examples.javacodegeeks.com\/#website","url":"https:\/\/examples.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/examples.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/examples.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/9874407a37b028e8be3276e2b5960d13","name":"Yatin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2023\/09\/cropped-Yatin-Batra_avatar_1515758148-96x96.jpg","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2023\/09\/cropped-Yatin-Batra_avatar_1515758148-96x96.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2023\/09\/cropped-Yatin-Batra_avatar_1515758148-96x96.jpg","caption":"Yatin"},"description":"An experience full-stack engineer well versed with Core Java, Spring\/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).","sameAs":["https:\/\/www.javacodegeeks.com"],"url":"https:\/\/examples.javacodegeeks.com\/author\/yatin-batra\/"}]}
{"@context":"http:\/\/schema.org","@type":"BreadcrumbList","@id":"#Breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"item":{"name":"Home","@id":"https:\/\/examples.javacodegeeks.com\/"}},{"@type":"ListItem","position":2,"item":{"name":"Java Development","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/"}},{"@type":"ListItem","position":3,"item":{"name":"Enterprise Java","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/enterprise-java\/"}},{"@type":"ListItem","position":4,"item":{"name":"sql","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/enterprise-java\/sql-enterprise-java\/"}}]}
{"@context":"http:\/\/schema.org","@type":"Article","dateCreated":"2022-08-29T11:00:00+03:00","datePublished":"2022-08-29T11:00:00+03:00","dateModified":"2022-08-26T12:36:46+03:00","headline":"SQL Case Statement","name":"SQL Case Statement","keywords":"PostgreSQL,sql","url":"https:\/\/examples.javacodegeeks.com\/sql-case-statements\/","description":"Hello. In this tutorial, we will learn the SQL Case statements and will use the postgresql database running on Docker. 1. Introduction SQL stands for Structured Query Language and is used to extract a","copyrightYear":"2022","articleSection":"sql","articleBody":"Hello. In this tutorial, we will learn the SQL Case statements and will use the postgresql database running on Docker.\n1. Introduction\nSQL stands for Structured Query Language and is used to extract and organize data stored in relational databases like MySQL, PostgreSQL, Oracle, etc. Also, a\u00a0relational database consists of rows and columns that allow fetching specific information from databases that can be used later for analysis. In real-time, SQL manages a large amount of data that is written and read simultaneously and any query that reaches the SQL server is processed into three parts \u2013\n\nParsing \u2013 Process to check the SQL query syntax\nBinding \u2013 Process to check the SQL query semantics\nOptimization \u2013 Process to generate the SQL query execution plan\n\n1.1 Usage of SQL\nStructured Query Language (popularly known as SQL) is commonly used by data analysts and data science professionals and is helpful too \u2013\n\nExecute queries against the database\nRetrieve data from the database\nInsert new records into the database\nUpdate existing records into the database\nCreated stored procedures, functions, and materialized views in the database\nCreate users and grant permissions\nSet permissions on tables, stored procedures, functions, and materialized views\n\n1.2 What is a CASE statement?\nPostgresql CASE keyword in a sql statement is the same as the if\/else statement in the programming language. It is represented by the below syntax in the sql query \u2013\nCASE syntax\nCASE \n WHEN condition_1 THEN result_1\n WHEN condition_2 THEN result_2\n ...\n ELSE \"other_result\"\nEND\n\n1.2.1 Types of CASE statements\nThere are two types of CASE statements i.e.\n\nSimple statement \u2013 Here the search expression is compared to each expression from top to bottom. If found its corresponding result is executed and no further comparison is done. If no match is found then the optional ELSE statement is executed. If no else statement in the CASE statement a sql CASE-NOT-FOUND exception is thrown\nSearched statement \u2013 Here each Boolean expression is evaluated from top to bottom. If true, the corresponding result is executed and no further comparison is done. If no match is found then the optional ELSE statement is executed. If no else statement in the CASE statement a sql CASE-NOT-FOUND exception is thrown\n\n2. SQL CASE statements\nLet us dive into some practice implementation on the postgresql database.\n2.1 Pre-requirement - Postgres Setup\nUsually, setting up the database is a tedious step but with Docker, it is a simple process. You can watch the video available at this link to understand the Docker installation on Windows OS. Afterward, \u00a0open the terminal and trigger the below command to set up and run postgresql.\nDocker commands\n-- command to run postgres on docker --\n\n-- remember to change the password --\ndocker run -d -p 5433:5432 -e POSTGRES_PASSWORD= --name postgres postgres\n\n-- command to stop the Postgres docker container --\ndocker stop postgres\n\n-- command to remove the Postgres docker container --\ndocker rm postgres\n\nRemember to enter the password of your choice. If everything goes well the postgresql database server would be up and running on a port number \u2013 5433 and you can connect with the Dbeaver GUI tool for connecting to the server.\n\n\nFig. 1. Postgres on Docker\n\n\n2.2. Creating a database\nTo practice this tutorial I will be using the dvdrental database. You can below command to create a database.\nCreate database sql query\n-- create database\ncreate database dvdrental\n\n2.3 Creating table and inserting data\nFor this tutorial, I am using the sql dump provided by postgresql and have restored it into the postgresql server running on my machine inside the dvdrental database. The sql dump can be downloaded from this link. Once the dump is downloaded it can be restored via the below command.\nRestore db dump\n-- restoring dump sql pg_restore command\npg_restore -d dvdrental path_where_dump_is_downloaded\\dvdrental.tar\n\nOnce the dump is restored in the dvdrental database use the SELECT command to verify. In this tutorial, I will be using the film table for CASE statements so let us view this table.\n\n\nFig. 2: Film table data\n\n\n2.4 Playing with the CASE statement\nLet us see some examples.\n2.4.1 Simple case statement\nThe below sql query will fetch the records from the film table and will replace the rating column values based on the case statement.\nSimple case statement query\n-- query1: example of a simple case statement\nselect film_id, title, description, length, release_year, \ncase rating\n\twhen 'PG' then 'parental guidance'\n\twhen 'G' then 'general auidence'\n\twhen 'NC-17' then 'inappropriate for ages 17 and under'\n\twhen 'R' then 'adult'\n\twhen 'PG-13' then 'parental strongly cautioned'\n\telse 'undefined'\nend rating\nfrom film order by film_id asc;\n\nIf everything goes well the below result will be shown.\n\n\nFig. 3: Query output 1\n\n\n2.4.2 Searched case statement\nThe below sql query will fetch the records from the film table and will create an additional column named - duration on runtime to map the value in the length with a string.\nSimple case statement query\n-- query2: example of a searched case statement\nselect film_id, title, description, release_year, length, \ncase\n\twhen length >= 0 and length < 100 then 'short'\n\twhen length >= 100 and length < 200 then 'medium'\n\twhen length >= 200 and length < 300 then 'long'\n\telse 'very long'\nend as duration\nfrom film order by film_id asc;\n\nIf everything goes well the below result will be shown.\n\n\nFig. 4: Query output 2\n\n\n3. Summary\nIn this tutorial, we learned about SQL Case statements and their practical implementation via different examples. You can download the SQL scripts used in this tutorial from the Downloads section.\n4. Download the SQL scripts\nThis was a tutorial to understand the SQL Case statements.\nDownloadYou can download the full source code of this example here: SQL CASE statement","publisher":{"@id":"#Publisher","@type":"Organization","name":"Examples Java Code Geeks","logo":{"@type":"ImageObject","url":"\/wp-content\/uploads\/2013\/05\/JavaCodeGeeks-logo.png"},"sameAs":["https:\/\/feeds.feedburner.com\/examplesjavacodegeeks","https:\/\/www.facebook.com\/javacodegeeks","https:\/\/twitter.com\/javacodegeeks","https:\/\/www.linkedin.com\/groups\/3810709\/","https:\/\/www.youtube.com\/channel\/UCxoUc7Rar2q90Gu0nT2ffuQ","https:\/\/github.com\/javacodegeeks\/"]},"sourceOrganization":{"@id":"#Publisher"},"copyrightHolder":{"@id":"#Publisher"},"mainEntityOfPage":{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/sql-case-statements\/","breadcrumb":{"@id":"#Breadcrumb"}},"author":{"@type":"Person","name":"Yatin","url":"https:\/\/examples.javacodegeeks.com\/author\/yatin-batra\/"},"image":{"@type":"ImageObject","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","width":1200,"height":150}}
| og:locale | en_US |
| og:type | article |
| og:site_name | Examples Java Code Geeks |
| article:publisher | https://www.facebook.com/javacodegeeks |
| article:published_time | 2022-08-29T08:00:00+00:00 |
| og:image | https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg |
| og:image:width | 150 |
| og:image:height | 150 |
| og:image:type | image/jpeg |
| author | Yatin |
| twitter:card | summary_large_image |
| twitter:creator | @javacodegeeks |
| twitter:label1 | Written by |
| twitter:data1 | Yatin |
| twitter:label2 | Est. reading time |
| twitter:data2 | 5 minutes |
| None | IE=edge |
| msapplication-TileImage | https://examples.javacodegeeks.com/wp-content/uploads/2013/05/JavaCodeGeeks-favIcon.png |
Links:
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow, max-image-preview:large, max-snippet:-1, max-video-preview:-1