Get the latest tech news

Postgres feature you're not using – CTEs a.k.a. WITH clauses


SQL by default isn’t typically friendly to dive into, and especially so if you’re reading someone else’s already created queries. For some reason most people throw out principles we follow in other languages such as commenting and composability just for SQL. I was recently reminded of a key feature in Postgres that most don’t use by @timonk highlighting it in his AWS Re:Invent Redshift talk. The simple feature actually makes SQL both readable and composable, and even for my own queries capable of coming back to them months later and understanding them, where previously they would not be. The feature itself is known as CTEs or common table expressions, you may also here it referred to as WITH clauses. The general idea is that it allows you to create something somewhat equivilant to a view that only exists during that transaction. You can create multiple of these which then allow for clear building blocks and make it simple to follow what you’re doing.

SQL by default isn’t typically friendly to dive into, and especially so if you’re reading someone else’s already created queries. I was recently reminded of a key feature in Postgres that most don’t use by@timonk highlighting it in his AWS Re:Invent Redshift talk. The simple feature actually makes SQL both readable and composable, and even for my own queries capable of coming back to them months later and understanding them, where previously they would not be.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of Postgres

Postgres

Photo of clauses

clauses

Photo of CTEs a.k.a

CTEs a.k.a

Related news:

News photo

Just use Postgres

News photo

Postgres.new: In-browser Postgres with an AI interface

News photo

ClickHouse acquires PeerDB to expand its Postgres support