大家好,作为SQL做题家来分享一下笔试面试经验,如果你之前没有面过SQL,只用看本文就能准备充分了!!希望可以帮助到目前正在找工作的小伙伴!求加米!!真的很需要米米!!!!!谢谢!!!
. 1point 3acres 1. 面试问题【回答各种知识点】
以下是99%会问到的SQL面试问题,按照出现频率排序,越top越高频,没写在里面的就过于低频了不用准备:. 1point 3acres
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,面试官也让过了】
开始live coding时要提前确认的问题:
1) Null的行需不需要显示?如果需要显示的话,是显示Null还是0.google и
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-baidu 1point3acres
写完代码自己检查的时候需要检查的问题:
1)SELECT 多个列时,记得加逗号, 多个CTE WITH语句时,记得加逗号
2)列名关键字有无拼写错误
3)subquery 有没有重命名【千万不能忘记alias】. 1point3acres.com
4) SQL关键字需要用’’括起来,比如’rank’,见178题
5)group by 有没有忘记. Χ
6)DISTINCT有没有忘记,一切非primary key的column都要注意是否有重名
7)CREATE FUNCTION题型,不要忘记分号
8) 语句末尾加分号. check 1point3acres for more.
我附上每一类对应的题号方便大家练习:
1) Computing ratio (分别计算numerator分子和denominator分母). Waral dи,
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拼接
1393. Capital Gain/Loss case when来定正负(+-1),再sum(). ----
1468. Calculate Salaries case when, round( , 0)
1212. Team Scores in Football Tournament 陷阱多,使用UNION ALL来包含重复项(尤其是需要sum()的时候不要忘了)
1907. Count Salary Categories 使用case when的话,不存在的分类不会显示null而是根本不会出现,所以要手动UNION
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-baidu 1point3acres
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
FROM (SELECT DISTINCT id, login_date FROM Logins) c
) d
LEFT JOIN Accounts a ON a.id = d.id
WHERE gap = 4
ORDER BY a.id