Combining Multiple Conditions to Narrow Down The GA4 Data in BigQuery SQL

BigQuery With GA4

Combining Multiple Conditions to Narrow Down The GA4 Data in BigQuery SQL

In the previous lesson, we looked at the WHERE clause with one condition.

However, most analyses are not that simple. In most practical cases you will need to narrow down your result using multiple conditions.

Let’s take a look at how to do that.

Here is the SQL  that we used in the previous lesson

SELECT * FROM `optizent-test-project.analytics_219941990.events_20221204`

WHERE event_name = 'first_visit'

Now, continuing with this example, let’s say we only want to look at the rows that have the “first_visit” event and are from the United States. In that case, you will use the “WHERE” clause in your statement to filter the result set based on two conditions

  1. Event Name is first_visit

  2. The country where that event took place is the United States

In this case, you will use the AND operator to combine two conditions:

To know which columns to use for this condition you will need to know how structure of the GA4 data in BigQuery (check out, GA4 Event Schema in BigQuery).

So the modified SQL will be

SELECT * FROM `optizent-test-project.analytics_219941990.events_20221204`

WHERE event_name = 'first_visit'

AND geo.country = 'United States'

Here is the screenshot of the BigQuery console.

Now let’s change the scenario and say you want to look at those records where either the event name is first_visit or the country is the United States.

In this case, you will use the OR operator to combine these conditions

So the modified SQL will be

SELECT * FROM `Optizent-test-project.analytics_219941990.events_20221204`

WHERE event_name = 'first_visit'

OR geo.country = 'United States'

Here is the screenshot of the BigQuery console. As you can see now we have a lot more events than just first_visit as the result set includes all events by sessions from the United States.

Advanced Section:

Subscribe to Premium to read the rest.

Become a paying subscriber of Premium to get access to this post and other subscriber-only content.

Already a paying subscriber? Sign In.

A subscription gets you:

  • • Advanced Content
  • • Readymade SQL Scripts

Reply

or to participate.