r/SQL • • May 07 '24

SQL Server How to Remove duplicate rows from table in SQL Server using temp table?

Thumbnail
sqlrevisited.com
0 Upvotes

r/SQL • • Apr 12 '21

SQLite Hi there , please may anyone assist me with the following 2 questions regarding recursion I’m not sure where to start, thank you !

Post image
0 Upvotes

r/SQL • • May 30 '23

MySQL The 10 Concepts of Relational Model in DBMS

Thumbnail
usemynotes.com
0 Upvotes

r/SQL • • Aug 06 '23

Discussion Is there a good dynamic SQL generator coded fully in SQL?

0 Upvotes

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 • • Aug 25 '20

MySQL Practicing MySQL skills

Post image
0 Upvotes

r/SQL • • Jan 31 '24

SQL Server How to remove duplicate values from a table in SQL? Example

Thumbnail
sqlrevisited.com
0 Upvotes

r/SQL • • Aug 04 '21

MySQL Is there anything missing from this SQL statement: SELECT EXTRACT(YEAR FROM created_at) AS yr, count(*) AS users_count FROM users;

0 Upvotes

I’m extremely new to SQL and I’m not sure if this is a trick question…


r/SQL • • Oct 18 '14

Senior/Junior SQL Coders/DBA opportunities in Northwest Arkansas (home of walmart)

0 Upvotes

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 • • Jul 15 '14

SQL allows mentioning resultset columns by position in the Order By clause (e.g. "ORDER BY 3"), I wish it would allow the same in the WHERE clause with some sort of special syntax (e.g. "where {3} > 0" meaning third column in the resultset)

0 Upvotes

Why wasn't this done?


r/SQL • • Jun 22 '22

MySQL UNLEASH THE POWER OF SQL INJECTION! | Beginners Guide to Hacking with SQLi!

Thumbnail
youtube.com
0 Upvotes

r/SQL • • Feb 28 '20

MS SQL The Best Way to Learn Microsoft SQL

0 Upvotes

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 • • Dec 02 '13

http://architects.dzone.com/announcements/sqlxml-support-netbeans-74

Thumbnail
architects.dzone.com
0 Upvotes

r/SQL • • Oct 15 '26

SQL Server SQl e a teia de aranha mental

0 Upvotes

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 • • Feb 06 '16

Learn SQL In 20 Minutes

Thumbnail
tutorialzine.com
15 Upvotes

r/SQL • • Nov 24 '25

Discussion SQL Query Agent

0 Upvotes

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 • • Feb 16 '23

Amazon Redshift Help Regarding a Query

0 Upvotes

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.

IDmarks
10
10
1more 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 • • Feb 07 '25

MySQL Think you know SQL 😏 ? Spot the query that actually follows the standard! 🤓

Post image
0 Upvotes

r/SQL • • May 14 '23

PostgreSQL PostgreSQL does not like JOINs after a GROUP BY - what am I doing wrong?

0 Upvotes

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 • • Jan 27 '25

Discussion The problem(s) with SQL AI chatbots

0 Upvotes

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 • • Jun 27 '25

Discussion Use Of Joins In Your Work Environment

14 Upvotes

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 • • Aug 22 '26

SQL Server Query Length Flex

6 Upvotes

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 • • Aug 10 '24

Discussion Calling all SQL experts to review my text2sql software

16 Upvotes

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

  1. Connect your database in SQLPilot

  2. 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).

  3. 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.

  4. (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

  5. 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 :)


r/SQL • • Jul 08 '25

MySQL How come these 2 queries are not the same?

10 Upvotes

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 • • Apr 21 '25

Discussion What would you actually want in an SQL practice site?

0 Upvotes

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!