Intro to SQLfor Organizing
Subsection 5.5

ORDER BY + checkpoint

~5 min

Reading

ORDER BY sorts your results.

You can sort in ascending order or descending order. Ascending order is the default. You can write ASC for ascending or DESC for descending.

Example:

SELECT *
FROM `prj-training-ao7c.voterfiles.voterfile_snapshot`
WHERE vb_tsmart_sd = 'ORANGE'
   OR vb_tsmart_hd = 'BEN 4'
ORDER BY vb_voterbase_age ASC;

This query reads from the voter file snapshot. It filters for voters in a specific senate district or house district. Then it sorts by age from youngest to oldest.

You can also sort by multiple columns:

SELECT vb_tsmart_first_name, vb_tsmart_last_name, urbanicity
FROM `prj-training-ao7c.voterfiles.voterfile_snapshot`
ORDER BY urbanicity DESC, vb_tsmart_last_name ASC;

Sorting helps the headline row float to the top. For example:

  • Sort by count descending to find the top zip codes.
  • Sort by date descending to find the most recent records.
  • Sort by age ascending to find youngest records first.
  • Sort by missingness or status to prioritize cleanup.

Checkpoint

Add a filter and sort to your starter query.

Pattern:

SELECT [columns]
FROM `[project].[dataset].[table]`
WHERE [condition]
ORDER BY [column] [ASC or DESC]
LIMIT 100;

Then write one sentence:

This query narrows the table to [condition] and sorts the result by [column] so that [organizing purpose].

From the deck (slide 41):

"ORDER BY sorts results in an ascending or descending order. The default sort order is ascending, but you can specify ASC or DESC."

The deck's worked example

SELECT *
FROM `prj-training-ao7c.voterfiles.voterfile_snapshot`
WHERE vb_tsmart_sd = 'ORANGE'
   OR vb_tsmart_hd = 'BEN 4'
ORDER BY vb_voterbase_age ASC;

Rosario's plain-English read of this query:

"This query is asking the database to give us everything (that's what the * means) from voters in the voterfile_snapshot table. We're looking for voters who belong to the 'ORANGE' senate district OR are in the 'BEN 4' house district. After finding those voters, we want the list organized by age, starting with the youngest voter. ASC means we're sorting in ascending order : counting up from the lowest number."

Why this matters for campaigns

From Rosario's speaker note: "Imagine you're working on a political campaign focused on specific districts where your candidate has strong support. By running this query, you get a detailed list of potential supporters or undecided voters in these key districts. Sorting by age lets you tailor your outreach or messaging based on age demographics."

Multi-column sort

SELECT vb_tsmart_first_name, vb_tsmart_last_name, urbanicity
FROM `prj-training-ao7c.voterfiles.voterfile_snapshot`
ORDER BY urbanicity DESC, vb_tsmart_last_name ASC;

Sorts first by urbanicity (DESC), then alphabetically by last name within each urbanicity tier.

ORDER BY + LIMIT = "top N"

-- Top 10 names in Georgia in 2023 (public dataset)
SELECT name, number
FROM `bigquery-public-data.usa_names.usa_1910_current`
WHERE state = 'GA'
  AND year = 2023
ORDER BY number DESC
LIMIT 10;
Knowledge check

The NULL trap

You want to find contacts whose phone number is missing. Which clause should you use?

You earned the Filter Builder badge

You can write WHERE clauses with confidence, combine conditions with AND/OR/NOT, handle NULLs correctly, and sort with ORDER BY so the headline row floats to the top. In Module 6 you'll summarize, count, save, and submit your Organizer Query Brief.

ORDER BY decides the row order in your result. ASC is ascending (default), DESC is descending. Find the oldest voters in the file.

Next: Module 6 · Summarizing and Sharing: GROUP BY, COUNT, Saved Queries

Continue to Module 6