Simple SQL Queries: Filter Rows and Check What COUNT Measures

By Dave TanguayPublished Updated Editorial draft

Quick Answer

Start a SQL query by defining the rows you need, selecting only relevant columns and checking the result against a small known example. In SQLite, COUNT(*) counts matching rows, while COUNT(column) counts their non-NULL values in that column. Those totals can differ without either query being broken.

Define the table and the question

SQL retrieves information from relational data. The examples below use SQLite and an invented events table, not a website or customer database. The question is: how many inquiry rows occurred in October 2026, and how many have a recorded campaign value?

Use an authorized reporting source and appropriate access when applying these ideas to real data. Confirm the database dialect, column meanings and privacy requirements with its owner. The examples demonstrate queries, not permission to connect to a production system.

Know the five sample rows

Row 1
October 1, 2026; inquiry; campaign autumn.
Row 2
October 31, 2026; inquiry; campaign NULL, meaning no value is recorded.
Row 3
November 1, 2026; inquiry; campaign winter.
Row 4
October 15, 2026; page_view; campaign autumn.
Row 5
September 30, 2026; inquiry; campaign autumn.

The event_date field contains uniformly formatted date-only text such as 2026-10-01. This example does not perform time-zone conversion or demonstrate every timestamp storage format.

Select the matching rows first

SELECT event_id, event_date, campaign
FROM events
WHERE event_type = 'inquiry'
  AND event_date >= '2026-10-01'
  AND event_date < '2026-11-01'
ORDER BY event_id
LIMIT 10;

SELECT names the output columns; FROM identifies the table. WHERE filters by event type and the inclusive start/exclusive end of the month. ORDER BY makes this preview’s order explicit, and LIMIT bounds the displayed rows.

In the tested synthetic example, this returns rows 1 and 2. Row 3 is outside the month, row 4 is the wrong event type and row 5 precedes the start. SQLite’s SELECT documentation defines these clauses. A display limit alone is not a guarantee of low query cost on a large real dataset.

Count rows and populated values separately

SELECT COUNT(*) AS inquiries,
       COUNT(campaign) AS inquiries_with_campaign
FROM events
WHERE event_type = 'inquiry'
  AND event_date >= '2026-10-01'
  AND event_date < '2026-11-01';

The result is two inquiry rows and one inquiry with a non-NULL campaign. SQLite’s aggregate-function reference explains that difference. An empty string is also different from NULL and needs a rule suited to the actual data.

These two queries were checked against the five synthetic rows in an isolated in-memory SQLite database. The counts do not establish distinct people, qualified leads, campaign attribution or sales.

Keep the query with its definitions

Save the SQL, source, date-field meaning, filters and expected sample result with the report. If another table is later joined, verify the row relationships before trusting the same count. A one-to-many join can repeat a row’s contribution.

Our COUNTIF guide covers a separate spreadsheet calculation. The evidence-tools guide helps choose an appropriate source, and the traffic investigation guide keeps reporting scope attached to a website question.

Key Takeaways

  • Define and preview the rows before aggregating.
  • State date storage and reporting boundaries.
  • Distinguish row counts from non-NULL value counts.
  • Keep the query and a known sample with the report.

Frequently Asked Questions

Why can COUNT(*) differ from COUNT(campaign)?

The first counts rows; the second excludes NULL campaign values. Check whether empty strings or other missing-value conventions also occur.

Does this query count unique people?

No. It counts the matching event rows. Distinct-person reporting needs an appropriate identity definition and source.

Can I use this unchanged in every database?

Check the actual dialect, table structure and date types. This worked example was verified with SQLite and the stated synthetic data.

Archives