SQL is the language you use to query data in Google BigQuery. You do not need a computer science background to use it. The core operators are simple, and once you understand them, you can extract exactly the marketing data you need.
This guide covers the SQL fundamentals every marketer needs to work with data in BigQuery.
Think of SQL Like a Google Sheet
The easiest way to understand SQL is to compare it to a Google Sheet. A BigQuery table is like a spreadsheet: it has rows and columns. SQL is the set of instructions you give to filter, sort, group, and calculate on those rows and columns.
In a spreadsheet, you use filters and pivot tables. In BigQuery, you write SQL queries. The logic is the same. The syntax is different.
The Core SQL Operators for Marketers
You need five SQL operators to do most marketing analysis in BigQuery.
SELECT: chooses which columns to return. Think of it as choosing which columns to show in your spreadsheet.
FROM: specifies which table to query. This is the source of your data.
WHERE: filters rows based on a condition. Think of it as applying a filter in your spreadsheet.
GROUP BY: groups rows and aggregates metrics. Think of it as a pivot table.
ORDER BY: sorts the results. Think of it as sorting a column in your spreadsheet.
SELECT and FROM: Getting Your Data
The most basic SQL query selects all columns from a table:
SELECT * FROM your_dataset.your_table LIMIT 100
The asterisk (*) means “all columns.” LIMIT 100 returns only the first 100 rows, which keeps the query fast and the cost low while you explore your data.
To select specific columns instead of all of them:
SELECT date, campaign_name, spend, impressions, clicks
FROM your_dataset.meta_ads
LIMIT 100
This returns only the five columns you named, for the first 100 rows of your Meta Ads table.
WHERE: Filtering Your Data
WHERE filters rows based on a condition. You use it to narrow your results to a specific time period, campaign, or account.
Filter by date range:
SELECT date, campaign_name, spend
FROM your_dataset.meta_ads
WHERE date BETWEEN ‘2026-01-01’ AND ‘2026-01-31’
Filter by campaign name:
SELECT date, campaign_name, spend
FROM your_dataset.meta_ads
WHERE campaign_name = ‘Brand Awareness Q1’
Filter by multiple conditions using AND:
SELECT date, campaign_name, spend
FROM your_dataset.meta_ads
WHERE date BETWEEN ‘2026-01-01’ AND ‘2026-01-31’
AND spend > 100
This returns only rows from January 2026 where spend was greater than $100.
GROUP BY: Aggregating Your Data
GROUP BY groups rows together and lets you calculate totals, averages, and counts. This is the SQL equivalent of a pivot table.
Total spend per campaign:
SELECT campaign_name, SUM(spend) AS total_spend
FROM your_dataset.meta_ads
WHERE date BETWEEN ‘2026-01-01’ AND ‘2026-01-31’
GROUP BY campaign_name
ORDER BY total_spend DESC
This returns one row per campaign, with the total spend for January 2026, sorted from highest to lowest spend.
Common aggregation functions:
- SUM: adds up all values in a column.
- AVG: calculates the average.
- COUNT: counts the number of rows.
- MAX: returns the highest value.
- MIN: returns the lowest value.
Calculating Custom Metrics
SQL lets you calculate metrics that do not exist as columns in your table. You define the formula directly in your SELECT statement.
Cost per click (CPC):
SELECT
campaign_name,
SUM(spend) AS total_spend,
SUM(clicks) AS total_clicks,
SUM(spend) / SUM(clicks) AS cpc
FROM your_dataset.google_ads
GROUP BY campaign_name
Click-through rate (CTR):
SELECT
campaign_name,
SUM(clicks) / SUM(impressions) AS ctr
FROM your_dataset.meta_ads
GROUP BY campaign_name
ROAS (return on ad spend):
SELECT
campaign_name,
SUM(conversion_value) / SUM(spend) AS roas
FROM your_dataset.meta_ads
GROUP BY campaign_name
ORDER BY: Sorting Your Results
ORDER BY sorts your query results by a column. Add DESC for descending order (highest first) or ASC for ascending order (lowest first).
Top 10 campaigns by spend:
SELECT campaign_name, SUM(spend) AS total_spend
FROM your_dataset.meta_ads
GROUP BY campaign_name
ORDER BY total_spend DESC
LIMIT 10
This returns the 10 campaigns with the highest total spend, sorted from highest to lowest.
Starting With SQL in BigQuery
The best way to learn SQL for marketing is to start with a real table. Load your Meta Ads or Google Ads data into BigQuery using Porter Metrics, then open BigQuery Studio and start with simple SELECT queries. Add WHERE filters. Add GROUP BY to aggregate. Add ORDER BY to sort.
Each query teaches you something. Within a few hours of practice with real marketing data, you have the SQL skills to answer most of the questions your team asks every day.
Ready to connect your marketing data to BigQuery?
Porter Metrics makes it easy to sync all your sources — no code required.