---
title: Two new MySQL expressions
url: "https://aaronfrancis.com/videos/two-new-mysql-expressions"
path: "/videos/two-new-mysql-expressions"
type: video
author: Aaron Francis
published: 2023-12-16
topic: Databases
description: "MySQL 8.0.31 added INTERSECT and EXCEPT. I use a customers table and a staff table to walk through unions, intersections, and one table without the other, plus why you should use UNION ALL when you don't need duplicates removed."
keywords: [mysql, intersect, except, union, union all, sql queries, duplicate removal, mysql 8.0.31]
youtube_url: "https://www.youtube.com/watch?v=NZqFiBDQG1s"
site_index: "/llms.txt"
keyword_index: "/llms/keywords.txt"
---

# Two new MySQL expressions

MySQL 8.0.31 added INTERSECT and EXCEPT. I use a customers table and a staff table to walk through unions, intersections, and one table without the other, plus why you should use UNION ALL when you don't need duplicates removed.

## Transcript

**Aaron Francis:** Of all of the videos on YouTube, you clicked on mine about set theory in MySQL? That makes me so happy. I can't express it enough. I'm gonna show you two tables. I'm gonna show you a customers table and a staff table, and we're gonna do a lot of things with these tables. The first thing we're gonna do is look at the union between these two tables. Then we will look at the intersection between the two tables, and then we'll look at one table without any of the matches from the other table, and then we will look at a, a kind of a weird case of the union between the

**Aaron Francis:** two tables without the intersection. Throughout this video, we're gonna be relying on two new table expressions. That's the INTERSECT expression and the EXCEPT expression, and those are both new in MySQL 8.0.31, which came out in November of 2022. So if you are following along, make sure you have an up-to-date version of MySQL. Let's go.

**Aaron Francis:** We're gonna start with the most basic thing possible, which is SELECT * FROM staff UNION SELECT * FROM customer. That's gonna take both tables and stack them on top of each other, and we see something weird already. First name, Mike, John, Aaron, one, one, two, two, two. That's because these columns don't line up. The table structures are different. And so usually you wanna be explicit here.

**Aaron Francis:** So we're gonna say first_name, last_name, email, and we'll just actually copy that down because that exists here. And now that looks a lot better, right? So we have Mike, John, Aaron, Mary, Patricia, Linda, Barbara, Elizabeth, Jennifer, Maria, and Susan. Great. We've got all the first names lined up, all the emails lined up. Now, if we go back here and just read the staff, you see we have three staff members.

**Aaron Francis:** And if we limit the customers to 10, and we'll go ahead and just limit the staff to 10 anyway so it's all nice and symmetric, and we read this back, we see we get 12, we get 12 rows. We don't get 13 rows. There are 10 customers, so if we do that, we get 10 customers there. And if we do that, we get three rows there.

**Aaron Francis:** So we have three staff members and 10 customers and 12 rows. So what is going on here? By default, the UNION operator is going to go through and weed out any duplicates. This may be what you're looking for. It may be the thing that you need. It is the default, and it is more expensive. So if you know that you don't need MySQL to do the distincting of the result set, you should add the ALL keyword.

**Aaron Francis:** If we run that again, you'll see we now have 13 rows, and the duplicate was me. I'm a member of staff and a happy customer, and you should be, too. PlanetScale, PlanetScale.com to, to be a customer. PlanetScale.com/jobs. We are hiring. So you believe me because I've shown you that there are 13 rows, but there's another way that I can prove this to you.

**Aaron Francis:** If we run EXPLAIN ANALYZE and we take a look at this. Now, brace yourself. This is going to be a lot. I will have a full video on this in the future, but get ready. Here is the output of EXPLAIN ANALYZE. Lot of stuff going on here. We'll start at the very bottom, kinda work our way back up. The first thing it does, table scan on customer, limits it down to 10 rows.

**Aaron Francis:** Table scan on staff, limits it down to 10 rows. That is running both of those queries and limiting the results to 10 rows. This is where we can get in trouble. Union materialize with deduplication. So this is where MySQL has to go through and compare every single row to the other s- to the other set and figure out, are any of these duplicates?

**Aaron Francis:** And if they are, let's get rid of them. And then finally, it does a table scan on the temporary table that was created out of that. Boy, that is a lot. If we run this again with the UNION ALL, you'll see that it looks the same, but it's very different. Scan on customer, limit, and then it just gives the results back. Scan on customer, limit, and then it just gives the results back into an append.

**Aaron Francis:** There's no deduplication. There's no scanning a UNION's temporary table at the end. If you know that there are no duplicates, if you know that logically the way that you've set up your queries, there cannot be duplicates, put the ALL keyword on there. It's gonna be faster. If you don't care if there are duplicates, put the ALL keyword on there. It's going to be faster.

**Aaron Francis:** You don't wanna do duplicate weed out if you don't care about duplicates. Let's move on to intersections. This is where we're gonna take both of the tables and figure out what they have in common. What is the overlap? Let's go ahead and drop these limit 10s, and we'll see instead of UNION ALL, we just type the word INTERSECT. That's it. That's it. I'm the only staff member in the customers table.

