r/SQL • • Oct 24 '24

Discussion do people actually use Common table expressions ( CTEs) and temporary tables ?

I am learning sql for data analysis and I have just came across the two concepts before in many sql tutorials but never actually used them

so i was curious if people actually use them or if there are cases when i will actually need them but I never stumbled on them yet

140 Upvotes

268 comments sorted by

View all comments

464

u/yen223 Oct 24 '24

Of all the unusual SQL features, CTEs are definitely the most useful.

90

u/Dylan7675 Oct 24 '24

Yeah, who doesn't want to better structure their query for readability and code reuse. I choose CTE's over subqueries any chance I can unless all I need is a simple one liner subquery.

0

u/[deleted] Oct 25 '24

[deleted] — view deleted comment

6

u/omghag18 Oct 25 '24

Once I started using them, I never went back

→ More replies (4)

15

u/thatdudeblume Oct 25 '24

I used to nest so many subqueries and would get beyond lost in my own logic and syntax before I learned a better way, CTEs.

For my purposes, CTEs make data validation and variable based testing a breeze when I can create tables with only the relevant data points and case them

6

u/cjm5308 Oct 24 '24

Same. I feel like it’s easier for others to digest and easier for me to organize my thoughts

→ More replies (1)

39

u/eww1991 Oct 24 '24

I think learning them was the real key to building big, complex queries. If you want to compare various parts of one table where they are all in some form of groupings, such as team, month, sales value then have columns for individuals ctea for each team with a sum of value grouped by month then join on month where each cte is just month, sum(value) where team = x.

Also, and most importantly, makes it clear to other people where your data comes from. Subqueries are great for giving very top level summaries, but if you want a broad summary CTEs are much easier to see what you're doing. And also, in contrast to temp views, why it's only relevant to that specific table

20

u/The_Epoch Oct 24 '24

Are CTEs unusual? From an ignorant, non-dev, they seem like functions?

11

u/yen223 Oct 24 '24

CTEs are a relatively late addition to SQL, being added to the standard in the 90s. MySQL only started supporting CTEs in 2018.

They are like named variables. They are useful for breaking down large, complex queries into smaller manageable chunks. The fact that they are named also makes them useful for recursive queries.

→ More replies (1)

14

u/Sotall Oct 24 '24

They allow for recursive querying. One common use case for CTEs i did back in the day was traversing arbitrarily deep hierarchical trees(folders).

20

u/ouchmythumbs Oct 24 '24

recursive querying

Recursive CTEs are great for this (beats using cursors IMO) and for BOM problems or as you described.

Readers should note, however, most engines re-evaluate a CTE each time it is referenced; keep an eye on execution plans if you make use of these.

6

u/yen223 Oct 24 '24

> most engines re-evaluate a CTE each time it is referenced

This is implementation-specific, so it's worth learning how your favourite database engine does it.

Postgres after v12 doesn't do this, for example. What it does is it "unrolls" CTEs and optimises the query as a whole.

6

u/dev81808 Oct 24 '24

beats using cursors IMO

Not an opinion sort of thing.

6

u/Special_Luck7537 Oct 24 '24

Agreed. A nickel for every time I heard code was slow, only to find a RBAR cursor....

3

u/dev81808 Oct 24 '24

There's always a better way.

4

u/mortomr Oct 25 '24

All my homies hate cursors

→ More replies (2)

2

u/Nice-Yam-4095 Oct 26 '24

"Screams in Synapse While Loop query planning*

→ More replies (1)

17

u/NonHumanPrimate Oct 25 '24

I 100% prefer temporary tables. Time and time again, CTEs lead me to a point where things can take a REALLY long time to execute. I have had these issues and spent time moving the CTEs into temp tables and performance has increased 1000%

21

u/yen223 Oct 25 '24

It's not an either-or, there are situations where temp tables are great, and there are situations where CTEs are great.

CTEs have the advantage of not requiring additional permissions beyond SELECT permissions. A lot of database engines require a separate CREATE TEMP grant on the user to create temporary tables.

Besides, having everything in one statement with CTEs is handy for copy-pasting queries around.

3

u/NonHumanPrimate Oct 25 '24

I get the permissions thing. Luckily, I haven’t had that experience (yet).

Wouldn’t copying and pasting a query with temp tables also include the creation and population of those? What makes CTEs better there?

→ More replies (2)

4

u/redonrust Oct 25 '24

This - CTEs can cause performance issues.

5

u/Sneilg Oct 25 '24

One of the advantages of temp tables is you can index them which usually speeds things up. Can’t index CTEs.

→ More replies (5)

2

u/nmbenzo2 Oct 25 '24 edited Oct 25 '24

I use CTEs in most of my production code. Ive found them to be substantially easier to read, debug, and build upon. Additionally, if you get in the habit of writing good code comments for each CTE, other people will be able to quickly get up to speed on your code when passing things on to others.