BigQuery | All About That Case…Statement | Complete Guide

Tom Ellyatt
13 min readJul 31, 2022

In this article we’ll cover the basics of a case statement, going right up to some advanced use-cases. Strap in!

IMPORTANT
If you’re familiar with the basics of a case statement, like what it is, its syntax and its general usage in the SELECT statement, then to be respectful of your time I highly suggest you scroll on down to the section titled ‘Advanced’

Basics

What even is a case statement?

You can think of a case statement like an if statement, as fundamentally they both work on the same true/false logic. We specify a condition, which if met (true), leads to an action, if the condition is not met (false), then do something else.

The formula for an IF statement looks like this.

IF(specify condition, true result, false result)

Each and every one of us goes through this simple logic in our every day lives:

  • Perhaps I want to buy a new pc
    -If my bank balance is greater than the cost of a new pc,
    -then buy it!
    -if not, keep saving
  • What should I say if someone says ‘STOP’?
    -
    If someone says ‘Stop’ to me,
    -then reply ‘Hammer Time’,
    -if not, carry on with whatever it is that I’m doing

--

--

Tom Ellyatt

Google Cloud Digital Leader | Customer Insight Analyst