**Aaron Francis:** I'm the only customer in the staff members table. One of the good things about the INTERSECT operator or keyword is it doesn't matter what order you put them in. It doesn't matter what order you put them in. An intersection is still an intersection. That's all there is to it. Now let's talk about taking the results from an entire query except for the results from a secondary query.

**Aaron Francis:** In this case, the order does really matter, so let's put it back the way that it was, 'cause that'll be easier to demonstrate off the bat. So instead of saying INTERSECT, we can say EXCEPT, and if we do that, we see all of the staff members who are not customers. We only get the two rows back, 'cause remember, there are only three rows, of which I am one. But if we say, "Show me the staff without the customers," we get two rows.

**Aaron Francis:** Now, fundamentally different is, "Show me the customers without the staff." We get 598 rows back. So in this case, it is extremely important that you pay attention to the ordering, because if you're saying customers without staff, that's pretty different than staff without customers. This last one we're gonna do just 'cause I think it's kinda fun.

**Aaron Francis:** We're gonna find all of the staff and all of the customers, put them together, but without the overlap of people who are staff and customers, i.e., without me. Sad. All right. There are a couple totally valid ways that we could do this. We're just gonna do this in whatever way pops into my head right now. Let's start by getting the universe of what we're looking for, the entire union of everything, 601 rows.

**Aaron Francis:** Now, when we finish, there should be 600 because I am the only intersection between the two. If we did a UNION ALL right now, we would see 602 because I am in there twice, and if we do the UNION, we see 601. We should end at 600 with no intersection whatsoever. So the way that I wanna do this is I wanna take the entire universe of possibilities, and then I wanna say EXCEPT the intersection between these two tables.

**Aaron Francis:** So EXCEPT the intersection. Let's make this look a little bit less wild. EXCEPT the intersection gives us 600 rows. I think we're there. One good thing we can do is we actually don't care about duplicates because we don't need to weed them out. In fact, we are going to exclude them with our EXCEPT, and so we still have our 600 rows.

**Aaron Francis:** I find this really fun. I find it a little bit academic, and I always enjoy that 'cause I don't, I don't have a computer science degree. I love learning about this stuff, and hopefully you do too. Hopefully, the two new table expressions, the INTERSECT and the EXCEPT expressions, you'll find useful in your work. Please subscribe to this channel. We have tons of MySQL videos all the time.

**Aaron Francis:** Please leave a comment and let me know what you wanna see next. See ya.

---

## Related on aaronfrancis.com

- [About](/about) *(Page · 2026-11-25 · shared: mysql)*
- [Do The Hardest Thing with Jesse Hanley](/podcast/do-the-hardest-thing-with-jesse-hanley-9b42d310) *(Podcast · 2026-05-26 · shared: mysql)*
- [Database School is live, and AI got really good](/newsletter/database-school-is-live-and-ai-got-really-good) *(Newsletter · 2025-12-05 · shared: mysql)*
- [Take things seriously](/newsletter/take-things-seriously) *(Newsletter · 2025-11-21 · shared: mysql)*
- [Bury The Hatchet](/podcast/bury-the-hatchet-8ee6484d) *(Podcast · 2025-09-23 · shared: mysql)*

## Relevant Resources from Aaron Francis

- **[MySQL for Developers](https://databaseschool.com)** (overview on site: [/courses](/courses)): Full video course covering MySQL indexing, deferred joins, pagination, query plans, and schema design for application developers.
- **[High Performance SQLite](https://databaseschool.com)** (overview on site: [/courses](/courses)): Comprehensive video course on running SQLite in production: WAL mode, concurrency, FTS5, strict tables, backups, and performance tuning.

## Search aaronfrancis.com (2-Hop Static Keyword Index)

Pick the alphabetical shard matching any topic to find exact pages across all articles, newsletters, videos, talks, and podcast transcripts (every shard is < 40 KB):
[A-B](/llms/keywords/a-b.txt) · [C](/llms/keywords/c.txt) · [D-E](/llms/keywords/d-e.txt) · [F-G](/llms/keywords/f-g.txt) · [H-J](/llms/keywords/h-j.txt) · [K-L](/llms/keywords/k-l.txt) · [M-N](/llms/keywords/m-n.txt) · [O-P](/llms/keywords/o-p.txt) · [Q-R](/llms/keywords/q-r.txt) · [S](/llms/keywords/s.txt) · [T-Z](/llms/keywords/t-z.txt) · [Directory](/llms/keywords.txt) · [Site Map](/llms.txt)

Shards for this page's topics: `mysql` ([M-N](/llms/keywords/m-n.txt)), `intersect` ([H-J](/llms/keywords/h-j.txt)), `except` ([D-E](/llms/keywords/d-e.txt)), `union` ([T-Z](/llms/keywords/t-z.txt)).

## About Aaron Francis (Current Context)

Aaron Francis is VP of Marketing and Community at [Laravel](https://laravel.com), founder of [Try Hard Studios](https://tryhardstudios.com), creator of [Solo](https://soloterm.com), co-host of [Mostly Technical](/podcast), and author of [Mastering Postgres](/courses), [High Performance SQLite](/courses), [MySQL for Developers](/courses), and [Screencasting.com](/courses). Full bio: [/about](/about) · Current focus: [/now](/now).
