SQL Builders: Advanced Selects

Episode #317 • Mar 17, 2025 • Subscriber-Only

We now have a type-safe syntax for generating SELECT statements using key paths to the columns we want to select, but while this syntax is nice and what many existing libraries use, we can do better. Let’s introduce a more advanced syntax that leverages variadic generics and supports more complex query expressions.

Collection
SQL Building
SQL Builders: Advanced Selects
Locked

Unlock This Episode

Our Free plan includes 1 subscriber-only episode of your choice, plus weekly updates from our newsletter.

Sign in with GitHub

Introduction

Brandon

We now have a statically checked, type-safe way to select columns from a table in Swift. We simply use the select static method on our table type, then provide a variadic list of key paths of the columns we want to select, and we will magically get a valid SQL statement written for us that can query the database.

Stephen

However, we can make things so, so much better. While the form of select we have built so far looks nice, and it’s how most SQL libraries out there handle things, but it unfortunately makes a lot of SQL functionality impossible to express. It turns out that SELECT statements in SQL have a lot more power than we have demonstrated so far.

Let’s see just how complex SELECTs can be, then see why our key path syntax is not capable of expressing these more complex queries, and then see what we can do to support them.

Advanced selects


References

Downloads

Get started with our free plan

Our free plan includes 1 subscriber-only episode of your choice, access to 71 free episodes with transcripts and code samples, and weekly updates from our newsletter.

View plans and pricing