习题资料:
1. Mysql经典练习题50题

  1. SQL大厂面试真题

1.习题一

image.png

select user_id ,count(distinct(artcle_id)) as count
from article
where view_time=”2021-01-13”
group by user_id
having count>100
order by count desc
limit 10;

2.排行表

1.分数排名
2.在MySQL中实现Rank高级排名函数

解法一:

  1. SELECT
  2. S1.score 'Score',
  3. COUNT( DISTINCT S2.score ) 'Rank'
  4. FROM
  5. Scores S1
  6. INNER JOIN Scores S2
  7. ON S1.score <= S2.score
  8. GROUP BY
  9. S1.id, S1.score
  10. ORDER BY
  11. S1.score DESC;

先观察这条sql的执行结果!

select *
FROM Scores S1
         INNER JOIN Scores S2
                    ON S1.score <= S2.score
ORDER BY S1.id, S1.score

按S1.id, S1.score进行分组,这样可以保证相同分数也不会分到一组,因为id肯定是不一样的(其实只按id分组效果也一样!)