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/
6
u/[deleted] Aug 14 '26
Honestly SQL is still hard to beat for analytics. what specifically is frustrating you?
if it's the verbosity, window functions and CTEs go a long way toward making queries readable. if it's the ecosystem, dialects like DuckDB or BigQuery have added a ton of analytical functions that make common patterns way less painful.
the "better SQL" alternatives tend to fall into two camps. visual query builders that break down on anything complex, or new languages that never get enough adoption for good tooling. both usually end up generating SQL under the hood anyway.
the real wins come from better abstractions on top of SQL, not replacing it. dbt for transformation logic. materialized views for precomputing expensive joins. proper indexing and partitioning so queries actually finish.
what's the actual pain point? that'd help narrow things down.
12
u/jshine13371 Aug 14 '26
Why do so many people spend countless hours trying to reinvent the wheel in this industry? 😐
2
u/soxinthebox Aug 14 '26
Is it worth building a variation on SQL for the edge cases where you need to be a bit more verbose to achieve a query?
2
u/a-s-clark SQL Server Aug 14 '26
What is this solving for you that SQL and other existing reporting tools don't?
1
u/Thadrea Data Science Manager Aug 14 '26
Unless I'm missing something here, you're just moving the from clause into an import statement.
Asset-centric modeling already exists with Dagster and similar tools, so I'm really not sure what this is adding that someone else isn't already doing better.
1
u/Gargunok Aug 14 '26
looks like c# linq. Didn't like that.
1
u/ThingElectronic1399 Aug 14 '26
Just work tickets like a good little dev u aren’t good enough to reinvent the wheel trust me buddy
1
u/Responsible_Status49 Aug 28 '26
I’m not sure SQL needs a better version. Nonetheless, I think the direction we are heading to is different. If you look at thinks like Databricks’ Genie you will see that more and more natural language seems to be the way to go to do analysis.
1
u/Yavuz_Selim Aug 14 '26
not this shit again