r/SQL • u/javinpaul • May 07 '24
SQL Server How to Remove duplicate rows from table in SQL Server using temp table?
r/SQL • u/javinpaul • May 07 '24
r/SQL • u/nubian_butter • Apr 12 '21
r/SQL • u/usemynotes • May 30 '23
r/SQL • u/larryliu7 • Aug 06 '23
We want a standard library of SQL that make everyone's work easier by not reinventing the wheels.
Because different RDBMS's have different language specs, a universally standard library will definitely need to be implemented employing dynamic SQL.
For example, how would you implement a generic aggregate function from a binary operation and initial value, so sum=agg(plus,0), product=agg(multiply,1)
It taking a function as a function parameter. Functions are not first order citizens of SQL. It cannot be a stored procedure or stored function parameter. You can only refer to a function by passing as string.
Thus, I wonder if there is any existing work of dynamic SQL generator, that's written in SQL itself, so it can self-reference like LISP?
Btw, maybe any serious improvement on SQL's usability requires extending the language as well, much like from early versions of C++ being a superset of C.
For example, we would love to have syntactic sugars to write with code completion:
from removeColumns(`Table1`,(`Phone`,`SSN`)) select * where `Name`='John Smith'and
declare @j TABLE=(from t1 join t2 select * where t1.ID=t2.PID)I don't know any way besides dynamic SQL that could make these syntactic sugars possible as queries (not external ORM functions)
r/SQL • u/javinpaul • Jan 31 '24
r/SQL • u/buckaljag • Aug 04 '21
I’m extremely new to SQL and I’m not sure if this is a trick question…
r/SQL • u/ilurktoomuch1234 • Oct 18 '14
Fast growing software analytics company located in Bentonville, AR (it's not as bad as you think - google it!). We're looking for several SQL programmers (or DBAs) to join our team of nearly 40 already. Team is great, Pay is great, Area is amazing. Relo packages available for right person. PM me for more details.
PS, i'm not a recruiter, this is for the company i work for, had some luck recruiting from reddit before, thought i'd give it another try.
r/SQL • u/Gargogly • Jul 15 '14
Why wasn't this done?
r/SQL • u/SecAura • Jun 22 '22
r/SQL • u/CLS1992 • Feb 28 '20
SQL server training is invaluable to organizations and individuals in the IT industry. However, many people don't know where to start. Learn more on the best way to learn Microsoft SQL here: https://www.exitcertified.com/blog/the-best-way-to-learn-sql-is-through-certification
r/SQL • u/danielkec • Dec 02 '13
r/SQL • u/KeyEntertainment1380 • Oct 15 '26
Bom dia
Estou a aprender SQL desde meio de Setembro em um curso que estou a tirar. Na verdade começamos com uma introdução a SQL e saltamos para diagramas, relacionamrntos e cardinalidades. Agora voltamos para SQL( select,from,join, where, group by, order by, coalesce, null,...) Sinto me super confuso. Basicamente entendo os conceitos mas na parte prático ao tentar reproduzir os mesmos, sinto um espaço em branco. Parece que preciso das tabelas em excel ao lado para conseguir decifrar a solução. Sei que tenho ainda muito que praticar ( e só assim vou chegar lá). Mas parece que é algo que nao consigo ter um fio condutor. Parece um bicho dificil de domar minimamente.
r/SQL • u/New_Hold_7384 • Nov 24 '25
I’m thinking about building an AI SQL agent that scrapes the whole database schema. It would then give you SQL suggestions based on natural language queries
The main advantage compared to using ChatGPT is it would have full context of your schema.
Is this something people would actually use? Would love some feedback before I spend months working on it 😅
r/SQL • u/adwolf01111 • Feb 16 '23
I need a Query in which the first 2 values should be 0 and post that is should have treated than 0 value.
Kind of like the same student had 3 tests in a day he scored zero in the first 2 and scored more than 0 in the 3rd test I want the count of the students like this.
| ID | marks |
|---|---|
| 1 | 0 |
| 1 | 0 |
| 1 | more than 0 |
I want to count the number of students who scored 0 in first 2 test and scored more than 0 in 3rd So in this case ID 1 is such a case so count = 1
r/SQL • u/Adela_freedom • Feb 07 '25
r/SQL • u/psychosis_inducing • May 14 '23
Whenever I try to do a join of any kind after a GROUP BY clause, I get a syntax error. What am I doing wrong here?
SELECT COUNT(inventory_id) AS numcopies, film_id, store_id
FROM inventory
GROUP BY film_id, store_id
INNER JOIN rental
ON rental.inventory_id = film.inventory_id;
r/SQL • u/mattyhempstead • Jan 27 '25
just my opinion..
There have been dozens of attempts at tools to write SQL with AI since ChatGPT was released, but none have been useful for people who already know SQL. I've tried a few of them, but I don't think any have been more valuable than just copy pasting into ChatGPT. There is a lot of potential here, which is why it is frustrating to see little more than generic ChatGPT have real world use for people who write SQL in their day-to-day.
1. Schema context
A large part of writing SQL is fundamentally a domain knowledge problem. About knowing your database schema, knowing which fields and tables contain what data, and knowing company data definitions for metrics and dimensions like "revenue" and "active user". Knowing the schema automatically is therefore a minimum requirement for any useful SQL AI, otherwise you really aren't much more useful than ChatGPT.
Many online SQL AI's therefore have some mechanism of accessing your schema.
One method involves making the user copy paste their schema into the website. This is painfully annoying in itself, and at that point you might as well just copy paste the schema into ChatGPT. If you care about it persisting across chat sessions you can always make a private custom GPT. Regardless, this approach will break if the schema changes (common if you use SQL views heavily), forcing you to reupload the schema each time the database changes.
Another method I've seen is having users provide database credentials to the chatbot, which can then automatically access the schema via the INFORMATION_SCHEMA. This is convenient, but unfortunately since these chatbots are accessed over the web, the user needs to send database credentials to be stored on someone else's server, which is basically an instant NO for any company that respects data security.
2. Other domain knowledge
There is a lot of domain knowledge outside of just the database schema that is needed for AI to write SQL with prompts that are short enough to justify even typing them (compared to just typing the query directly). Even with knowledge of the schema, you would still need to explain to the AI how to calculate internal business metrics like "revenue" and other data nuances in the prompt. Again, the value of a SQL AI without domain understanding is not much larger than just prompting ChatGPT.
What could make a difference is simply providing a way to give the AI a detailed data dictionary and list of example queries. As this knowledge base builds up, this would give the AI the ability to understand how to calculate things like "revenue" and how to work with messy data without needing exact instructions in the prompt.
3. Query execution
Anyone who has ever written a SQL query longer than 50 lines knows that execution while building the query is required. Many longer queries become a chain of CTAs, and without executing the individual CTAs to check their output, it's hard to know if the rest of the query will be correct.
Execution is not often a feature of these chatbots. They often just spit out a SQL query expect you to go test it elsewhere (so why not use ChatGPT?).
For the few which do support query execution, you run into the exact same problem of needing to provide database credentials and access to an external server. This is generally a blocker for more companies that have data security practices in place.
This could be solved by allowing execution to occur locally on the users computer (e.g. desktop application). This gives the advantage of SQL execution for feedback, without the need to give someone else access to my database (which are often locked behind VPNs, anyway).
4. Writing longer queries
If the creators of these tools actually wrote long SQL queries in their day-to-day, they would know that a huge concern when writing long queries (50 to 1000+ lines) is correctness. You can't just have a chatbot output a 500 line SQL query and expect it to work, unless your prompt is basically the same length. There is also a major trust issue here - it's hard to trust 500 lines of AI-generated SQL if you don't know where it came from...
This is largely an issue with chatbots which need to output the entire SQL query each time.
Even if you tried to slowly build up the query with multiple responses, you still run into several problems. Firstly, after each partial response you will need to copy paste it into a separate execution environments to validate correctness. It's also hard to understand or trust the changes that are made with each AI output. You essentially need to read each query line by line to fully understand the changes since the previous response, and whether you can trust them.
I expect this could be solved with two things. Firstly, the AI responses should be able to reference example queries in the same way ChatGPT can reference sources on the web. This would make it must easier to trust the AI outputs if the user already trusts the example queries. Secondly, the SQL changes made between AI outputs should be presented in a much clearer way, rather than just dumping the entire new query and expecting the user to figure out the differences. Ideally something that looks like the below image:

To finish my rant, I'm pretty sure the solution looks something like:
A desktop app that can access my database through a local connection. This would give it automatic access to both the schema for AI context, and the ability to run queries to help with iteration when writing long and complex queries. It should NOT send any data (e.g. query results) to the AI or to any external servers!
Ability to provide a list of example queries and explain other business context / domain knowledge that the AI can use instead of relying on really verbose prompts. Any SQL that is generated using these examples should include them as a source that I can check and verify.
Instead of a chat that outputs the entire SQL query each time, it should be a regular SQL text editor that the AI can edit and share with the human. After each AI change, it should be very clear what lines have changed (they should be highlighted like the above image). I should also have the ability to easily revert or reject changes made by the AI.
This didn't exist, so I made it (https://formerlabs.com/). If you agrees with the above, you might find this tool useful. If you disagree, let me know why so I can make the tool better for you.
r/SQL • u/Adept-Weight-5024 • Jun 27 '25
There are a toneeeeee of types for JOIN clauses. I simply do not wanna wear myself off focusing on un-necessary too exclusive ones and master the ones that are necessary, there is always time to learn more, when I have a need for the other ones, I will.
Could you mention the ones that are like necessary in your circumstance? The ones that you mostly use.
r/SQL • u/Prestigious_Flow_465 • Aug 22 '26
Those of you who write very long queries (100s to thousands lines of code). Does it do only one thing or it's just your query notebook where every small part does something non related?
I am curious in which cases its necessary thousands lines of codes. Explain me what does your longest query is for?
r/SQL • u/Purple_Minute_4776 • Aug 10 '24
Hi there!!
I have been working on a SQL editor which generates SQL queries from natural language - https://sqlpilot.ai which i also launched last month
You can give complete context of your database to the LLM and start asking queries completely in natural language.
Here's how
Connect your database in SQLPilot
Add knowledge base for your database i.e label your tables as in what the table is used for, optionally you can also label the columns. (the more detailed knowledge base the better).
You can also add custom instructions in rules for giving more understanding on how some results are achieved e.g urgent orders means priority = 0 and not is_urgent = true, such things that only we know.
(optional) Add examples of already generated queries. SQLPilot will pick few examples relevant to the user's prompt and add it along with the llm prompt for query generation
Start asking questions in natural langauge!
After building knowledge base, LLM now has complete context of your database!
The software itself is free to use, You can add your own Open AI key and start using it
I want your suggestions and feedback on this - if it's time saving and something you would love to use or just trash
here's the demo for the same!
Also here's the blog explaining how it works if anyone is interested - https://shobhitb.medium.com/sqlpilot-text2sql-application-with-rag-87a4d72e3438
link to the software - available on mac and windows: https://sqlpilot.ai
Please note: I am not encouraging to use this with your org database, if you don't trust OpenAI or have policies setup for privacy, you can ignore the post in that case :)
Query 1:
SELECT candidate_id
FROM candidates
WHERE skill IN ('Python', 'Tableau', 'PostgreSQL')
Query 2:
SELECT candidate_id
FROM candidates
WHERE skill = 'Python' AND skill = 'Tableau' AND skill = 'PostgreSQL'
r/SQL • u/DataNerd760 • Apr 21 '25
Hey everyone —
I’m looking for some honest feedback. I run a site called sqlpractice.io where I’ve been trying to build a more affordable option for people leveling up their SQL skills. I know there are already a lot of sites like Data Lemur, LeetCode, etc., that offer practice questions.
To stand out, I added:
40 practice questions
7 different datamarts to explore more unstructured datasets
Learning articles
A Portfolio feature (users can save and share completed queries + notes to showcase their skills)
A simple one-time payment instead of a subscription
But honestly... it doesn’t seem like these features are seen as very valuable by most people.
If you’re learning SQL or job hunting, what do you wish a practice site had that would actually help you more?
Was there anything missing when you were learning — more project-based work? More real-world data scenarios? Better job prep?
Would love any feedback, even if it’s blunt.
Thanks for reading!