--1.查询语文成绩前两名的学生名称 select top(2) * from grade where subject = '语文' order by score desc;--2.查询语文大于60的学生名称select * from grade g,student s where g.s_id = s.id and g.subject = '语文' and g.score > 60;--3.查询学生id为1的学生的分数平均值 (各个科目总分/科目)select avg(score) from grade g where g.s_id = 1--4.查询学生的语文成绩,并按照倒序排名select * from grade g where subject = '语文' order by score asc;--5.查询科目70分以上的学生名称还有年龄-- 1. 查询的表有哪些 grade student-- 2.确定要查询的字段student.name studen.age-- 3.确定筛选条件-- a:grade.score > 70-- 确定关联关系 学生表.id = 成绩表的.stu_idselect s.name,s.age,g.score from grade g, student s where s.id= g.s_id and g.score > 70
-- 使用子查询-- in-- 查询姓名是老子 孟子 孔子-- student-- name = 老子 孟子 孔子select * from student where name = '老子' or name = '孟子' or name = '孔子'select * from student where name in ('老子','孟子','孔子');-- exists判断是否存在 然后执行-- sqla where exists (sqlb)-- 如果sqlb能查询到数据 就返回true 然后就执行sqla-- 如果sqlb查询不到数据 就返回flase 就不执行select * from student where EXISTS (select * from student where id = 1)--嵌套子查询 当一个查询的条件依赖与另一个查询的结果-- 查询成绩大于语文60分学生名称select s_id from grade where score > 60 and subject = '语文';select * from student where id in(select s_id from grade where score > 60 and subject = '语文')-- 查询平均分大于分的学生select avg(score) from grade; -- 查询平均分select s_id from grade where score >(select avg(score) from grade )-- 查询大于平均分的select * from student where id in(select s_id from grade where score >(select avg(score) from grade ))select * from student where id in (2,1,1) -- in查询是带去重的