Join Bill Weinman for an in-depth discussion in this video Conditional expressions with CASE, part of SQL Essential Training.
SQL provides a form of conditional expression. It's a little cumbersome, but here's how it works. I'm going to use the test.db database. I'm going to copy and paste from the exercise files for chapter 2 here, under CASE this is around line 157. I'm going to grab these 3 lines that create a table and insert a row. The table is called bull test, going to paste this into our box here and press go. Boolean's in SQL lite are integers with a value of 0 for false and anything else for true.
So Boolean's are often handled like this with an integer column. And so I've inserted one row and there's a one or a true in column A. And a zero or a false in column B. And so we'll leave that select there. And I'm going to create another select after it. With a case statement in it. And it's probably easiest to just copy and paste this as well. And this case expression, it's not actually a statement, it's an expression, is a conditional expression. So, how this works, it's introduced with case and it ends with end.
And the when clause tests for true or false. If I say when A, then A is true in this column, and there's a then and an else. And the then is the string true, and the else is the string false. Remember this is an expression, so the expression is going to return one of these two values, either the then expression or the else expression. And we're testing both A and B, and you'll see in our result we get boolA is true and boolB is false. I use the as alias here for both of these.
And you see it's a little cumbersome but it is pretty simple. There's what we're testing with the when for when is true, it'll return the then expression. And when the when is false, it'll return the else expression. It's really pretty simple. There is one other form of this, though, and it's just a little bit different. So I'm going to leave both of these in here, so we can look at them both. In this case, it actually has an expression after case. And this is the thing that's being tested for true or false.
So it usually represents a value. So in this case, it's the value in column A or the value in column B. And then when it is equal to this expression and that will be true, other wise it will be false. So case A when 1 means when A is equal to 1. And case B when 1 means when B is equal to 1. And you see we get exactly the same result here. It's just two different forms of the conditional expression. So the CASE expression can be a bit awkward, but it's very useful and you'd do well to become familiar with it.
Use CASE wherever you need a conditional in SQL and you'll see one or two other examples of it later in this course. Now I'm going to remove our temporary table to restore the database to it's original state. And I do that with a simple drop statement. And now our database is back the way it should be for the next chapter.
- Understanding SQL terminology and syntax
- Creating new tables and records
- Inserting and updating data
- Writing basic SQL queries
- Sorting and filtering
- Accessing related tables with JOIN
- Working with strings
- Finding the numeric type of a value
- Using aggregate functions and transactions
- Updating a table with triggers
- Creating views
Skill Level Beginner
Q: For Mac OS X: When I try to start the Apache Web Server from the XAMPP control panel, it doesn't start, and when I open "localhost" in my web browser, I see a white screen that says "It Works!" instead of the XAMPP page.
sudo apachectl stop
Q: I'm on a Mac, and I get an error in SID that says "attempt to write a read only database." How can I fix this?
A: This usually means that the database folder does not have sufficient permissions for writing by the web user. This can happen if you create the SQL folder new, rather than copying it from the Exercise Files. Here's how to fix this:
- Open a Finder window and Navigate to /Applications/XAMPP/htdocs/SQL
- Control-click on the SQL folder and select "Get Info" from the context menu.
- Under "Sharing and Permissions" (you may need to open the disclosure triangle), in the "everyone" row, select "Read & Write."Then you can close the Info window.
- Now repeat the process for the three *.db files inside the folder.