0Pricing
Digital Marketing Academy · Lesson

SELECT, WHERE, GROUP BY

Aggregate campaign data.

SELECT, WHERE, GROUP BY is a free Digital Marketing Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Digital Marketing Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Three Verbs

Almost every marketing metric is built from three clauses: SELECT chooses columns, WHERE filters rows, and GROUP BY collapses rows into segments.

Master this trio and you can answer most day-to-day analytics questions on your own.

SELECT channel, SUM(revenue) AS revenue
FROM orders
GROUP BY channel;

SELECT: Picking Columns

SELECT lists what you want back. You can rename columns with AS to make output readable, and compute new values inline.

Keep result sets lean: pull only the columns you actually need, especially on large tables.

SELECT campaign_id AS campaign,
       spend,
       spend * 1.2 AS spend_with_fees
FROM campaigns;

WHERE: Filtering Rows

WHERE keeps only rows that match a condition. Combine conditions with AND and OR to zero in on a segment.

Date filters are the marketer's bread and butter, defining the reporting window precisely.

SELECT *
FROM sessions
WHERE channel = 'email'
  AND session_date >= DATE '2026-01-01'
  AND device = 'mobile';

Filtering with IN and BETWEEN

IN matches any value in a list, perfect for several campaigns at once. BETWEEN captures a range, ideal for date windows.

These keep complex filters short and readable instead of long chains of OR.

SELECT order_id, revenue
FROM orders
WHERE channel IN ('paid_search', 'paid_social')
  AND order_date BETWEEN DATE '2026-03-01' AND DATE '2026-03-31';

GROUP BY: Segmenting

GROUP BY collapses rows that share a value into one row per group. Pair it with aggregates like SUM, COUNT, and AVG to get per-segment metrics.

This is how you turn raw events into a channel-by-channel performance table.

SELECT channel,
       COUNT(*) AS sessions,
       SUM(revenue) AS revenue
FROM sessions
GROUP BY channel;

Aggregate Functions

SUM totals revenue, COUNT tallies events, AVG finds the mean, and COUNT(DISTINCT ...) counts unique users.

Distinct counts matter in marketing: total sessions and unique visitors are very different numbers.

SELECT campaign_id,
       COUNT(*) AS clicks,
       COUNT(DISTINCT user_id) AS reach,
       AVG(revenue) AS avg_order
FROM orders
GROUP BY campaign_id;

Multi-Column Grouping

Group by more than one column to build a matrix, such as channel by device. Each unique combination becomes its own row.

This unlocks deeper segmentation without exporting anything to a spreadsheet.

SELECT channel, device,
       COUNT(*) AS sessions
FROM sessions
GROUP BY channel, device
ORDER BY channel, sessions DESC;

HAVING: Filtering Groups

WHERE filters rows before grouping; HAVING filters the groups after aggregation. Use HAVING to keep only segments above a threshold.

For example, show only campaigns that drove more than 100 orders.

SELECT campaign_id, COUNT(*) AS orders
FROM orders
GROUP BY campaign_id
HAVING COUNT(*) > 100
ORDER BY orders DESC;

Ordering and Limiting

ORDER BY sorts results so the most important rows surface first. LIMIT caps how many you see, ideal for top-N reports.

Together they answer "what are my top 5 campaigns by revenue?" instantly.

SELECT campaign_id, SUM(revenue) AS revenue
FROM orders
GROUP BY campaign_id
ORDER BY revenue DESC
LIMIT 5;

Conditional Aggregation

FILTER (or CASE inside an aggregate) lets one query compute several conditional metrics at once, like new vs returning revenue.

This avoids running multiple queries and keeps segments perfectly aligned.

SELECT channel,
       SUM(revenue) FILTER (WHERE is_new_customer) AS new_rev,
       SUM(revenue) FILTER (WHERE NOT is_new_customer) AS repeat_rev
FROM orders
GROUP BY channel;

Putting It Together

A full query layers all clauses: filter rows with WHERE, group, filter groups with HAVING, then sort and limit.

Read it top to bottom and it tells a complete analytical story about your channels.

SELECT channel, SUM(revenue) AS revenue
FROM orders
WHERE order_date >= CURRENT_DATE - INTERVAL '30 days'
GROUP BY channel
HAVING SUM(revenue) > 1000
ORDER BY revenue DESC;

Quick Check

You want only campaign groups with more than 50 orders. Which clause filters aggregated groups?

Recap

SELECT picks columns, WHERE filters rows, GROUP BY segments, HAVING filters groups, and ORDER BY plus LIMIT shape the output.

With conditional aggregation you compute several metrics in one pass. Next: combining data across multiple tables with JOINs.

Frequently asked questions

Is the “SELECT, WHERE, GROUP BY” lesson free?

Yes — the full text of “SELECT, WHERE, GROUP BY” is free to read here on the web, and the Digital Marketing Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Digital Marketing Academy course, upgrade to CoddyKit PRO.

What will I learn in “SELECT, WHERE, GROUP BY”?

Aggregate campaign data. You practise Digital Marketing Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Digital Marketing Academy?

No prior experience is required. Digital Marketing Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “SELECT, WHERE, GROUP BY” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Digital Marketing Academy lesson?

Yes. Every Digital Marketing Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Why Marketers Learn SQL
  2. SELECT, WHERE, GROUP BY
  3. Joining Marketing Tables
  4. Cohort and Funnel Queries
← Back to Digital Marketing Academy