sql基本操作实例(二)

一、练习题目(25道)

1、 查询Student表中的所有记录的Sname、Ssex和Class列。

select sname,ssex,class from student;

2、 查询教师所有的单位即不重复的Depart列。

select distinct depart from Teacher;

3、查询Student表的所有记录。

select * from student;

4、查询Score表中成绩在60到80之间的所有记录。

select * from score where degree>=60 and degree<=80;
//或者
select * from score where degree between 60 and 80;

5、查询Score表中成绩为85,86或88的记录。

select * from score where degree=85 or degree=86 or degree=88;
//或者
select * from score where degree in (85,86,88);

6、查询Student表中“95031”班或性别为“女”的同学记录。

select * from student where class='95031' or ssex='女';

7、以Class降序查询Student表的所有记录。

select * from student order by class desc;

8、以Cno升序、Degree降序查询Score表的所有记录。

select * from score order by cno asc,Degree desc;

9、查询“95031”班的学生人数。

select count(*) from student where class='95031';

10、 查询Score表中的最高分的学生学号和课程号。(子查询或者排序)

select  Sno,Cno from score where degree>= all(select degree from score);
//or
select  Sno,Cno from score where degree= (select max(degree) from score);
//or
Select top 1 sno,cno from score order by degree desc ;

11、查询每门课的平均成绩,要按照课程分组group by,然后求没门课平均avg

Select avg(degree),cno from score group by cno;

12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。

Select avg(degree),cno from score where cno like '3%' 
group by cno having count(*)>=5;

13、查询分数大于70,小于90的Sno列。

 from score where degree>70 and degree<90;

14、查询所有学生的Sname、Cno和Degree列。

Select sname,cno,degree from student as s,score
 where s.sno=score.sno;
//or
Select Sname,Cno,degree from score 
join student on score.sno=student.sno
//or
Select 
(select Sname from student where student.sno=score.sno),
cno,degree from score

15、查询所有学生的Sno、Cname和Degree列。

Select Sno,Cname,degree from score join course on score.cno=course.cno;

16、查询所有学生的Sname、Cname和Degree列。

Select Sname,Cname,degree from Student s,Course c,score sc 
where s.Sno=sc.sno and c.cno=sc.cno;

17、 查询“95033”班学生各门课的平均分。

Select avg(degree),cno from Student join score 
on student.sno=score.sno where class='95033' group by cno;

18、假设使用如下命令建立了一个grade表:

create table grade(low1  int,upp  int,rank1  char(1))
insert into grade values(90,100,’A’)
insert into grade values(80,89,’B’)
insert into grade values(70,79,’C’)
insert into grade values(60,69,’D’)
insert into grade values(0,59,’E’)

现查询所有同学的Sno、Cno和rank列。

select sno,cno,rank1 from score,grade 
where score.degree>= grade.low1 and score.degree<=grade.upp;

19、 查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的score记录。

select * from score where degree>(select degree from score 
where sno='109' and cno='3-105');

20、查询score中选学多门课程的同学中分数为非本人最高分成绩的记录。

select * from score where degree not in 
(select max(degree) from score sc1 group by sc1.sno)
and sno in 
(select sno from score group by Sno having count(*)>1);

21、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。

select * from score where degree>
(select degree from score where sno='109' and cno='3-105');

22、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。

select sno,sname,sbirthday from student 
where year(Sbirthday)=
(select year(sbirthday) from student where sno='108');

23、查询“张旭“教师任的学生成绩。

select degree from score s
where s.cno in
(select cno from teacher t,course c where t.tname='张旭' and c.tno=t.tno);

24、查询选修某课程的同学人数多于5人的教师姓名。

select tname from teacher,course
where teacher.tno= course.tno and course.cno in
(select cno from score group by cno having count(*)>5);

25、查询95033班和95031班全体学生的记录。

(select * from student where student.class='95033')
union
(select * from student where student.class='95031');
//or
Select * from student where Class=95033 or class='95031';

二、相关知识点

1.一般形式

SELECT column_name,column_name
FROM table_name
WHERE condition1
ORDER BY column_name,column_name ASC|DESC//order by 是按关键词排序
GROUP BY column_name//按关键词分组
HAVING condition1;//筛选组
//只有having和select后面可以跟着函数,如avg(),count(*),min(),max(),sum()等
SELECT *
FROM table_name
WHERE condition1
ORDER BY column_name,column_name ASC|DESC//order by 是按关键词排序
GROUP BY column_name//按关键词分组
HAVING condition1;//筛选组
//只有having和select后面可以跟着函数,如avg(),count(*),min(),max(),sum()等

2.特殊用法

SELECT DISTINCT column_name,column_name
FROM table_name
WHERE condition;//distinct只管一个,但后面的也可以加,表示不能重复
SELECT TOP number [percent] column_name(s)
FROM table_name
where ;//选取前几行的相应列
SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern;
//like后可以用%,例如张%,%配一个或多个字符
//也可以是张_,_是一个,可以连用

通配符使用

SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,...);
//在不在集合里,也可以用not in
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
//两边取等号

SQL别名

SELECT column_name AS alias_name
FROM table_name;//列别名
SELECT column_name(s)
FROM table_name AS alias_name;//表别名
//as可以省略,只用空格区分就可以了

3.多表联合嵌套

SELECT column_name,column_name
FROM table_name1,table_name2,...//可以是同一个列表,然后起别名区分,如果有相同的列一定要区分
WHERE condition1
ORDER BY column_name,column_name ASC|DESC//order by 是按关键词排序
GROUP BY column_name//按关键词分组
HAVING condition1;//筛选组
//只有having和select后面可以跟着函数,如avg(),count(*),min(),max(),sum()等
SELECT column_name AS alias_name
FROM table_name1 join table_name2
ON condition//默认为innerjoin,即不一致就去掉
WHERE;
//还有left join,right join,full join
//left join表示左边的即使不满足条件也要存在,full就是left and right
(Select from where) union (select from where)
//集合并

4.select的嵌套

select语句得到的是一个集合,因此可以将此一个select语句嵌套到需要集合的任何地方去

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值