r/dataengineering • u/Consistent-Zebra3227 • Dec 19 '25
Discussion In SQL coding rounds, how to optimise between readibility and efficiency when working with CTEs?
Any hard problem can be solved with enough CTEs. But the best solutions that an expert can give would always involve 1-2 CTEs less ( questions like islands and gaps, sessionization etc.)
So what's the general rule of thumb or rationale?
Efficiency as in lesser CTEs make you seem smarter in these rounds and the code looks cleaner as it is lesser lines of code
32
Upvotes
3
u/SoggyGrayDuck Dec 19 '25
Reuse, I tend to do a lot of research instead of working from specs and this has killed me in two interviews I basically had nailed. It's like my strength became my weakness
94
u/ZirePhiinix Dec 19 '25
CTE isn't for optimizing efficiency. That's just not what it is designed to do.
78
u/Scuba-Steven Dec 19 '25
Readability/maintainability is almost always more important unless you’re performing tons and tons of operations. A super fancy join that shortens your script and gives you 3 seconds better performance isn’t valuable if the next person that has to work on it can’t figure out what it’s doing
17
u/mike-manley Dec 19 '25
Why is "1 or 2" a limit? The real limit is if the code can answer the question, and do so as efficiently as possible.
For CTEs, I tend to label them as such with a "_cte" suffix for readability.
5
u/Ok_Basil5289 Dec 19 '25
What i learnt is that unless the result set is huge, which can be handled by temp table, otherwise prioritise readability over efficiency. Code is a form of communication between developers. Some optimisation like query compilation can also be applied for efficiency.
3
u/crytek2025 Dec 19 '25
Well, at the EoD you are writing code for other devs to understand. Easy to understand over minimal performance gains
3
u/[deleted] Dec 19 '25 edited Dec 19 '25
Sometimes ctes are actually more efficient though . . . in some circumstances
For example, when trying to take the first items of a set according to multi-valued ordering, you can use row number in a cte and use where rn =1.
Generally, ctes and subqueries are equally efficient, but every once in a while you can performance tune a query with a cte rather than a subquery. Depends on the indexing, dbos and database though, so you gotta check the query plan.
2
u/SQLDevDBA Dec 19 '25
Depends on the platform.
In Oracle and Postgres, for example, CTEs can be materialized and that does improve things. In SQL server, not so much.
Erik Darling has a great series on CTEs and their misconceptions.
Literally everything you know about CTEs is wrong.
How to write SQL Correctly: CTEs
A little about CTEs
2
u/SQLofFortune Dec 19 '25
Depends on the interviewer unfortunately it’s a roll of the dice. You can ask what they prefer but they usually don’t tell me. They just say ‘build it how you normally would’ which is stupid for various reasons.
2
u/DenselyRanked Dec 19 '25
Writing more concise SQL comes with experience, but ultimately the primary thing that matters is how the optimizer interprets it, and if it is readable.
It's likely that the "expert" solutions involved pre-planning, and a rushed solution will be more verbose.
1
u/sdrawkcabineter Dec 19 '25
This whole sub is plagued with CTE.
1
u/DrTrainwreck Dec 19 '25
Solve the problem/question, then discuss ways you could further optimize for readability/performance etc.
1
u/sloth_king_617 Dec 19 '25
I’ve been interviewing rather heavily over the past month with a handful of live sql coding rounds.
IMO, it’s more important to be able to explain the trade offs between writing queries one way or another. When coding live, I think it’s best to get to a solution ASAP, but also talk about alternative solutions. This can show that you have breadth and depth of knowledge.
I personally prefer readability for any queries being committed (to a repo) and that typically means more CTEs with clear names. For ad hoc reporting, go ahead and throw things together and get it done. I would worry about efficiency when it becomes a problem like if cost becomes an issue or run time gets out of hand.
1
u/PurepointDog Dec 19 '25
This is part of my issue with SQL - stuff like readability vs performance is often a tradeoff.
Tools like Polars let you do both. That's why I advocate so strongly for them.
1
u/IDoCodingStuffs Software Engineer Dec 21 '25
Says who?
-7
u/ResidentTicket1273 Dec 19 '25
Meh, CTEs are nice syntactic sugar for sub-queries, tbh I don't see what all the fuss is about. If you label your sub-queries neatly enough, you shouldn't really need them - that being said, I suppose defining a set of source objects up-front is fairly good practice, but it all gets munged by the parser/optimiser before execution, so optimising for efficiency can only ever be done by looking at the explain-plan - different optimisers will interpret the same sql in totally different ways.
At the end of the day, SQL is there to be read by humans, so go for readability first. The rest can come later.
0
u/dev_lvl80 Principal Data Engineer Dec 20 '25 edited Dec 25 '25
First, depends on database vendor, some can use persisting data from cte if it refers 2+ times. You should clarify this at first question to interviewer.
General rule - CTE is syntax sugar, as thus should not be used as "optimization" technique. That's what you need to know about CTEs and not overthinkint them.