SQL刷题每日总结

在投了无数次简历,看了无数公司对数据分析岗的jd后,我决定开始重新自学SQL。

其实我在本科时有系统学习过SQL,但是代码这个东西嘛,不用就忘了。在b站上看完了尚硅谷的SQL课程,收获自认还是不浅的,然后开开心心去面试滴滴,结果一面的SQL题就答错了😅。

于是我痛定思痛,意识到只有刷题才是学习SQL的唯一正解。从今天开始,我会把刷题过程中的一些笔记记录在动态里,希望刷完牛客的SQL题后能有所提升。

昨天和今天都刷了一个小时,拢共刷了非技术快速入门篇的30道题。错的点都是很基础的问题😭

1.取某些列用limit

limit m,n #从第m+1行开始总共取n行
limit n #从第1行开始总共取n行
limit n offset m #从第m行开始取n行

2.改列名用as

select device_id as user_infos_example from user_profile

3.SQL中符号不要用中文的,尤其是逗号之类的。

4.mySQL的between两边都包括。

现在运营想要针对20岁及以上且23岁及以下的用户开展分析,请你取出满足条件的设备ID、性别、年龄。

select device_id, gender, age 
from user_profile
where age between 20 and 23
#也可写作
select device_id,gender,age
from user_profile
where age >=20 and age<=23

5.题目:现在运营想要查看除复旦大学以外的所有用户明细,请你取出相应数据

select device_id,gender,age,university
from user_profile
\#where university != '复旦大学'
\#where university not like '复旦大学'
\# where university not in ('复旦大学')

但是where university is not '复旦大学'不对

6.现在运营想要找到gpa在3.5以上(不包括3.5)的山东大学用户 或 gpa在3.8以上(不包括3.8)的复旦大学同学进行用户调研,请你取出相应数据

法1 union

SELECT device_id, gender, age, university,gpa from user_profile where gpa > 3.8 and university = '复旦大学' **UNION**

SELECT device_id, gender, age, university,gpa from user_profile where gpa > 3.5 and university = '山东大学'

法2 or

SELECT 
  device_id, gender, age, university,gpa 
from 
  user_profile 
where 
  (gpa > 3.8 and university = '复旦大学') or (gpa > 3.5 and university = '山东大学')

7.题目:运营想要知道复旦大学学生gpa最高值是多少,请你取出相应数据

select max(gpa) from user_profile
where university ='复旦大学';

8.ROUND(AVG(gpa),1) 用以保留小数点

9.Where不能与合计函数同时使用

现在运营想查看每个学校用户的平均发贴和回帖情况,寻找低活跃度学校进行重点运营,请取出平均发贴数低于5的学校或平均回帖数小于20的学校。

错误代码:
select university, avg(question_cnt) as avg_question_cnt,
avg(answer_cnt) as avg_answer_cnt
from user_profile
where avg_question_cnt<5 or avg_answer_cnt
group by university
正确代码:
SELECT
    university,
    avg(  question_cnt  )  AS  avg_question_cnt,
    avg(  answer_cnt  )  AS  avg_answer_cnt  
FROM
    user_profile  GROUP BY university  
HAVING
    avg_question_cnt  <  5  OR  avg_answer_cnt  <  20

注意:

  1. WHERE 关键字无法与合计函数一起使用;
  2. SQL语句执行顺序 摘自《MySQL技术内幕:SQL编程》FROM - ON - JOIN - WHERE - GROUP BY - WITH - HAVING - SELECT - DISTINCT - ORDER BY - LIMIT

10.SQL中or自带去重,不去重用union

现在运营想要分别查看学校为山东大学或者性别为男性的用户的device_id、gender、age和gpa数据,请取出相应结果,结果不去重。

错误作法:
select device_id, gender, age, gpa
from user_profile
where university='山东大学' 
or gender='male'##or自带去重效果
正确做法:
select device_id, gender, age, gpa
from user_profile
where university='山东大学' 
union all
select device_id, gender, age, gpa
from user_profile
where gender='male'

11.if的用法

本题注意:age为null 也记为 25岁以下

方法一:
select (case when age>=25 then '25岁及以上' else '25岁以下' end) as age_cut, 
count(device_id) as number
from user_profile
group by age_cut


方法二:
SELECT 
IF(age>=25,'25岁及以上','25岁以下') AS age_cut, 
COUNT(device_id) AS number 
FROM user_profile 
GROUP BY age_cut 

方法三:
select '25岁以下' as age_cut,count(device_id) as number
from user_profile
where age<25 or age is null
union all
select '25岁及以上' as age_cut,count(device_id) as number
from user_profile
where age>=25;

12.case when end 的用法

现在运营想要将用户划分为20岁以下,20-24岁,25岁及以上三个年龄段,分别查看不同年龄段用户的明细情况,请取出相应数据。(注:若年龄为空请返回其他。)

select device_id, gender, 
case 
    when age<20 then '20岁以下'
    when age between 20 and 24 then '20-24岁'
    when age>24 then '25岁及以上'
    else '其他'
end 
as age_cut
from user_profile;

全部评论
好棒
点赞 回复
分享
发布于 01-29 17:16 江西
厉害啊
点赞 回复
分享
发布于 01-30 09:36 山东
滴滴
校招火热招聘中
官网直投
点赞 回复
分享
发布于 03-18 09:45 吉林

相关推荐

6 20 评论
分享
牛客网
牛客企业服务