r/SQL • u/Prestigious_Bench_96 • Aug 14 '26
Discussion A better SQL for analytics?
Lots of attempts to dethrone SQL, lots of failures - I'm looking to add to the list with a proposed improved SQL (for analytics - please don't try this for OLTP workloads). Please take me down for my hubris.
What makes this attempt different? I want to lean into one of SQL's strengths - being declarative.
How to make it more declarative? No tables in queries.
Write this:
import baseball.batting;
WHERE SUM(hr) BY people.id > 500
SELECT
people.name_given,
lg_id,
SUM(hr) AS hr_count
ORDER BY
hr_count DESC;Instead of this:
WITH career_hr AS (
SELECT playerID
FROM read_csv('.../Batting.csv')
GROUP BY playerID
HAVING SUM(HR) > 500
)
SELECT
p.nameGiven,
b.lgID,
SUM(b.HR) AS hr_count
FROM read_csv('.../People.csv') p
JOIN read_csv('.../Batting.csv') b
ON p.playerID = b.playerID
JOIN career_hr c
ON b.playerID = c.playerID
GROUP BY p.nameGiven, b.lgID
ORDER BY hr_count DESC;It's just SQL, but with late-binding to physical tables through a (very lightweight) semantic layer.
This has a lot of nice properties - you can change your tables and refactor and no queries need to change; you can automatically resolve to aggregates if they exist and are equivalent; you can make the query syntax more flexible and composable because the lexical scope isn't constrained to a specific set of accessed tables. There's *lots* of other fun things you can do when the semantic layer has types, etc as well but this is already a longer pitch than I want!
A very brief example
pip install pytrilogy
trilogy init baseball duckdb; cd baseball;
trilogy ingest https://storage.googleapis.com/trilogy_public_models/duckdb/lahman/Batting.csv,https://storage.googleapis.com/trilogy_public_models/duckdb/lahman/People.csv,https://storage.googleapis.com/trilogy_public_models/duckdb/lahman/Teams.csv;
trilogy run 'where sum(hr) by people.id>500 select people.name_given, lg_id,sum(hr) as hr_count order by hr_count desc;' --import root.batting;Is this AI slop?
I've been working on ideas for the language for almost 6 years now so much of it predates AI, though it has evolved quite a bit in that time! Core discovery is all mostly hand-crafted; I do use AI to accelerate a lot of the tooling/interface work (a billion deepseek tokens (aka ~40 dollars, hilariously) on evaluating CI args, etc).
Read more/try
Website/docs: https://trilogydata.dev/
Github: https://github.com/trilogy-data/pytrilogy (open source, MIT)
I've seen this before
Posted 2 years ago here, floating around a few other places too:
https://www.reddit.com/r/SQL/comments/1e1h5mf/trilogy_simpler_data_warehouse_sql/
View all comments
Show parent comment
1
u/Prestigious_Bench_96 Sep 11 '26
Oh yes absolutely - I actually think 'better tools' is very much NOT agent instructions and MCP servers; that's patching it too late. To your example, a robust asset catalog with metadata + certification (or a semantic model capturing it) is the _better tool_ (and there's a whole bunch of fun work in making sure that is up to date and accurate). DuckDB is a better tool; etc.
And yes, the repeated/evolution over time is where a semantic layer starts to pay off - then you actually have a very low cost for repeating a question or updating your business metrics, it just doesn't demo as well.
So - do you need a semantic layer for text to sql? Nah. Do you need one for efficient, consistent, maintainable business reporting? Probably much more yes. Better tools help in both cases.
To bring it back to my original post, I'm pitching SQL-ish as the semantic layer - that unifies both cases, your initial text-to-sql *is* your semantic layer.