select b.sname, c.sid , c.cname,c.score FROMresultAS c , student AS b where c.sid = b.sid and b.classid=1 AND c.cname="数学" ORDERBY c.score desc LIMIT 2
求各个科目前3名的学生(腾讯)
1 2 3 4 5 6 7 8 9 10 11 12
SELECT b.sname, c.sid, c.cname, c.score FROMresultAS c JOIN student AS b ON c.sid = b.sid WHERE ( -- 自查训 selectcount(distinct score) fromresultas r1 where r1.courseid = c.courseid and score>c.score ) <3 ORDERBY c.cname, c.score DESC;
求每个班级的总分
(腾讯)
1 2 3 4 5 6
SELECT b.classid ,sum(c.score) FROMresultAS c JOIN student AS b ON c.sid = b.sid groupby b.classid
求每门课程的最大分数(百度)
1 2 3 4
SELECT c.courseid ,max(c.score) FROMresultAS c JOIN student AS b ON c.sid = b.sid groupby c.courseid
1 2 3 4 5 6 7 8 9
SELECT c.sid , b.sname, sum(c.score) FROMresultAS c , ( select sid , sname from student where classid =1and sname LIKE"路%" ) as b where c.sid = b.sid groupby c.sid , b.sname
取总成绩最高的三位学生展示,输出:学号、总成绩(美
团)
1 2 3 4 5 6 7 8 9 10
SELECT c.sid , sum(c.score) as sc FROMresultAS c -- student as b -- where c.sid = b.sid groupby c.sid ORDERBY sc desc LIMIT 3
查询平均分低于80的
1 2 3 4 5 6 7 8 9
SELECT b.sname , b.sid , avg(c.score) as av FROMresultAS c , student as b where c.sid = b.sid groupby b.sid , b.sname HAVINGavg(c.score) <80
查询学生数量
1 2 3 4 5 6 7 8 9
SELECT b.classid , count(1) as tt FROMresultAS c , student as b where c.sid = b.sid groupby b.classid
求重复姓名
1 2 3 4 5 6 7 8 9 10 11 12 13
-- select b.sname -- from student as b -- WHERE b.sid in ( -- select sid -- from student -- where sname = b.sname -- and sid !=b.sid -- )
SELECT sname,COUNT(*)as ct FROM student WHERE classid=1 GROUPBY sname HAVING ct>1
求最少选了4个course
1 2 3 4
select s.student_id ,s.name ,count(s.course_id) as ct from t_student as s groupby s.student_id ,s.NAME having ct >=4
求低于69 家伙的平均分
1 2 3 4 5 6 7 8 9
select name , avg(course_score) as sc FROM t_student where student_id in ( SELECTDISTINCT student_id from t_student WHERE course_score <60) groupby name orderby sc desc
科目相同成绩学生
1 2 3 4 5 6 7
select a.name from t_student as a ,t_student as b where a.student_id=b.student_id and a.course_id !=b.course_id and a.course_score = b.course_score groupby a.name havingcount(a.name) >=2
都在80以上的
1 2 3 4 5
select a.student_id from t_student as a
groupby a.student_id havingmin(course_score)>=80
统计大于60的学生数量
1 2 3 4 5
selectcount(distinct id) as ct fromuser where age >18
年龄最大的在每个城市
1 2 3 4 5
select city ,MAX(age) fromuser groupby city
每个city的人数
1 2 3 4 5
select city ,count(id) fromuser groupby city
年龄段分层
使用分段函数,构造新的id18-29,然后再使用这个id来进行聚合
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
-- 年龄段分层 select CASE WHEN age>=0and age<18then '0-17' WHEN age>=18and age<29then '18-29' WHEN age>=30and age<59then '30-29' ELSE '60+' ENDas judge, count(*) fromuser groupby judge
出现最多的
1 2 3 4 5
select city ,count(*) as t fromuser groupby city ORDERBY t DESC limit 1
订单表(单表)
有个订单表t order,有id、money、country:字段
查找country是中国的,money倒序的第100条到第200条
记录(顺丰)
1 2 3 4 5
select* from t_order where country="中国" orderby money DESC limit 100offset99
使用offset自动移除前面的99个,然后限制查找100个
1 2 3 4
select* from A leftjoin B on A.tid = B.tid where b.tid =null
时间函数使用
1 2 3 4 5 6
select vid,count(*) as c from vedio where play_time > DATE_SUB(now(),INTERVAL1hour) GROUPBY vid orderby C desc limit 1