---
title: "MongoDB Aggregation Pipeline vs SQL"
description: "MongoDB aggregation pipeline queries vs SQL queries, side by side. I came to MongoDB from SQL, so I map pipeline stages to SQL clauses you already know."
url: https://www.joekarlsson.com/blog/mongodb-aggregation-pipeline-queries-vs-sql-queries/
---

Jan 25, 2021 (Updated Nov 12, 2026 ) | 8 min read | [Web Dev](https://www.joekarlsson.com/blog/category/web-dev/) | [Written for MongoDB](https://www.joekarlsson.com/blog/written-for/mongodb/)

# MongoDB Aggregation Pipeline Queries vs SQL Queries

**Blog** MongoDB aggregation pipeline queries vs SQL queries, side by side. I came to MongoDB from SQL, so I map pipeline stages to SQL clauses you already know.

**Corrections**

-   **Nov 12, 2026:** The `$filter` condition used `'$item.plays'`; it’s `'$$item.plays'`. The MongoDB sample data didn’t match the SQL data.

**TL;DR** A side-by-side cheat sheet translating common SQL queries (GROUP BY, SUM, JOIN, etc.) into MongoDB Aggregation Pipeline stages. If you know SQL and are learning MongoDB, this is the Rosetta Stone you need.

On this page

1.  [What is the Aggregation Framework?](https://www.joekarlsson.com/blog/mongodb-aggregation-pipeline-queries-vs-sql-queries/#what-is-the-aggregation-framework)
2.  [Terminology and Concepts](https://www.joekarlsson.com/blog/mongodb-aggregation-pipeline-queries-vs-sql-queries/#terminology-and-concepts)
3.  [SQL Setup](https://www.joekarlsson.com/blog/mongodb-aggregation-pipeline-queries-vs-sql-queries/#sql-setup)
4.  [MongoDB Setup](https://www.joekarlsson.com/blog/mongodb-aggregation-pipeline-queries-vs-sql-queries/#mongodb-setup)
5.  [Quick Reference](https://www.joekarlsson.com/blog/mongodb-aggregation-pipeline-queries-vs-sql-queries/#quick-reference)
6.  [Next steps](https://www.joekarlsson.com/blog/mongodb-aggregation-pipeline-queries-vs-sql-queries/#next-steps)
7.  [Resources:](https://www.joekarlsson.com/blog/mongodb-aggregation-pipeline-queries-vs-sql-queries/#resources)
8.  [Want to check out more of my technical posts?](https://www.joekarlsson.com/blog/mongodb-aggregation-pipeline-queries-vs-sql-queries/#want-to-check-out-more-of-my-technical-posts)

**[1](https://www.joekarlsson.com/blog/mongodb-aggregation-pipeline-queries-vs-sql-queries/#mn-w-1)**

[From SQL to NoSQL: A Gentle Introduction For Devs](https://www.joekarlsson.com/talks/from-sql-to-nosql-a-gentle-introduction-for-devs/): From SQL to NoSQL: my talk on how SQL terms map to MongoDB documents, and the habits SQL developers have to unlearn.

Many devs coming to MongoDB are joining the community with a strong background in SQL. I would personally include myself in this subset [1](https://www.joekarlsson.com/blog/mongodb-aggregation-pipeline-queries-vs-sql-queries/#mn-1) of MongoDB devs. I think it’s useful to map terms and concepts you might be familiar with in SQL to help “[translate](https://docs.mongodb.com/manual/reference/sql-comparison/)” your work into MongoDB Query Language (MQL). More specifically, in this post, I will be walking through translating the MongoDB Aggregation Pipeline Queries vs SQL Queries.

## What is the Aggregation Framework?

The aggregation framework allows you to analyze your data in real-time. Using the framework, you can create an aggregation pipeline that consists of one or more stages. Each stage transforms the documents and passes the output to the next stage.

**[2](https://www.joekarlsson.com/blog/mongodb-aggregation-pipeline-queries-vs-sql-queries/#mn-w-2)** We should have some ways of connecting programs like garden hose--screw in another segment[Doug McIlroy, Bell Labs memo, 1964. He later got pipes added to Unix](https://web.archive.org/web/20201229210155/https://www.bell-labs.com/usr/dmr/www/mdmpipe.html)

If you’re familiar with the Unix pipe, you can think of the aggregation pipeline as a very similar concept [2](https://www.joekarlsson.com/blog/mongodb-aggregation-pipeline-queries-vs-sql-queries/#mn-2). Just as output from one command is passed as input to the next command when you use piping, output from one stage is passed as input to the next stage when you use the aggregation pipeline.

**[2](https://www.joekarlsson.com/blog/mongodb-aggregation-pipeline-queries-vs-sql-queries/#mn-w-2)** We should have some ways of connecting programs like garden hose--screw in another segment[Doug McIlroy, Bell Labs memo, 1964. He later got pipes added to Unix](https://web.archive.org/web/20201229210155/https://www.bell-labs.com/usr/dmr/www/mdmpipe.html)

SQL is a declarative language. You have to declare what you want to see-that’s why SELECT comes first. You have to think in sets, which can be difficult, especially for functional programmers. With MongoDB’s aggregation pipeline, you can have stages that reflect how you think-for example, “First, let’s group by X. Then, we’ll get the top 5 from every group. Then, we’ll arrange by price.” This is a difficult query to do in SQL, but much easier using the aggregation pipeline framework.

The aggregation framework has a variety of [stages](https://docs.mongodb.com/manual/reference/operator/aggregation-pipeline/) available for you to use. Today, we’ll discuss the basics of how to use [$match](https://docs.mongodb.com/manual/reference/operator/aggregation/match/), [$group](https://docs.mongodb.com/manual/reference/operator/aggregation/group/), [$sort](https://docs.mongodb.com/manual/reference/operator/aggregation/sort/), and [$limit](https://docs.mongodb.com/manual/reference/operator/aggregation/limit/). Note that the aggregation framework has many other powerful stages, including [$count](https://docs.mongodb.com/manual/reference/operator/aggregation/count/), [$geoNear](https://docs.mongodb.com/manual/reference/operator/aggregation/geoNear/), [$graphLookup](https://docs.mongodb.com/manual/reference/operator/aggregation/graphLookup/), [$project](https://docs.mongodb.com/manual/reference/operator/aggregation/project/), [$unwind](https://docs.mongodb.com/manual/reference/operator/aggregation/unwind/), and others.

> If you want to check out another great introduction to the MongoDB Aggregation Pipeline, be sure to check out Introduction to the MongoDB Aggregation Framework.

## Terminology and Concepts

The following table provides an overview of common SQL aggregation terms, functions, and concepts and the corresponding MongoDB aggregation operators:

##### Terminology and Concepts

| SQL Terms, Functions, and Concepts | MongoDB Aggregation Operators |
| --- | --- |
| WHERE | $match |
| GROUP BY | $group |
| HAVING | $match |
| SELECT | $project |
| LIMIT | $limit |
| OFFSET | $skip |
| ORDER BY | $sort |
| SUM() | $sum |
| COUNT() | $sum and $sortByCount |
| JOIN | $lookup |
| SELECT INTO NEW_TABLE | $out |
| MERGE INTO TABLE | $merge (Available starting in MongoDB 4.2) |
| UNION ALL | $unionWith (Available starting in MongoDB 4.4) |

Alright, now that we’ve covered the basics of MongoDB Aggregations, let’s jump into some examples.

## SQL Setup

The SQL examples assume _two_ tables, _album_ and _songs_, that join by the _song.album\_id_ and the _songs.id_ columns. Here’s what the tables look like:

##### Albums

| id | name | band_name | price | status |
| --- | --- | --- | --- | --- |
| 1 | lo-fi chill hop songs to study to | Silicon Infinite | 2.99 | A |
| 2 | Moon Rocks | Silicon Infinite | 1.99 | B |
| 3 | Flavour | Organical | 4.99 | A |

##### Songs

| id | title | plays | album_id |
| --- | --- | --- | --- |
| 1 | Snow Beats | 133 | 1 |
| 2 | Rolling By | 242 | 1 |
| 3 | Clouds | 3191 | 1 |
| 4 | But First Coffee | 562 | 3 |
| 5 | Autumn | 901 | 3 |
| 6 | Milk Toast | 118 | 2 |
| 7 | Purple Mic | 719 | 2 |
| 8 | One Note Dinner Party | 1242 | 2 |

I used a site called SQL Fiddle, and used PostgreSQL 9.6 for all of my examples. However, feel free to run these sample SQL snippets wherever you feel most comfortable. Here’s the code I used to set up and seed my tables with our sample data:

```sql
-- Creating the main albums table
CREATE TABLE IF NOT EXISTS albums (
    id BIGSERIAL NOT NULL UNIQUE PRIMARY KEY,
    name VARCHAR(40) NOT NULL UNIQUE,
    band_name VARCHAR(40) NOT NULL,
    price float8 NOT NULL,
    status VARCHAR(10) NOT NULL
);

-- Creating the songs table
CREATE TABLE IF NOT EXISTS songs (
    id SERIAL PRIMARY KEY NOT NULL,
    title VARCHAR(40) NOT NULL,
    plays integer NOT NULL,
    album_id BIGINT NOT NULL REFERENCES albums ON DELETE RESTRICT
);

INSERT INTO albums (name, band_name, price, status)
VALUES
    ('lo-fi chill hop songs to study to', 'Silicon Infinite', 7.99, 'A'),
    ('Moon Rocks', 'Silicon Infinite', 1.99, 'B'),
    ('Flavour', 'Organical', 4.99, 'A');

INSERT INTO songs (title, plays, album_id)
VALUES
    ('Snow Beats', 133, (SELECT id from albums WHERE name='lo-fi chill hop songs to study to')),
    ('Rolling By', 242, (SELECT id from albums WHERE name='lo-fi chill hop songs to study to')),
    ('Clouds', 3191, (SELECT id from albums WHERE name='lo-fi chill hop songs to study to')),
    ('But First Coffee', 562, (SELECT id from albums WHERE name='Flavour')),
    ('Autumn', 901, (SELECT id from albums WHERE name='Flavour')),
    ('Milk Toast', 118, (SELECT id from albums WHERE name='Moon Rocks')),
    ('Purple Mic', 719, (SELECT id from albums WHERE name='Moon Rocks')),
    ('One Note Dinner Party', 1242, (SELECT id from albums WHERE name='Moon Rocks'));
```

## MongoDB Setup

The MongoDB examples assume _one_ collection `albums` that contains documents with the following schema:

```json
{
	"name": "lo-fi chill hop songs to study to",
	"band_name": "Silicon Infinite",
	"price": 7.99,
	"status": "A",
	"songs": [
		{ "title": "Snow Beats", "plays": 133 },
		{ "title": "Rolling By", "plays": 242 },
		{ "title": "Clouds", "plays": 3191 }
	]
}
```

**[3](https://www.joekarlsson.com/blog/mongodb-aggregation-pipeline-queries-vs-sql-queries/#mn-w-3)**

[How To Use The MongoDB Visual Studio Code Plugin](https://www.joekarlsson.com/blog/how-to-use-the-mongodb-visual-studio-code-plugin/): Here it is: connecting to Atlas, browsing collections and running queries in a Playground.

For this post, I did all of my prototyping in a MongoDB Visual Studio Code plugin playground. For more information on how to use a MongoDB Playground in Visual Studio Code, be sure to check out this post: How To Use The MongoDB Visual Studio Code Plugin [3](https://www.joekarlsson.com/blog/mongodb-aggregation-pipeline-queries-vs-sql-queries/#mn-3). Once you have your playground all set up, you can use this snippet to set up and seed your collection. You can also follow along with this demo by using the [MongoDB Web Shell](https://docs.mongodb.com/manual/tutorial/getting-started).

```javascript
// Select the database to use.
use('mongodbVSCodePlaygroundDB');

// The drop() command destroys all data from a collection.
// Make sure you run it against the correct database and collection.
db.albums.drop();

// Insert a few documents into the albums collection.
db.albums.insertMany([
	{
		name: 'lo-fi chill hop songs to study to',
		band_name: 'Silicon Infinite',
		price: 7.99,
		status: 'A',
		songs: [
			{ title: 'Snow Beats', plays: 133 },
			{ title: 'Rolling By', plays: 242 },
			{ title: 'Clouds', plays: 3191 },
		],
	},
	{
		name: 'Moon Rocks',
		band_name: 'Silicon Infinite',
		price: 1.99,
		status: 'B',
		songs: [
			{ title: 'Milk Toast', plays: 118 },
			{ title: 'Purple Mic', plays: 719 },
			{ title: 'One Note Dinner Party', plays: 1242 },
		],
	},
	{
		name: 'Flavour',
		band_name: 'Organical',
		price: 4.99,
		status: 'A',
		songs: [
			{ title: 'But First Coffee', plays: 562 },
			{ title: 'Autumn', plays: 901 },
		],
	},
]);
```

## Quick Reference

### Count all records from albums

#### SQL

```sql
SELECT COUNT(*) AS count
FROM albums
```

#### MongoDB

```javascript
db.albums.aggregate([
	{
		$group: {
			_id: null, //  An _id value of null on the $group operator accumulates values for all the input documents as a whole.
			count: { $sum: 1 },
		},
	},
]);
```

### Sum the price field from albums

#### SQL

```sql
SELECT SUM(price) AS total
FROM albums
```

#### MongoDB

```javascript
db.albums.aggregate([
	{
		$group: {
			_id: null,
			total: { $sum: '$price' },
		},
	},
]);
```

### For each unique band\_name, sum the price field

#### SQL

```sql
SELECT band_name,
SUM(price) AS total
FROM albums
GROUP BY band_name
```

MongoDB

```javascript
db.albums.aggregate([
	{
		$group: {
			_id: '$band_name',
			total: { $sum: '$price' },
		},
	},
]);
```

### For each unique band\_name, sum the price field, results sorted by sum

#### SQL

```sql
SELECT band_name,
    SUM(price) AS total
FROM albums
GROUP BY band_name
ORDER BY total
```

MongoDB

```javascript
db.albums.aggregate([
	{
		$group: {
			_id: '$band_name',
			total: { $sum: '$price' },
		},
	},
	{ $sort: { total: 1 } },
]);
```

### For band\_name with multiple albums, return the band\_name and the corresponding album count

#### SQL

```sql
SELECT band_name,
    count(*)
FROM albums
GROUP BY band_name
HAVING count(*) > 1;
```

MongoDB

```javascript
db.albums.aggregate([
	{
		$group: {
			_id: '$band_name',
			count: { $sum: 1 },
		},
	},
	{ $match: { count: { $gt: 1 } } },
]);
```

### Sum the price of all albums with status A and group by unique band\_name

#### SQL

```sql
SELECT band_name,
    SUM(price) as total
FROM albums
WHERE status = 'A'
GROUP BY band_name
```

MongoDB

```javascript
db.albums.aggregate([
	{ $match: { status: 'A' } },
	{
		$group: {
			_id: '$band_name',
			total: { $sum: '$price' },
		},
	},
]);
```

### For each unique band\_name with status A, sum the price field and return only where the sum is greater than $5.00

#### SQL

```sql
SELECT band_name,
    SUM(price) as total
FROM albums
WHERE status = 'A'
GROUP BY band_name
HAVING SUM(price) > 5.00;
```

MongoDB

```javascript
db.albums.aggregate([
	{ $match: { status: 'A' } },
	{
		$group: {
			_id: '$band_name',
			total: { $sum: '$price' },
		},
	},
	{ $match: { total: { $gt: 5.0 } } },
]);
```

### For each unique band\_name, sum the corresponding song plays field associated with the albums

#### SQL

```sql
SELECT band_name,
    SUM(songs.plays) as total_plays
FROM albums,
    songs
WHERE songs.album_id = albums.id
GROUP BY band_name;
```

MongoDB

```javascript
db.albums.aggregate([
	{ $unwind: '$songs' },
	{
		$group: {
			_id: '$band_name',
			qty: { $sum: '$songs.plays' },
		},
	},
]);
```

### For each unique album, get the song from album with the most plays

#### SQL

```sql
SELECT name, title, plays
    FROM songs s1 INNER JOIN albums ON (album_id = albums.id)
WHERE plays=(SELECT MAX(s2.plays)
    FROM songs s2
WHERE s1.album_id = s2.album_id)
ORDER BY name;
```

MongoDB

```javascript
db.albums.aggregate([
	{
		$project: {
			name: 1,
			plays: {
				$filter: {
					input: '$songs',
					as: 'item',
					cond: { $eq: ['$$item.plays', { $max: '$songs.plays' }] },
				},
			},
		},
	},
]);
```

## Next steps

This post is in no way a complete overview of all the ways that MongoDB can be used like a SQL-based database. If you’re still early in your SQL-to-MongoDB journey, I’d also recommend checking out my [intro to moving from SQL to NoSQL](https://www.joekarlsson.com/talks/from-sql-to-nosql-a-gentle-introduction-for-devs/) for a broader overview of the differences. The aggregation framework has many other powerful stages, including [$count](https://docs.mongodb.com/manual/reference/operator/aggregation/count/), [$geoNear](https://docs.mongodb.com/manual/reference/operator/aggregation/geoNear/), [$graphLookup](https://docs.mongodb.com/manual/reference/operator/aggregation/graphLookup/), [$project](https://docs.mongodb.com/manual/reference/operator/aggregation/project/), [$unwind](https://docs.mongodb.com/manual/reference/operator/aggregation/unwind/), and others.

If you want to get better at using the MongoDB Aggregation Framework, be sure to check out [MongoDB University: M121 - The MongoDB Aggregation Framework](https://university.mongodb.com/courses/M121/about). Or, better yet, try to use some advanced MongoDB aggregation pipeline queries in your next project! If you have any questions, be sure to head over to the [MongoDB Community Forums](https://university.mongodb.com/courses/M121/about). It’s the best place to get your MongoDB questions answered.

## Resources:

-   [MongoDB University: M121 - The MongoDB Aggregation Framework](https://university.mongodb.com/courses/M121/about)
-   [Aggregation Pipeline Quick Reference](https://docs.mongodb.com/manual/meta/aggregation-quick-reference/)
-   [SQL to Aggregation Mapping Chart](https://docs.mongodb.com/manual/reference/sql-aggregation-comparison/)
-   [SQL to MongoDB Mapping Chart](https://docs.mongodb.com/manual/reference/sql-comparison/)
-   [MongoDB Community Forums](https://www.mongodb.com/community/forums/)

## Want to check out more of my technical posts?

-   [Building a Claude Code Blog Skill: What I Learned Systematizing Content Creation](https://www.joekarlsson.com/blog/building-a-claude-code-blog-skill-what-i-learned-systematizing-content-creation/) Oct 24, 2025**Building a Claude Code Blog Skill: What I Learned Systematizing Content Creation**
-   [How to Seed a MongoDB Database with Fake Data](https://www.joekarlsson.com/blog/how-to-seed-a-mongodb-database-with-fake-data/) Feb 19, 2021**How to Seed a MongoDB Database with Fake Data**
-   [I Replaced Alexa with a Dumber Voice Assistant (But at Least It’s Private)](https://www.joekarlsson.com/blog/i-replaced-my-smart-home-with-a-dumber-home-but-at-least-its-private/) Jun 18, 2025**I Replaced Alexa with a Dumber Voice Assistant (But at Least It’s Private)**
-   [Why Clickhouse Should Be Your Next Database](https://www.joekarlsson.com/blog/why-clickhouse-should-be-your-next-database/) Jul 6, 2023**Why ClickHouse Should Be Your Next Database**
-   [Data Warehouses Are Terrible Application Backends](https://www.joekarlsson.com/blog/data-warehouses-are-terrible-application-backends/) Jul 13, 2023**Data Warehouses Are Terrible Application Backends**

## Works on My Machine

Get the next post by email. My newsletter covers homelab builds, AI tools and open source, and it goes out when I have something to say.

It’s free, and you can unsubscribe anytime. You’ll get an email to confirm first.

![Joe Karlsson at his desk, chin on hand, with a microphone in front of him](https://www.joekarlsson.com/images/about/collage/studio.webp)

Joe Karlsson

Technology is better when it's weird and it's yours. Everything I write about runs in my house.

[More about me →](https://www.joekarlsson.com/about/)

[Bluesky](https://bsky.app/profile/joekarlsson.com) | [LinkedIn](https://www.linkedin.com/in/joekarlsson/) | [GitHub](https://github.com/JoeKarlsson)
