- 7 weeks ago
Case statements in PostgreSQL make conditional logic easy, and this lesson breaks them down with clear examples you can use right away. Using a Star Wars dataset, the tutorial shows how CASE works like an if else statement, then builds from a simple human versus alien label to more practical bucketed categories based on estimated net worth.
The lesson also covers multiple conditions in one CASE expression, including checking whether a character is human and has both arms, and it shows how CASE can return calculated values such as tax amounts instead of just text labels. You’ll also see how order matters when conditions overlap, plus a useful trick for using CASE inside ORDER BY to sort humans and droids first.
This PostgreSQL tutorial is a hands-on guide for SQL beginners and anyone learning database logic, conditional queries, data categorization, and sorting with CASE WHEN. It’s useful for practice, SQL interview prep, and building more advanced PostgreSQL queries with confidence.
The lesson also covers multiple conditions in one CASE expression, including checking whether a character is human and has both arms, and it shows how CASE can return calculated values such as tax amounts instead of just text labels. You’ll also see how order matters when conditions overlap, plus a useful trick for using CASE inside ORDER BY to sort humans and droids first.
This PostgreSQL tutorial is a hands-on guide for SQL beginners and anyone learning database logic, conditional queries, data categorization, and sorting with CASE WHEN. It’s useful for practice, SQL interview prep, and building more advanced PostgreSQL queries with confidence.
Category
📚
LearningTranscript
00:00What's going on, everybody? Welcome back to another video. Today, we're continuing our
00:03PostgreSQL series, and in this lesson, we are looking at case statements. Now, case statements
00:08are kind of like if-else statements. If you've ever done any type of programming language or
00:13even done things in Excel, where you have some type of condition, and if that condition is met,
00:18then something happens. If it is not met, something else happens. Let's look at this
00:23in a really simple example. So right here, we start our code. We are going to specify a condition,
00:29and if it is true, then we'll do this body of code. If it is false, we'll do this body
00:37of code,
00:37and we also have an else statement, and we'll look at that as well, but it just depends on whether
00:43the
00:43condition is true or not, and that is case statements. It evaluates the statement, determines
00:48if it's true or false, and then does something with it, and then we finish our code, and that's it.
00:54We can have multiple conditions needing to be evaluated at the same time. So in essence,
00:59this is quite simple, but it gets a little bit more advanced as we go, and so we'll start with
01:04the really simple stuff, and you'll pick it up right away. I know you will, and then I'll show
01:08you a little bit more of the advanced stuff, and I think by the end of this, you're going to
01:12be
01:12nailing case statements. You're going to want to go and start writing it in all your code because it
01:16is really helpful. Now, if you haven't already, we are working with our Star Wars data set.
01:21I will have it right over here. You can get that down below in the description. You just download
01:26it, run the sequel, and you will have our three tables or four tables now since the last lesson
01:31we added another one, and you'll be able to follow along with us. But here we have all of our
01:34Star
01:34Wars characters, and we're going to take this from kind of a human perspective. They're either human
01:39or they're not, right? They're human or they're aliens. They're some type of wookie, weird-looking
01:45thing. I'm just kidding. I'm sorry, Chewbacca. But what we want to do is we want to kind of
01:50categorize these people. And all we're going to want is the character name, and then we
01:57want the species. Oops, species. And then we're going to do this case statement. So
02:03I'm going to come right here. I'm going to say case, and I'm going to go down, and I'm
02:07going to tab over, and I'm going to say when. So this is the syntax that we're using. We're
02:10using this case when and when then. And we'll look at that in just a second. So we're going
02:15to say when the species is equal to human. We're going to say then they're normal. And
02:24I'm actually going to use, I just like single ticks better. And then we're going to come
02:27here and we're just going to say else, and all the other ones are aliens. And that's all
02:32we're going to say. Case, when, and else. And that's it. So we're saying case. When the
02:39species is equal to human, then they're normal. If they're not, then they're aliens. Now, we
02:45have to end this by literally saying end. And then we're going to label this. So we're
02:51going to say as, we'll call this species identifier. If I can spell that, identifier. I nailed
02:58it. All right, let's go ahead and run this. Now when we come down here, we have our character
03:02name. We have our species. So human is normal. Human is normal. Human is normal. Wookiee, aliens.
03:07I probably should say alien, actually. That just makes more sense. So we have normal and
03:13alien. So whenever it's unknown, droid, zebrack, gungan, it doesn't matter. If it's not human,
03:20it's normal. This is essentially any if-else statement in any other tool, any other programming
03:26language, anything. It's just slightly different syntax for SQL. So we're using case. When this
03:33condition right here is true, then it's normal. If it's false, it goes to the next line, which
03:39is just alien. Everything else is alien. And then we end it. And that's it. That is the
03:45simplest example. Now, let's come down here and let's put this here. We're going to change
03:53this up a little bit. And we're going to make this a little bit more useful. And let's pull
03:57this. I'm just going to show this to you. So we'll select everything from character info.
04:03I should be having some semicolons around here just so we can see it. But we have over here
04:09this estimated net worth. And I do things like this often with case daemons, where I want to
04:15kind of bucket these people into different categories. Maybe they're ultra wealthy. Maybe
04:20they're wealthy. Or maybe they're poor. Maybe we want to say low income to not be rude to the
04:25droids because they're very important. They just don't have much money. But we can put
04:30these in buckets. And bucketing is really useful with case statements. In fact, I would use it
04:35with things like birth dates. You can bucket people into certain categories of age ranges
04:40if you want to do that. But I'm going to demonstrate it with this estimated net worth. So let's get
04:46rid
04:46of all of this. We're going to do case when. And we want the estimated net worth. And we're also
04:56going
04:56to put this right up here because I want it in my output. So when the estimated net worth, and
05:01let's
05:01provide our condition, we're going to say is greater than or equal to. Now we need to specify a value.
05:08What is it going to be for this first value? Now this really matters because when we have
05:14multiple conditions, it's going to perform this in the order in which you write it.
05:18I'll demonstrate that in a little bit how you might mess it up. But let's say anyone worth more
05:22than $100,000. If they're worth more than $100,000, then we're going to label them. I'm just going to
05:31call them the ultra wealthy. I mean, these people are rolling in dough. But let's add another bucket.
05:39So now we're going to take this exact same format. But now if they have more than $10,000,
05:46we're just going to say they're middle class. How about that? Middle class. And let's add a little
05:54space here. And then anyone else. So we're going to say else. They are just low income. Now we need
06:03to end this. So we're going to say end. And we'll say as wealth underscore class. Now let's go ahead
06:10and run this. And let's see what we get in our output. So we have Luke Skywalker. He makes $150
06:17,000.
06:17He's ultra wealthy. So we got a lot of ultra wealthy people here. Now Yoda, he's a very altruistic
06:23person. He's not making all the money or at least keeping it. He's middle class. And then we have our
06:27droids here. Poor guys. They are both low income. Obi-Wan, Darth Maul, they're both ultra wealthy.
06:33And then Jar Jar Binks, low income. Again, I think this is from like the first time he was introduced.
06:38Once he becomes like a war hero general, he's very wealthy. So this must be from when he was like
06:43first
06:44introduced. Anyways, that's not important. Now this output is perfect. But the order of operations
06:50actually matters a lot. Because what happens is, is it goes, it says case. If this condition is true
06:57right here, then we stop. It isn't going to go to the next one or the next one. It's just
07:03going to
07:03stop at this first one. So let's just, for the sake of things, let's change this around. Let's put this
07:10right here. So now we're saying when the estimated net worth is greater than 10,000, they're going to be
07:16middle class. Let's go ahead and run this and see what happens. Now you'll notice that nobody is
07:22ultra wealthy. Or at least they don't get assigned. So we're incorrectly assigning people because what's
07:29happening is, is it's saying, is their net worth greater than 10,000? If yes, then they're middle
07:34class. So even though Luke Skywalker makes 150,000, it never reaches this point because this is going to
07:42happen every single time that this is true. So the order in which you place things is really
07:48important. And as it gets more advanced, you may have multiple conditions that need to be met. And so
07:52you want to make sure that you're aligning these in the proper order. You don't want those to be
07:58mixed up. So this one to me looks really good. Now let's copy this down below and let's run this
08:05query
08:06one more time. Now let's look at an example of using multiple conditions because sometimes it is as simple
08:12as just one condition needing to be met. But maybe you want to include other conditions in each of your
08:20win then statements. So let's get rid of all of this. You'll notice down here we have species of humans
08:27and
08:27this odd column that has both arms. And it is unfortunate, but there are many characters in Star Wars that
08:34just
08:35don't have both arms. Luke Skywalker doesn't. Darth Vader doesn't. Darth Maul. A lot of them in the
08:42later movies have robotic arms because they got them chopped off because they were just wielding
08:47lightsabers and fighting each other across the universe. And that's a problem. And so what we want
08:51to do is we want to label whether these humans are fully functional or they are enhanced. For example,
08:58Darth Vader, yes, he lost his arm and got a robotic arm with that. And that's great. But he lost
09:05a lot of his metachlorians and he wasn't as powerful as a Jedi. But robotically, I mean,
09:09that's a pretty big advancement. But he wasn't fully functional. So what we're going to do is we're
09:14going to come right here and we're going to do the character name. And now we're going to choose
09:19species, comma, has both arms. And now we'll do two conditions. So we want the species to be equal to
09:30human and case sensitive. We want to be able to human and has both arms is going to be equal
09:39to
09:40yes. If they have both arms, we're going to say then they are fully functional. And let's get rid of
09:49that. Then we're going to say when. And we're going to basically copy this whole thing. The species is
09:57humans and have both arms is equal to no. We're going to say that they are, let's just call them
10:04damaged. I don't know if that's the right term. There we go. Damaged. And then we'll say else.
10:11Instead of a string, we're just going to put no. And let me actually put that in all caps.
10:16So we're going to say end as human evaluation. Now let's go ahead and run this. Let's look at
10:25our output. So if Luke Skywalker is both a human and he has no for both arms, he is damaged.
10:33But
10:34Leia, she's a human and she has yes for both arms. So she's fully functional. Yoda is not a human.
10:40These droids are not human. So they're just going to have null in their output. So we can have
10:45multiple conditions being met in order to get some type of output. Now we're going to look at two more
10:52things. And these are just, you know, things that I think are really useful. Now let's come copy
10:58this estimated net worth because we're going to use that column again. And we'll run this query
11:03just so we have it up and about. Now so far, when we've run this estimated net worth, we say
11:10then,
11:11and we've just done a string. That's all we've done. But we don't have to do that. In fact,
11:17we can perform calculations in this then. For example, let's say this is a specific range of
11:24net worth. And we're figuring out the taxes. So let's come over here, we're going to say taxes,
11:29this is how much taxes they owe. What we can do is we're going to say estimated net worth,
11:34we're going to perform a calculation, we're going to say estimated net worth times,
11:38and we can specify how much taxes on their net worth that they need to pay. For example,
11:44these people are the ultra wealthy, they're going to pay 50% in taxes. But then people who
11:51make 10,000 or less, they're just going to have to pay 10% and low income, we're going to
11:58do the
11:58same thing, we're going to get rid of this, those people who are low income are going to pay 0
12:03.05%.
12:04So now, let's run this. And now we'll see, here's how much they're worth. And here's how much they owe
12:12in taxes. And so we're able to perform these calculations for different groups. So you'll
12:19see Luke Skywalker, he's a high net individual, he's paying 50%. That's 75,000. But if we look at
12:25R2D2, poor R2D2, he's only going to pay 5% in taxes, that's $100, or whatever currency R2D2 uses.
12:34And then we have Yoda, he's our middle income, that's 50,000. And he's going to pay 10%, which is
12:39$5,000. And so we're able to perform calculations based off of these conditions being met as well.
12:46I've used this for a ton of different scenarios. I think that one's really useful. And the last thing
12:51that I want to show you is something maybe you haven't seen before, but I've used it before.
12:56I think it's, you know, pretty neat. Let's actually go and just get this base query here. Now, what most
13:04people use this for is exactly what we just did. They'll do it in the select statement. And that's
13:08perfectly fine. But you don't have to only use it in the select statement, we can actually use it
13:15in something like an order by. Let's just say, for example, we wanted to order on the species.
13:21And let me spell this right. And I'll say, just do alphabetical. So let's run this. And so we have
13:29droid, Gungan, human, unknown, Wookiees, and Zabrak. That's in alphabetical order. But maybe I don't
13:37want that. Maybe I actually want the humans to come first. Maybe they're the most important one in
13:41whatever we're doing. So let's use a case statement here, in order to specify this. So
13:47we're going to say order by, and we'll say case. And let's say, when the species is equal to human,
13:55and then we're going to do something, and we're going to use a number here, which is one. Now,
14:00in the order by, if you took my order by lesson, you can order by multiple columns. So you can
14:06have
14:06the first column, then the second column, then the third one, we're just assigning where this is
14:10placed. And so we're saying, this is the first one that we want. And so then we can come here,
14:15and we say, when maybe our next one that we want to look at is the droids. When the species
14:20is droid,
14:23and I think I need to capitalize that, then it's two. And then for everyone else, we don't really care.
14:29We're just going to say three. And then we'll end to this. Let's go ahead and run this.
14:35So now we have humans that are first, then we have the droids, and then it looks like it's actually
14:40in random order, because the unknown is coming after the W. But we don't care. We didn't specify
14:45that in this. Otherwise, we would have. We could specify all of them if we wanted to.
14:50So that is case statements in a nutshell. We're evaluating a condition or conditions,
14:55and then we're doing something with that. If it evaluates true, or it evaluates to false.
15:00That's what case statements are. They're really useful. And I hope that you're able to learn in this
15:04lesson how to use them. Be sure to just mess around with them, try different things out,
15:09and see what you can do with it, because that's how you learn. If you like this video,
15:13be sure to like and subscribe, and I will see you in the next lesson.