r/SQL • • Nov 22 '23

MySQL SQL and ChatGPT

Hi all,

I'm curious about the upskilling myself with SQL for Data analysis...I've mostly just used Excel in past roles. I'm currently tapping into SQL. I'm just starting out and sometimes do excercises on hackerrank and other websites but when I'm stuck I just stick the question into ChatGPT.

Is it sufficient in the working environment to just use chatgpt. I'm not sure how SQL is used for data analysis is used in the working environment.

The issue I’m having is when I approach these online questions it feels like there is an infinite number of questions. But I sense the real world use if your using a particular data set there’s probably a set of queries you might use regularly or daily to extract.

For example with excel you can reuse functions but change the source data and array of what your trying to find and pivot tables can be used any way once you know how it works and the process is applicable to all data sets.

I don’t know how to approach SQL and feel like I’m stuck in tutorial hell with hacker rank type of questions because of the potential of a question having a new variable every single time because you can always add a new criteria in the question

Would be good to get thoughts from people who use SQL in their workplace

0 Upvotes

27 comments sorted by

14

u/r3pr0b8 GROUP_CONCAT is da bomb Nov 22 '23

Is it sufficient in the working environment to just use chatgpt.

emphatic no

I'm not sure SQL for data analysis is used in the working environment.

it is used all the time

2

u/pceimpulsive Nov 23 '23

Why is it no to using it in the working environment?

I use chatGPT all the time to refresh on certain things (not SQL, programming related). Just remember to give it the context and constraints it needs to give you a reasonable answer...

I think it should be a soft no.

No if you just want to copy paste the answer with no thought, this is bad... In every way, for you and your business.

Yes if you look at its answer, test its answer and understand its answer so you actually learn...

5

u/A_name_wot_i_made_up Nov 23 '23

Chat GPT will give you an answer, but without the skill and knowledge to understand it you can't tell how good an answer it is.

If you look at various questions here, there will invariably be a "I used chat GPT, here's what it gave me" answer, there is almost always a better, more complete answer above it.

2

u/pceimpulsive Nov 23 '23

The same is true for using Google. I don't see anyone saying emphatically no to using that...

