Querying Your Data
Storing data is half the job โ the other half is *asking questions of it*: find the matching records, sort them, pick the fields you want. This is querying, and it's at the heart of every data app.
Data you can't ask questions of isn't much use. The other half of working with data is querying: finding the records that match some condition, sorting them, and picking out the parts you want - 'the active users, newest first, just their names'. Every data-driven feature (search, feeds, dashboards, reports) is querying underneath. This session covers the concepts, which are the same whether you're querying a list in memory or a giant database.
Filter, sort, select
Almost every query is a combination of three operations: filter (keep matching records), sort (order them), and select (choose which fields).
from larzquery import query
# active users, oldest first, just their names
query(users) \
.where(active=True) \
.order_by("age", desc=True) \
.select("name")The same idea, at any scale
These concepts are universal - a database query does exactly this, just faster and over far more data.
| In larzquery (a list) | In a database (SQL-ish) |
|---|---|
| where(active=True) | WHERE active = true |
| order_by('age', desc=True) | ORDER BY age DESC |
| limit(10) | LIMIT 10 |
| select('name') | SELECT name |
| count() / sum('x') | COUNT(*) / SUM(x) |
Learning to think in filter/sort/select over a list transfers directly to databases - the words differ (SQL uses WHERE/ORDER BY/SELECT) but the concepts are identical. So practising queries on an in-memory collection is genuinely learning how database querying works, which the next session builds on.
Ask questions of your data
- Querying is asking questions of your records - the other half of data work.
- Filter, sort, select - narrow, order, and pick fields - is the shape of most queries.
- Add limit and aggregate (count/sum/avg) to cover almost every real question.
- The concepts are universal - the same thinking powers database (SQL) queries.
๐งฉ Do it with LarzOS
Turn this lesson into action with the exact free tools and AI workflows for the job:
๐ฏ Key takeaways
- Querying - asking questions of your records - is the other half of working with data.
- Nearly every query is filter (keep matching), sort (order), and select (choose fields).
- Add limiting (first N) and aggregating (count/sum/average) to express almost any real data question.
- These concepts are universal - practising them on a list is learning how database (SQL) queries work.
Frequently asked
Is querying a list of dicts really the same as querying a database?
Conceptually, yes - the core operations and the way of thinking are the same - though databases add power, speed, and features that matter at scale. When you query a list of dicts with something like larzquery (filter with where, order with order_by, pick fields with select, limit results, aggregate with count/sum/average), you're performing exactly the operations that a database query performs; the mental model of 'narrow to matching records, order them, choose what to return' is identical. This is genuinely valuable, because it means practising queries on an in-memory collection teaches you the concepts that transfer directly to real databases - when you later write SQL (the language most databases use), you'll recognise that WHERE is your filter, ORDER BY is your sort, SELECT chooses fields, LIMIT limits, and COUNT/SUM/AVG aggregate - same ideas, different syntax. So learning to think in filter/sort/select over a list is a real head start on database querying, not a toy version. What databases add on top of these shared concepts is significant, though, and it's why you move to them at scale: speed on large data (databases use indexes - special lookup structures - to find matching records without scanning everything, so querying millions of rows stays fast, whereas filtering a huge in-memory list means checking every item); the ability to handle data too big to fit in memory (a database keeps data on disk and efficiently fetches what a query needs); querying across relationships between different kinds of data (joining users to their orders, which is central to real applications); safe concurrent access (many users querying and updating at once without corrupting data); and durability and integrity guarantees. Databases also have more sophisticated query capabilities (grouping, complex conditions, joins) expressed in SQL. So the honest picture is: the fundamental concepts of querying - filter, sort, select, limit, aggregate - are the same whether you're working with a list of dicts or a database, and learning them on a list is real preparation; but databases implement those concepts in a way that scales to huge data, handles relationships and concurrency, and stays fast through indexing, which is why real applications with significant data rely on them. Think of querying a list as learning the grammar of data questions, which you then speak fluently to databases (in SQL) when your data outgrows a list. The transferable skill is the thinking; the database is the powerful engine that runs that thinking at scale. Both matter, and starting with the concepts on a manageable list is a great way in.
What does it mean to 'aggregate' data, and when would I need it?
Aggregating means combining many records into a single summary value - answering questions about a group as a whole rather than listing individual records. Instead of 'show me all the orders', aggregation answers 'how many orders are there?' (count), 'what's the total revenue?' (sum), 'what's the average order value?' (average), 'what's the highest order?' (max), or 'the lowest?' (min). You're collapsing a collection down to a number (or a few numbers) that summarise it. You need aggregation constantly in real applications, because summaries are how people understand data at a glance. Think of any dashboard or report: 'you have 47 unread messages' (a count), 'total sales this month: $12,400' (a sum), 'average response time: 240ms' (an average), 'most expensive product: $89' (a max) - all aggregations. Whenever you want to know something about a group rather than see every member of it, that's aggregation: the number of users who signed up today, the total items in a cart, the average rating of a product, the highest score on a leaderboard. It's essential for analytics, reporting, dashboards, summaries, and any feature that presents 'the big picture' of a dataset. In querying tools, aggregation typically comes after filtering: you narrow to the records you care about, then aggregate them - 'how many active users?' is filter (active) then aggregate (count); 'total revenue from completed orders this month' is filter (completed, this month) then aggregate (sum of amounts). larzquery provides count, sum, avg, min, and max for exactly this, and databases provide the same via SQL's COUNT, SUM, AVG, MIN, MAX (often combined with grouping to aggregate per category - like sales per region). A related, powerful idea you'll meet later is grouping: aggregating within categories rather than over everything (average order value per customer, count of posts per user), which turns one summary into a summary-per-group. But the foundation is the basic aggregates: ways to reduce many records to a meaningful summary number. So you'll need aggregation the moment you want to report on, summarise, or understand your data in the aggregate rather than record by record - which is very often in real apps. It's how raw collections of records become the counts, totals, and averages that people actually want to see. Learning to filter-then-aggregate ('of the records matching this condition, how many / what total / what average?') is a core data skill that shows up in nearly every data-driven feature.
Related lessons
Storing and Loading Data
The practical heartbeat of a data app: save your records so they persist, and load them baโฆ
Working with DataDatabases & SQL
When files aren't enough, you reach for a database โ and the language for talking to most โฆ
Data Structures & AlgorithmsDictionaries & Sets: Instant Lookup
Dictionaries and sets are the secret weapons of fast code. Thanks to hashing, they find thโฆ