ChatGPT-SQL篇(会打字就能学会SQL)

朋友们,想转行数据吗?那我的第一个问题就会是,你会写SQL吗?

之前有推荐过几个SQL的课程,其实有了ChatGPT之后,连SQL课程都不需要了。甚至连在电脑上安装SQL,这件曾经让我很崩溃,几乎要放弃SQL的事情也不需要了。家庭教师ChatGPT都可以提供!

[ ⚠️ ]以下会有大量中英文夹杂,因为我不会翻译,介意的朋友可以优雅离场了。

今天来分享一下我测试ChatGPT来学习SQL的全过程,以及我用的prompt。有了ChatGPT,只要会打字就能学会SQL!

以下内容包括:

  1. 让ChatGPT给一个study plan;
  2. 让ChatGPT给题目,不要给答案,并且visualize 表格;
  3. 让ChatGPT分析解题思路;
  4. 让ChatGPT出面试题目;
  5. 让ChatGPT辅导写Leetcode

直接开始!

i don't have SQL on my computer, can you act as mysql

首先我问了ChatGPT它可不可以act as SQL server。它说不行。但是后面测试的时候,只要在对话框里打SQL syntax,它就可以理解啦。所以不用担心!

what are the most basic sql i should know about

接着我问它需要知道SQL最基础的内容。它给了回答。

Screenshot 2023-03-20 at 2.24.18 PM.png
can you come up with a study plan for SQL. i want to learn the basics one by one with practices.
Screenshot 2023-03-20 at 3.01.19 PM.png
can you give me a basic sql question practicing select statement only. please do not give me answer yet. please visualize the example tables with examples.

下一步的话,我们就可以开始练习啦!可以从最上面的一条SELECT开始,让ChatGPT出题目。记得让它不要给答案先哦。并且可以让它数据的时候可以给一个例子。

Screenshot 2023-03-20 at 2.29.07 PM.png
Screenshot 2023-03-20 at 2.29.15 PM.png
how should i start thinking about this question.

但是我完全不会写SQL怎么办呢,那就问家庭教师呀。(不得不说,思路很清晰!)

Screenshot 2023-03-20 at 2.31.08 PM.png
Screenshot 2023-03-20 at 2.31.14 PM.png

到这里,我们就可以开始写写看。我故意写了错的答案,看一下它会怎么指导我。

select first name, last name, salary, from employees where salary >1000
Screenshot 2023-03-20 at 2.34.55 PM.png

这个题目比较简单。JOIN是面试的时候经常会考到的题目,并且会考不同的JOIN之间的区别。所以我让ChatGPT给我一道JOIN的题目,但是不要告诉我是哪一种JOIN。

i want to practice join, including INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN. please give me a question to practice one of the joins. do not tell me which type of join i should use. visualize the example tables.
Screenshot 2023-03-20 at 2.38.01 PM.png
how the output should look like? can you visualize the output table

如果你觉得这个题目有一点点复杂,可以先让ChatGPT给一个output的样子,帮助理解。

Screenshot 2023-03-20 at 2.40.11 PM.png
how should i think about this question

还是觉得有一点不知道如何下手,那就让ChatGPT一步步拆解。

Screenshot 2023-03-20 at 2.41.52 PM.png
how to determine which type of join to use in this question?

在关于JOIN的题目里,决定哪一种JOIN是最重要的一步,我们也可以问ChatGPT。

Screenshot 2023-03-20 at 2.45.12 PM.png
select first_name, last_name, department_name, salary from employees join departments on department_id = department_id

这里我又故意给了错误的回答,在department_id前面没有加上table的名字。

ChatGPT就指出了这个错误,并且教我使用aliases。

Screenshot 2023-03-20 at 2.48.43 PM.png

在各种练习之后,我们可以开始准备面试了!

can you find a question used by facebook during data engineer interview
Screenshot 2023-03-20 at 2.49.55 PM.png
can you give me a more complex question that has been used in data engineer interviews

上面这个问题有一点点太简单啦,我就问有没有更难的问题。

Screenshot 2023-03-20 at 2.51.31 PM.png

最后的话来看一下ChatGPT如何辅导Leetcode SQL题目。 我随便找了一道Leetcode database的题目,https://leetcode.cn/problems/second-highest-salary/,复制粘贴给ChatGPT。

Screenshot 2023-03-20 at 3.12.55 PM.png
can you explain?
Screenshot 2023-03-20 at 3.14.44 PM.png
what if we want to get the Nth highest salary?
Screenshot 2023-03-20 at 3.16.44 PM.png
what does 'LIMIT n-1, 1 ' mean?

这里出现了一个新的概念Offset,于是就追问一下。

Screenshot 2023-03-20 at 3.18.55 PM.png

总结一下:

上面是我测试如果用ChatGPT来自学SQL的实录。总结一下:

  1. ChatGPT教得很好;
  2. SQL最主要的学习方式就是练习以及解题思路,可以让ChatGPT给很多练习题,并且让它给出如何一步步思考的过程;
  3. 遇到不会写的题目,就拿给ChatGPT吧!

发表评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注