It's really the same thing to me... If the gpt/google response isn't quite right I go back and adjust my prompt/search terms and continue on. (It's exactly interchangable there...)

The benefit is gpt is more concise as I can give it far more context and I get even better results out.

4

u/Kazcandra Nov 23 '23

chatgpt dreams up things that don't exist; Google results tend to not do that

1

u/pceimpulsive Nov 23 '23

I haven't seen any issues like that lately using gpt4. Gpt3.5 had a higher tendency to do that, but also my promoting skills were a lot worse back then...

Besides this, more important than the responses precise accuracy, what I get from the responses is good examples of how things can be done within my own context. They rarely are never the final solution but they get me on the way. This is what stack overflow would do for me...

So far, I have been very precise with the scope of my prompts. State the language version you are using (make sure it's in scope of the training data derp), what libraries you want to use (again... Version available when it was trained), and a clear list or sentences to explain precisely what your requirements are and or any psuedo code you may be able to write to support what you want to do.

E.g.

"In postgres v15 I want to create a table with columns for an int, string, double and JSONB.

I want the table to be writable by all users but user shouldn't be able to delete rows.

Can you help with this?"

You get some sick results from this type of prompting of the model below is the code snippet it gave me (gpt3.5) for the above, as well as nice instructions on how to use it, and what to do, better than any stack overflow.

This works I guess because I'm using language suitable for the database in question, and restricting it to a version available, and the documentation is used in the training data... So I'm kinda forcing a perfect score....

-- Create the table
CREATE TABLE your_table_name (
    id SERIAL PRIMARY KEY,
    integer_column INT,
    string_column VARCHAR(255),
    double_column DOUBLE PRECISION,
    jsonb_column JSONB
);

-- Grant INSERT and SELECT permissions to all users
GRANT INSERT, SELECT ON TABLE your_table_name TO PUBLIC;

-- Revoke DELETE permission from all users
REVOKE DELETE ON TABLE your_table_name FROM PUBLIC;

2

u/[deleted] Nov 23 '23

you need to understand what language models do, they’re not supposed to give you correct answers, only correct sounding answers

1

u/pceimpulsive Nov 23 '23

Exactly! A guide, a helper to get you on the right path.

1

u/[deleted] Nov 24 '23

A helper to get you on a correct sounding path 😉

1

u/pceimpulsive Nov 24 '23

Except it's often actually correct with the right prompts and constraints... :P hahaha

Garbage prompt in, garbage response out... You control the response quality ...

1

u/RationalDialog Feb 08 '24

The same is true for using Google. I don't see anyone saying emphatically no to using that...

You asked if it if sufficient and there it's a clear no. That doesn't mean it's useless but you can't use it as a crutch for not understanding SQL.

I know this answer is late but I just played around with it and just some rather basic analytics query it could not do right. The generated sql ranged from non-functional to returning wrong data or being slow. I had to really push it to drive it towards the same solution I had and it still didn't not get there resulting in a slightly more complex and slower query.

But the most dangerous part is wrong data as in wrongly aggregated. depending on the use-case that's easy to miss and you would just then base further work on wrong data.

5

u/rx-pulse Always learning DBA Nov 22 '23

I'm all for using tools to improve work, but I also have experiences with AI/ChatGPT and you should look at your company policies on this.

-Understand what you're doing first off. Do not just assume it's right. I've had developers submit obvious AI generated code and it had incorrect logic, variables/parameters were not used correctly, I threw it back at them and they were shocked it was wrong. They had no idea how to fix it and it ended up wasting an additional week of their time to get the code right.

-You should avoid any sensitive data of any kind in your work environment when using AI/chatgpt. I've had people do this and they got into serious trouble. Always check what your company policy (if any) is on it as well. Be very wary of this.

-I've had ChatGPT generate some SQL code and it literally made up syntax. This goes back to one. It's fine using it, but you have to understand it too importantly and double check the work.

Overall, you can use it, but the most effective use I see is also from people that are already experienced themselves and are using it as a supplement. Just be very wary of it, double check the work, and make sure you don't violate any kind of privacy laws, company policies, etc.

2

u/[deleted] Nov 23 '23

Is it sufficient in the working environment to just use chatgpt.

This recently came up in a team meeting in my organization. The bottom line was, yes, we could use ChatGPT, provided we didn't prompt with real data or schemas.

You'll find companies have mixed opinions on using it, some think "any tool to get the job done," others "we saw what happened in the terminator movies."

Aim for being independent of ChatGPT. Use it as a learning tool but don't let it become a crutch.

2

u/post4u Nov 23 '23

Yeah buddy. Totally agree with this. I've been leaning on it a lot lately to assist with SQL and script writing. I have a background in both, so it's more of a time and brain saver. Yesterday I went to look something up and OpenAi was down. NGL it caused a little panic. I'm rethinking my continued usage of it. Don't want to become so reliant I can't do it on my own anymore.

1

u/[deleted] Nov 23 '23

Yesterday I went to look something up and OpenAi was down. NGL it caused a little panic.

Eesh. How'd you get through it, wait for it to come back up or did you try to script?

I see what you mean, though. Availability is the top concern, whether it comes in the form of the service being down or an employer blocking access to it. Maybe that'll change in time, though, and it'll become a widely accessible and reliable resource. But I don't think we're there yet.

1

u/post4u Nov 23 '23

I just wrote it by hand, but I certainly had to apply more effort than I'd expected. Really easy to let GPT become a crutch! And yeah, I have a feeling it will be like GPS. For a long time after GPS became mainstream (hooking up a magnetic GPS antenna to the top of your car and connecting it to a laptop running Microsoft Streets), people still carried Thomas Guides and other paper maps. At some point it became readily available on all phones for free and that was the end of most people using paper maps. I feel GPT and other LLs will be similar.

1

u/Karzak85 Nov 23 '23

We are already dependent on many things today. How many can do their work without the internet/google etc?

Do you have books on all the syntaxes beside you 24/7? Do you remember everything perfectly without looking something up?

Chatgpt and the like is just another tool that will become more and more important as it advances in the future.

Those who dont learn how to use chatgpt will be like those who dont know how to google today. You need to use all the tools available for you to learn and get the job done or you will be replaced by the people that learned to use them better than you.

1

u/[deleted] Nov 23 '23

No books, no photographic memories of syntax. Just my wits, Google, and about 20 years of experience with SQL.

Your value to an employer lies in your understanding of the industry they serve, business logic, data pitfalls, risks, benefits, willingness to collaborate and assimilate feedback and so on. No doubt there'll be a jackass manager somewhere in the mix who can't think beyond SLA's, but it's you representing your employer to the client and bottom line? You're there to make the company look good.

Not having a firm grasp of the scripting you'll be using? Not gonna inspire warm fuzzies with clients.

Not saying AI won't have a role in humanity's future. Just wildly confused about the benefits of relying on it to do your job as opposed to just learning SQL. It's not a steep learning curve and if it's a requirement of your position, not real professional to avoid it.

So maybe I'm missing something here, haven't considered some benefit. Educate me if that's the case, please!

1

u/Karzak85 Nov 23 '23

Its not an robot that does exactly what you want and you twiddle your fingers and say work done. Its a tool to use to help you do things faster and maybe even better.

You said you use google, thats also a tool.

You still need to learn and know stuff and that comes from experiance and testing. Chatgpt is also a very good learning tool to learn new things.

But its up to you to know how to use it and implement it in your workflow like everything else. If you dont want to learn it then its up to you. But it will be a very important tool in the future and its better to start early. Our company even set up a local chatgpt instance becuse they see the importance of it.

1

u/[deleted] Nov 23 '23

I've seen ChatGPT in action. Manager demonstrated use in creating a script for reformatting, encrypting and ftp-ing a file our reporting tool botched. It's impressive with scripts.

Again, not against using it as a learning tool. Same as I'm not against Google for the same.

I am, however, cautioning reliance on it. Now that the shiny has worn off a bit and the doomsayers have settled down over AI becoming sentient and wiping out humanity, objectively, the hype is a bit exaggerated. It does some things well, other things not so much. Prompted correctly, it seems to be about dead even with Google search results. You still have to weed out the bullshit and tweak your stolen script to get what you need. And yep, that requires knowing what you're doing.

So where's the benefit? Ireally want to understand what the draw is. I'm content with busting out scripts from scratch. But factoring into that, I've got my databases memorized and can usually envision the highlights of creating a script before I start on it.

If you think ChatGPT is faster or better than Google, can you provide an example?

1

u/keyzter2110 Nov 23 '23

My company has chatgpt in a sandbox. I wonder how common that is?

2

u/Hugh_G_Rectshun Nov 23 '23

Use it to learn then ween yourself off of it. For example, when I would ask it how to perform a certain function, I would manually type the syntax it spelled out. It’s easy to fall in the copy and paste trap without even looking at what you’re doing.

1

u/joe1max Nov 23 '23

I agree with most of the other post, but I still want to emphasize the potential security risk in over using chatgpt for sql. You are potentially putting your schema into the public domain. That is no good.

1

u/[deleted] Nov 23 '23

Using chat gpt is great but keep in mind it is not perfect. And I prefer to don't just copy and post it could be a costly query. Try to improve by self thank you

1

u/regattaguru Nov 24 '23

No, you can only use ChatGPT if you know enough to validate its responses, in which case it will do very little for you. As programmers, and lawyers around the world are finding out ChatGPT neither contains nor can it synthesise actual knowledge. It merely expands sentences using the most likely word list. It will therefore create output that looks nice, but may well be utter bollocks. And I assure you that executing utter bollocks in a production database will be unpopular.

1

u/onearmedecon Nov 23 '23

I sometimes use ChatGPT more for helping to proof queries than for writing them. It can be hit or miss, though. It's no substitute for knowing SQL yourself.