Challenge - AI and SQL

Use Chat2BT to generate SQL queries based on database screenshots and analyze your data.

Leverage the power of Chat2BT to effortlessly generate SQL queries, enhancing your data analysis workflow. Discover practical techniques to effectively prompt AI using visual database structures.

Key Insights

  • Utilize Chat2BT to automate SQL query creation by clearly specifying your SQL flavor (such as Postgres or SQL Server) to avoid inaccuracies.
  • Streamline interactions with Chat2BT by creating targeted chats for individual SQL queries, preventing confusion from lengthy or overlapping conversations.
  • Optimize database diagram sharing by capturing screenshots directly from tools like dBeaver or SQL Server Management Studio, making it easier for Chat2BT to identify table and column names accurately.

Note: These materials offer prospective students a preview of how our classes are structured. Students enrolled in this course will receive access to the full set of materials, including video lectures, project-based assignments, and instructor feedback.

All right, so now it's time for you to get hands-on using ChatGPT to help you write your SQL queries. Envision that you're a data analyst and you're trying to think of what are things that I would want to know about my data. You'd want to look at your sales, start to understand your sales.

For example, in 2021, where did our revenue come from? How is it distributed across our states? How much money are we making per state? Secondly, another example, how much are our sales growing or shrinking? What's that year-over-year revenue change? Let's compare our revenue from 2020 to 2021. What's that change? Your challenge is for you to write these queries using the help of ChatGPT. So instead of you writing it yourself, prompt ChatGPT to get it to write the SQL queries for you.

Additionally, aside from these examples, are there any other ideas that you can come up with? Think about what we have in our database and what are some questions you could ask and try to get ChatGPT to write those queries for you. As you're doing this, here's a few things to keep in mind. So that you don't have to explain your database to ChatGPT, take a screenshot and upload that.

That way it'll know your table and column names and can write your queries so they work without you having to insert those names manually yourself. I'll show you a couple tips on how to do that easily in just a moment. Tell ChatGPT which flavor of SQL you're writing.

If you're using Postgres or SQL Server, make sure you make that clear because otherwise it might assume the wrong one. Once you're done, test your code. Make sure it runs.

SQL Bootcamp: Live & Hands-on, In NYC or Online, Learn From Experts, Free Retake, Small Class Sizes,  1-on-1 Bonus Training. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

Look at the results. Does it pass the sniff test? Does everything seem right? Look at the code. See if it makes sense.

Run it and take a look at the results. Do the results make sense? And as you're doing this, if you just have one long chat and you're asking it multiple things to do different things, sometimes it can get hung up or kind of remember things that it was working on and think that you just want to keep changing the current SQL query. If you want to do a completely different query, consider making a new chat for different tasks or different queries instead of just keeping one long running chat in ChatGPT.

So here are some tips of creating your screenshots of your database structure. Here in dBeaver, I can double click on my tables and click on the diagram here to see my entity relationship diagram. If there's tables that I'm not interested in or views or things that I want to delete, I can select those and then I can do a right click or a control click and delete those.

So anything that I'm not interested in, I could just get rid of those from my diagram. And if I want, I'll get rid of this one. And then whatever I'm left over with, then I could again, right click or control click and save this diagram as a screenshot, essentially as a PNG image.

And I can save that wherever I want. Go to ChatGPT, click the plus and upload it from my computer. So that's one way to do it.

But a faster way to do it might be if, let's say you're on a Mac and you just want to do a screenshot. So let's say you don't want to edit and do all of that stuff. Let me close these without saving changes to it.

When you just double click to open this up and you look at your diagram here, if I just want to screenshot a certain area of this on the Mac, I can hit Command Shift 4. And after I hit Command Shift 4, I can then drag over the area that I want to screenshot. And before I let go, I want to copy this to my clipboard so I can simply paste it into ChatGPT. So before I let go, I'm going to hold Control.

So I hit Command Shift 4, drag over the area. Before I let go, hold Control while I let go, that gets copied to my clipboard. Now I can go to ChatGPT and I can simply paste with Command V and it pastes into ChatGPT.

If I'm creating a database diagram for SQL Server in SQL Server Management Studio, I can right click here on my database diagrams and create a new diagram just with the tables that I'm interested in. You might not be able to save this, but in this case, even though I can't save it, that's okay. I can still create the diagram and I can select the tables that I want to add to it.

And once I'm done here, I can then copy that diagram to my clipboard. So here I can right click and copy it to my clipboard. Now, if you can't paste, like here I'm hitting Control V to paste and it's not working, then we just have to go through something like Paint, Microsoft Paint, and open that up and do a paste with a Control V and then copy it from here.

So Control C once again, and then I can go and paste in there and then you've got your database diagram. So go ahead and take up your challenge and see how you do. And in the next video, we'll come back and talk through and see how we might approach this.

How to Learn SQL

Master SQL with hands-on training. SQL is one of the most in-demand programming languages and is used across a variety of professions.

Yelp Facebook LinkedIn YouTube Twitter Instagram