简单SQL语句
简单的Sql查询语句,很实用的哦!!
1、查询<学生信息表>,查询学生"张三"的全部基本信息
Select *
from A_studentinfo
where sname='张三'
2、查询<学生信息表>,查询学生"张三"和”李四”的基本信息
Select *
from A_studentinfo
where sname='张三'
or sname='李四'
3、查询<学生信息表>,查询姓"张"学生的基本信息
Select *
from A_studentinfo
where sname like '张%'
4、查询<学生信息表>,查询姓名中含有"四"字的学生的基本信息
Select *
from A_studentinfo
where sname like '%四%'
5、查询<学生信息表>,查询姓名长度为三个字,姓“李”,且最后一个字是“强”的全部学生信息。
select *
from A_studentinfo
where sname like '李_强'
6、查询<学生信息表>,查询姓"张"或者姓”李”的学生的基本信息。
Select *
from A_studentinfo
where sname like '张%'
or sname like '李%'
7、查询<学生信息表>,查询姓"张"并且"所属省份"是"北京"的学生信息
Select *
from A_studentinfo
where sname like '张%'
and province='北京'
8、查询<学生信息表>,查询"所属省份"是"北京"、”新疆”、”山东”或者"上海"的学生的信息
Select *
from A_studentinfo
where province in ('北京','上海','新疆','山东')
9、查询<学生信息表>,查询姓"张",但是"所属省份"不是"北京"的学生信息
Select *
from A_studentinfo
where sname like '张%'
and province !='北京'
10、查询<学生信息表>,查询全部学生信息,并按照“性别”排序,性别相同的情况下按照“所属省份”排序,所属省份相同的情况下再按照“班级”排序
select *
from A_studentinfo
order by sex,province,class
11、查询<学生信息表>,查询现有学生都来自于哪些不同的省份
select distinct province as省份
from A_studentinfo
Select *
from A_studentcourse
where score is null
Select *
from A_studentcourse
where score is not null
order by score desc
1、统计<学生信息表>,统计共有多少个学生
Select count (*) as学生数量
from A_studentinfo
Select count(*) as学生数量
from A_studentinfo
where (2008-yearofbirth)>20
select count(*) as学生数量
from A_studentinfo
where enrollment between '1998-01-01' and '2003-12-30'
select count(*) as学生数量
from A_studentinfo
where enrollment between '1998' and '2003'
Select avg(score) as平均成绩
from A_studentcourse
where sno='S001'
select sum(score) as总成绩
from A_studentcourse
where sno ='S001'
select max(score) as最高成绩
from A_studentcourse
where cno='C001'
select 2008-min(yearofbirth) as最大年龄
from A_studentinfo
1、统计<学生选修信息表>,统计每个课程的选修人数
select cno,count(*) as学生数量
from A_studentcourse
group by cno
select sno,sum(score) as总成绩
from A_studentcourse
group by sno
select class as班级,sex as性别, count(*) as人数
from A_studentinfo
group by class,sex
order by class
Select cno,avg(score) as平均成绩
from A_studentcourse
group by cno
order by avg(score) desc
Select sno as不及格学生学号
from A_studentcourse
where score<60
group by sno
having count(*)>1
select class as班级, 2008-min(yearofbirth) as最大年龄
from A_studentinfo
group by class
1、用子查询实现,查询选修“高等数学”课的全部学生的总成绩
select sum(score) as高等数学总成绩
from A_studentcourse
where cno=
(
select cno
from A_courseinfo
where subject='高等数学'
)
2、用子查询实现,统计<学生选修信息表>,显示学号为"S001"的学生在其各科成绩中,最高分成绩所对应的课程号和成绩
select score,cno
from A_studentcourse
where sno='S001'
and score =
(
select max(score)
from A_studentcourse
where sno ='S001'
)
思考:如果该学号学生有两个课程分数都为最高的100分,查询会有什么结果
select sum(score) as数据库技术总成绩
from A_studentcourse
where cno =
(
select cno
from A_courseinfo
where subject='数据库技术')
and sno in
(
select sno
from A_studentinfo
where class='2'
)
select score
from A_studentcourse
where cno=
(
select cno
from A_courseinfo
where subject='测试管理'
)
and sno in
(
select sno
from A_studentinfo
where class='3'
and sname='张三'
)
1、查询"张三"的各科考试成绩,要求显示姓名、课程号和成绩
select sname as姓名,cno as课程号,score as成绩
from A_studentinfo,A_studentcourse
where A_studentinfo.sno=A_studentcourse.sno
and sname='张三'
select sname as姓名,cno as课程号,score as成绩
from A_studentinfo,A_studentcourse
where A_studentinfo.sno=A_studentcourse.sno
and sname='张三'
and score is null
select sname as姓名,subject as课程名称,score as成绩
from A_studentinfo,A_courseinfo,A_studentcourse
where A_studentcourse.sno=A_studentinfo.sno
and A_studentcourse.cno=A_courseinfo.cno
and A_studentinfo.sname='张三'
select sname as姓名,score as成绩
from A_studentcourse,A_courseinfo,A_studentinfo
where A_studentcourse.cno=A_courseinfo.cno
and A_studentcourse.sno=A_studentinfo.sno
and subject='测试管理'
and class='3'
and sname='张三'
select class as班级,avg(score) as男生平均成绩
from A_studentcourse,A_courseinfo,A_studentinfo
where A_studentcourse.cno=A_courseinfo.cno
and A_studentcourse.sno=A_studentinfo.sno
and sex='
TAG: