You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -526,6 +527,111 @@ The total price of all computers in the `products` table:
526
527
527
528
-[SQLBolt - Queries with expressions](https://sqlbolt.com/lesson/select_queries_with_expressions)
528
529
530
+
## CASE
531
+
532
+
The `CASE` statement is similar to a chain of conditional statements in a language like Python or JavaScript. You use it to generate different values based on some condition. At CWHQ, we use the `CASE` statement to generate an additional column with a range of values generated from our other columns.
533
+
534
+
Consider a shopping app where we want to rank products by their affordability. Any product that costs $100 or less is considered "Cheap", any product between $100 and $1000 is considered "Affordable", and anything else is "Expensive".
Notice that there is no `affordability` column? We can create one and populate it with values based on the `product_price` by using the `CASE` statement and using `AS` to ensure the result is placed in a column called `affordability`.
556
+
557
+
The `THEN` keyword is what populates the values in whatever column name we created with `END AS`. If you have an `ELSE` clause, it doesn't need a `THEN` clause.
SQLite uses the `||` operator for string concatenation. This is used at CWHQ to combine column names together (possibly with other string data) to merge the data from multiple columns into a single column. This is often used in conjunction with the `AS` clause to rename the combined columns.
0 commit comments