r/learnSQL • u/DiscombobulatedBid19 • Jan 04 '26
How good are LLMs at generating SQL queries in 2026?
I’m a mechanical engineer by trade, thinking of going into data analytics if feasible. The idea came to me when I was studying SQL: the language seemed so easy. So instead I thought of reading the book: SQL and Relational theory by C. j. Date, brush up on the theory, on the WHY, and leave the basic syntax to the LLM for now (I’d pick it up very fast later). What’s your thoughts on this approach?
5
Upvotes
11
u/13ass13ass Jan 04 '26
You should try doing something interesting with sql. A project. Even if you’re generating the sql wit an llm, it’s better that you know how to use rather than why to use it.
Books aren’t a great way to learn sql.
1
u/DiscombobulatedBid19 Jan 04 '26
Of course, but these mistakes are detectable or fixable right?
5
u/PythonEntusiast Jan 04 '26
It makes mistakes.
4
u/DMReader Jan 04 '26
It depends on the complexity to be sure. You will want to be very explicit with column names and data types.
3
u/B_Huij Jan 04 '26
Syntax isn’t what makes SQL hard. Institutional knowledge about the idiosyncrasies of your data sources is the difference between a good query and an unusable one.
Can LLMs write syntactically correct SQL? Usually.
But my experience with trying to leverage them to write complicated models for me has been that it takes way longer to prompt and re-prompt than it does to just write the model myself. With the added advantage that if I write it myself, and later on it needs changes, I know the query well enough to tweak it without breaking stuff, instead of rewriting from scratch.
And this doesn’t even touch on the topic of computational efficiency of the query.
2
u/DatabaseSpace Jan 04 '26
Reading books on database theory is a good idea. It's really interesting material.
1
u/maestro-5838 Jan 04 '26
Deepseek is better than chatgpt I have found for sure. I haven't tried others
1
u/BungieDidntDoIt Jan 04 '26
It depends. I know SQL, i use llms for scripting that i don’t do often enough to remember all the syntax. It’s good at knowing sql syntax, it’s bad at fitting my specific context.
But as a tool its great
1
u/squadette23 Jan 04 '26
"SQL and Relational Theory" is a weird book, frankly I am not sure that you will get a lot of value out of it, given your needs and background. For example it only mentions "LEFT JOIN" once. Out of 500+ pages, it spends 5 (five) on joins, and 1 (one) one what it calls "semijoins".
Second chapter ("Types and domains") is 40 pages long and the first sentence is "This chapter is related only tangentionally to the main theme of the book". Second chapter!
I think that you would be much better if you read https://modern-sql.com/
Also, specifically about joins and LLMs: here is my take on JOINs, it's long but it's relevant to your question: https://kb.databasedesignbook.com/posts/sql-joins/ e.g. "Note that commonly used LLMs consistently get confused about that topic. In some cases they will proudly announce that they just wrote you a correct SQL query that avoids double-counting, but for a different case they will get back to writing a knee-jerk LEFT JOIN chaining version. They certainly know about the overcounting problem, but they don’t always use that knowledge."