1. 面试问题【回答各种知识点】
以下是99%会问到的SQL面试问题,按照出现频率排序,越top越高频,没写在里面的就过于低频了不用准备:.google и
1. What is a JOIN? What are the types of join and explain each?. ----
2. What is a View?
3. 如何优化SQL代码?如果一个 sql query 花很长时间才出结果,可能的原因是什么
4. Difference between WHERE and HAVING ?
5. Difference between JOIN and UNION?
6. Difference between UNION and UNION ALL?
7. Difference between rank & dense_rank & row_number
8. SQL query process/execution steps.执行顺序
9. What is a primary key?What is a foreign key? What is a partition key?
10. When to use GROUP BY?
11. What is a DBMS?
12. What's the characters manipulation functions you have used? 【当时听到题目懵逼了,不懂characters manipulation functions是什么意思,后来回答substring(), upper(), lower()之类处理string的functions,面试官也让过了】
.. 2. 笔试/OA/live coding
准备工作就是刷Leetcodes,premium是一定要开的。刷到easy可以秒做,medium做到读完题的一瞬脑子里有大致解题思路,这种熟练程度就没问题。不用刷hard题。lz本人差不多全部的easy+medium来来回回刷了3遍
有些人会推荐hackerrank。我觉得如果你是SQL小白,刷刷hackerrank是OK的【但如果你真的是小白,推荐刷LintCode而不是hackerrank来练习基础】,准备面试的话则需要以Leetcode为主。因为Leetcodes的题目会给你一个特定的业务场景,跟笔试SQL题比较接近。hackerrank的题奇奇怪怪的。。。什么找三角形这种,感觉挺偏的。。。. .и
.--
接下来上干货:
. Χ 开始live coding时要提前确认的问题:
1) Null的行需不需要显示?如果需要显示的话,是显示Null还是0
2) What’s the format of a timestamp column?
3) Clarify the format of the result and the columns’ name
4) 比率是保留几位小数?百分比还是小数?
5)某列(尤其是where语句中会用到的列)是否存在null值?见Leetcode 584题
6)当输出为null时,是输出null还是[]
一边做题,一边talk out loudly。这样面试官才能检查你的logic和communication.1point3acres
我附上每一类对应的题号方便大家练习:
1) Computing ratio (分别计算numerator分子和denominator分母)
1173. Immediate Food Delivery I 算百分比,可以使用if或者case when,满足条件的设为1,不满足的设为0,这样就能简单用avg来求了
1174. Immediate Food Delivery II 典型题:用case when来标记0和1,再用SUM()/COUNT()算比率
1322. Ads Performance CTR=total clicks/(total views + total clicks)分别统计每一项有多少个可以用sum(if(列 = ‘XXX’, 1, 0)),sum(if())的优点是,将数个重复的只有where条件不同的query组合成一个query
1211. Queries Quality and Percentage 典型题:用case when来标记0和1,再用SUM()/COUNT()算比率。注意哪个是1哪个是0
2) Data Categorization
1435. Create a Session Bar Chart 不能使用case when的情况:一定要显示某种分类,即使没有一行落入该分类。此时需要手动SELECT “”然后UNION拼接. 1point 3acres
1393. Capital Gain/Loss case when来定正负(+-1),再sum()
1468. Calculate Salaries case when, round( , 0)
1212. Team Scores in Football Tournament 陷阱多,使用UNION ALL来包含重复项(尤其是需要sum()的时候不要忘了). From 1point 3acres bbs
1907. Count Salary Categories 使用case when的话,不存在的分类不会显示null而是根本不会出现,所以要手动UNION . From 1point 3acres bbs
3) Cumulative Sum (与time series data结合考)
这个题型我总结出了一个模板,应该附件里就有。consecutively题型, 第一种就是套我的模板,第二种就是传统的使用JOIN id+1, JOIN id-1
534. Game Play Analysis III 窗口函数 sum(partition by order by) 来统计累加值(so far)。或者 JOIN ONdate1 ≥ date2
1454. Active Users consecutively题型, 使用DATEDIFF(l1.login_date, l2.login_date) BETWEEN 0 and 4解决consecutively题型
180. Consecutive Numbers consecutively题型。WHERE语句中不能使用连比,LEFT JOIN ON l1.id = l2.id-1 ON l1.id = l3.id+1
1454 最好用window function. datediff between 1and 4 这个有四种可能,intermediate table 会很大
网上找到个code, 写的不是很好,有点啰嗦,但基本意思是用window function。
SELECT DISTINCT a.id, a.name
FROM (
SELECT
id, LEAD(login_date, 4) OVER (PARTITION BY id ORDER BY login_date) AS five_days,
DATEDIFF(LEAD(login_date, 4) OVER (PARTITION BY id ORDER BY login_date), login_date) AS gap.google и
FROM (SELECT DISTINCT id, login_date FROM Logins) c. 1point3acres.com
) d
LEFT JOIN Accounts a ON a.id = d.id
WHERE gap = 4
ORDER BY a.id