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/a-s-clark SQL Server Aug 14 '26
Sounds like your describing the semantic layers of a reporting tool like PowerBI or Looker as if its a shortcoming of the underlying SQL engine.
1 and 2 Are incompatible goals. If you're "late binding" tables, then any "strict type checking at the model level" is irrelevant. What table changes are you concerned about that would break your queries, but isnt necessary for consideration in your proposed approach? Reporting tools already build a typed semantic layer.
3. Clauses such as HAVING have very specific purposes in data filtering. Not clear why you think thats a problem, its functionality.
4. "Less footguns"? This is meaningless.
5. Again, you're describing a reporting tool
6. SQL Server already substitutes precomputed aggregates in some cases (not aure about other database systems, id imagine so), but aggregate navigation is also something for a reporting tool.
7. How many developers get hung up on cross-system compatibility compared to how many ever actually need that. If youre not using the specific functionality of your platform, you're tying your hands behind your back.
8. Refreshing stale caches is functionality for a reporting/ etl tool, not a SQL engine.
So, essentially, youre describing functionality of other tools, not SQL, and they already exist.