sql ctes subquery

Why I use SQL CTEs

How do CTEs work?

In the example below I’ve used CTEs to:

  • get all the data I need from the ‘Email Sent’ table, told SQL to hold that in memory,
  • then get what I need from the ‘Unsubscribe’ table, told SQL to hold that in memory too,
  • then joined them together with flags from the ‘Customers’ table.

Where you see a WITH, is the CTE starting and then I’m naming them ‘sent’ and ‘unsubs’ before starting to tell the CTE what I want to return:

When I do the final ‘join everything together’ part I’m joining fields from the ‘sent’ dataset such as ‘sent.email’.

Why not just use a Subquery?

The way I get my head around reading a subquery is by reading from the inside out. It’s nesting everything you need together, but in my opinion, it tends to get ugly really quickly.

  • The first step is to run the query in the centre starting ‘select accountID …’ to get all orders greater than 30 from the ‘OrderHistory’ table.
  • Then join on the ‘account’ table to look up which accounts were from New Zealand.
  • Then the top select runs to return all the fields from the ord dataset and the three columns I want to see from the account table.

Why are they so great?

You can use them multiple times throughout your script and they are readable, you can return what you need then reference it later.

Why not just create a table?

If you don’t have write permissions this may not be possible. And if it’s only used for this query your DBA might not be thrilled with you creating one-off tables.

Are there any negatives?

CTEs don’t last forever and can only be used in the query you’re currently in, unlike temp tables or views that can survive outside the current script.

What about performance?

The database will always decide the best way to execute your query. If you ask your friendly DBA which strategy to use, they can fill you in on which strategy works best for your database. CTEs are all about readability, so if it works for you give it a try.

Photo by Adrianna Calvo from Pexels

Comments are closed, but trackbacks and pingbacks are open.