多表查询

子查询

https://www.nowcoder.com/practice/55f3d94c3f4d47b69833b335867c06c1?tpId=199&tqId=1975673&ru=/exam/oj&qru=/ta/sql-quick-study/question-ranking&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26tab%3DSQL%25E7%25AF%2587%26topicId%3D199
  1. select
  2. device_id,
  3. question_id,
  4. result
  5. from
  6. question_practice_detail
  7. where
  8. device_id = (
  9. select
  10. device_id
  11. from
  12. user_profile
  13. where
  14. university = '浙江大学'
  15. )
  16. order by
  17. question_id asc
  1. select
  2. question_practice_detail.device_id,
  3. question_id,
  4. result
  5. from
  6. question_practice_detail
  7. inner join user_profile on question_practice_detail.device_id = user_profile.device_id
  8. where
  9. user_profile.university = '浙江大学'
  10. order by
  11. question_id asc

链接查询

统计每个学校的答过题的用户的平均答题数

https://www.nowcoder.com/practice/88aa923a9a674253b861a8fa56bac8e5?tpId=199&tags=&title=&difficulty=0&judgeStatus=0&rp=0&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26tab%3DSQL%25E7%25AF%2587%26topicId%3D199
  1. select
  2. u.university,
  3. round(
  4. count(u.device_id)/count(distinct u.device_id),4
  5. ) avg_answer_cnt
  6. from
  7. user_profile u
  8. join question_practice_detail q on u.device_id = q.device_id
  9. group by
  10. u.university
  11. order by
  12. u.university
  1. select
  2. university, round(count(1) / count(distinct(u.device_id)), 4) as avg_answer_cnt
  3. from
  4. user_profile u,
  5. question_practice_detail p
  6. where
  7. p.device_id = u.device_id
  8. group by
  9. university
  10. order by
  11. university

统计每个学校各难度的用户平均刷题数

https://www.nowcoder.com/practice/5400df085a034f88b2e17941ab338ee8?tpId=199&tags=&title=&difficulty=0&judgeStatus=0&rp=0&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26tab%3DSQL%25E7%25AF%2587%26topicId%3D199
  1. select
  2. u.university,
  3. q2.difficult_level,
  4. round(count(u.device_id) / count(distinct u.device_id), 4) avg_answer_cnt
  5. from
  6. user_profile u
  7. join question_practice_detail q on u.device_id = q.device_id
  8. join question_detail q2 on q2.question_id = q.question_id
  9. group by
  10. u.university,
  11. q2.difficult_level
  12. order by
  13. u.university
  1. select
  2. university,
  3. p1.difficult_level,
  4. round(count(1) / count(distinct(u.device_id)), 4) as avg_answer_cnt
  5. from
  6. user_profile u,
  7. question_practice_detail p,
  8. question_detail p1
  9. where
  10. p.device_id = u.device_id
  11. and p1.question_id = p.question_id
  12. group by
  13. university,
  14. p1.difficult_level
  15. order by
  16. university

统计每个用户的平均刷题数

https://www.nowcoder.com/practice/f4714f7529404679b7f8909c96299ac4?tpId=199&tqId=1975676&ru=%2Fpractice%2F5400df085a034f88b2e17941ab338ee8&qru=%2Fta%2Fsql-quick-study%2Fquestion-ranking&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26tab%3DSQL%25E7%25AF%2587%26topicId%3D199
  1. select
  2. u.university,
  3. q2.difficult_level,
  4. round(count(u.device_id) / count(distinct u.device_id), 4) avg_answer_cnt
  5. from
  6. user_profile u
  7. join question_practice_detail q on u.device_id = q.device_id
  8. join question_detail q2 on q2.question_id = q.question_id
  9. group by
  10. u.university,
  11. q2.difficult_level
  12. having
  13. u.university = '山东大学'
  1. select
  2. university,
  3. p1.difficult_level,
  4. round(count(1) / count(distinct(u.device_id)), 4) as avg_answer_cnt
  5. from
  6. user_profile u,
  7. question_practice_detail p,
  8. question_detail p1
  9. where
  10. p.device_id = u.device_id
  11. and p1.question_id = p.question_id
  12. group by
  13. university,
  14. p1.difficult_level
  15. having
  16. university = '山东大学'

组合查询

查找山东大学或者性别为男生的信息

https://www.nowcoder.com/practice/979b1a5a16d44afaba5191b22152f64a?tpId=199&tags=&title=&difficulty=0&judgeStatus=0&rp=0&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26tab%3DSQL%25E7%25AF%2587%26topicId%3D199
  1. select
  2. device_id,
  3. gender,
  4. age,
  5. gpa
  6. from
  7. user_profile
  8. where
  9. university = '山东大学'
  10. union all
  11. select
  12. device_id,
  13. gender,
  14. age,
  15. gpa
  16. from
  17. user_profile
  18. where
  19. gender = 'male'

条件函数

计算25岁以上和以下的用户数量

https://www.nowcoder.com/practice/30f9f470390a4a8a8dd3b8e1f8c7a9fa?tpId=199&tags=&title=&difficulty=0&judgeStatus=0&rp=0&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26tab%3DSQL%25E7%25AF%2587%26topicId%3D199
  1. select
  2. (case
  3. when age<25 then '25岁以下'
  4. when age is null then '25岁以下'
  5. else '25岁及以上' end) as age_cut,count(*) number
  6. from user_profile
  7. group by age_cut
  1. select
  2. (case
  3. when age>=25 then '25岁及以上'
  4. else '25岁以下' end) as age_cut,count(*) number
  5. from user_profile
  6. group by age_cut

查看不同年龄段的用户明细

https://www.nowcoder.com/practice/ae44b2b78525417b8b2fc2075b557592?tpId=199&tags=&title=&difficulty=0&judgeStatus=0&rp=0&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26tab%3DSQL%25E7%25AF%2587%26topicId%3D199
  1. select device_id,gender,
  2. (case
  3. when age<20 then '20岁以下'
  4. when age>=20 and age<25 then '20-24岁'
  5. when age>=25 then '25岁及以上'
  6. else '其他' end) as age_cut
  7. from user_profile

日期函数

计算用户8月每天的练题数量

https://www.nowcoder.com/practice/847373e2fe8d47b4a2c294bdb5bda8b6?tpId=199&tags=&title=&difficulty=0&judgeStatus=0&rp=0&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26tab%3DSQL%25E7%25AF%2587%26topicId%3D199
  1. select
  2. day(date) as day,
  3. count(1) as question_cnt
  4. from
  5. question_practice_detail
  6. where
  7. year(date) = 2021
  8. and month(date) = 8
  9. group by
  10. date
  1. select
  2. day(date) as day,
  3. count(1) as question_cnt
  4. from
  5. question_practice_detail
  6. group by
  7. date
  8. having
  9. (
  10. year(date) = 2021
  11. and month(date) = 8
  12. )