Skip to content

Master Conditional Expressions in BigQuery

Firstly, Let’s create a table called personnel and stored in BigQuery.

CREATE TABLE tutorial.personnel (
  name STRING,
  age INT64,
  salary FLOAT64
);
INSERT INTO tutorial.personnel(name, age, salary) VALUES
  ('John', 30, 50000.00),
  ('Alice', 25, 60000.00),
  ('Bob', 35, NULL),
  ('Emma', NULL, 70000.00),
  (NULL, 28, 55000.00),
  ('Michael', 40, NULL),
  ('Sara', 33, 65000.00),
  (NULL, NULL, NULL),
  ('David', 45, 75000.00),
  ('Sophia', 27, 58000.00);
Rownameagesalary
1nullnullnull
2Bob35null
3Michael40null
4Emmanull70000.0
5Alice2560000.0
6Sophia2758000.0
7John3050000.0
8Sara3365000.0
9null2855000.0
10David4575000.0

CASE


In BigQuery, the CASE statement is used to perform conditional logic within SQL queries. It allows you to evaluate conditions and return different values based on those conditions.

For example, we can set threshold to evaluate the salary.

  • If the salary is above 65000, it is high level.
  • If the salary is between 55000 and 65000, it is middle level.
  • If it is under 55000, it is the low level.
  • The rest, we set it as others.
SELECT name, salary,
CASE
  WHEN salary >= 65000 THEN 'high'
  WHEN salary < 65000 and salary >= 55000 THEN 'middle'
  WHEN salary < 55000 THEN 'low'
  ELSE 'others'
  END AS salary_level
FROM `instruction-415216.tutorial.personnel` 
Rownamesalarysalary_level
1nullnullothers
2Bobnullothers
3Michaelnullothers
4Emma70000.0high
5Alice60000.0middle
6Sophia58000.0middle
7John50000.0low
8Sara65000.0high
9null55000.0middle
10David75000.0high

IF

The IF function is used to evaluate a condition and return one value if the condition is true, and another value if the condition is false.

For example, I want to create a new column called is_salary, to verify whether the salary data is not missing. If the data is not missing, then return true; otherwise, return false.

SELECT salary,
  IF(salary IS NOT NULL, TRUE, FALSE) AS is_salary
FROM
  tutorial.personnel;
Rowsalaryis_salary
1nullfalse
2nullfalse
3nullfalse
470000.0true
560000.0true
658000.0true
750000.0true
865000.0true
955000.0true
1075000.0true

IFNULL

The IFNULL function is used to return a specified value if the expression is NULL, otherwise, it returns the expression itself.

For example, I want to impute the missing value with 0 in salary column.

  • If the element is not null, then nothing changes;
  • If it is null, then fill it with value 0.
SELECT name, salary,
IFNULL(salary, 0) AS salary_imputed
FROM
  tutorial.personnel;
Rownamesalarysalary_imputed
1nullnull0.0
2Bobnull0.0
3Michaelnull0.0
4Emma70000.070000.0
5Alice60000.060000.0
6Sophia58000.058000.0
7John50000.050000.0
8Sara65000.065000.0
9null55000.055000.0
10David75000.075000.0

NULLIF

The NULLIF function is used to compare two expressions. If the expressions are equal, it returns NULL, otherwise, it returns the first expression.

SELECT NULLIF(5, 5) as result     # the two are identical, it returns NULL

/*--------*
 | result |
 +--------+
 | NULL   |
 *--------*/

SELECT NULLIF(5, 2) as result    # the two are not identical, returns the first exp

/*--------*
 | result |
 +--------+
 | 5      |
 *--------*/