SQL Server的简单查询命令

一、实验目的

  • 通过本次实验使学生初步掌握数据库中表的单表,多表查询操作。

二、实验内容

  1. 简单无条件查询
  2. 有条件查询
  3. 分组查询
  4. 模糊查询

三、实验环境

  1. Windows
  2. SQL Server

四、实验步骤及结果

SQL Server中是不区分大小写的

  1. 查询全体学生的详细记录
    select * 
        from Student
    go
  1. 查询全体学生的学号与姓名
    select Sno,Sname 
    	from Student
    go
  1. 查询全体学生的姓名、学号、所在系
    select Sno,Sname,Sdept 
    	from Student
    go
  1. 查询选修了课程的学生学号
    select Distinct Sno
    	from SC
    go
  1. 查全体学生的姓名及其出生年份
    select Sno,SageAge
    	from Student
    go
  1. 查询学生总人数
    select Count(*)人数
    	from Student
    go
  1. 查询选修了课程的学生人数
    select Count(distinct Sno)人数
    	from SC
    go
  1. 查询学号是 201815121 的学生的总分、平均分、最高分和最低分。
    select SUM(Grade)总分,
    	AVG(Grade)平均分,
    	MAX(Grade)最高分,
    	MIN(Grade)最低分
    	from SC
    	where Sno='201815121'
    go
  1. 查询计算机科学系全体学生的名单
    select Sname
    	from Student
    	where Sdept='CS'
    go
  1. 查询所有年龄在 20 岁以下的学生姓名及其年龄
    select Sname,SageBirthday
    	from Student
    	where Sage <20
    go
  1. 检索出年龄在 18-19 岁的学生姓名(两种方法)

      (1).select Sname
        	from Student
        	where Sage between 18 and 19
        go

     (2).select Sname
        	from Student
        	where Sage>=18 and 
        	Sage<=19
        go
  1. 查询年龄不在 18-19岁之间的学生姓名 (两种方法)
    (1).select Sname,SageBirthday
    		from Student
    		where Sage<18 or 
    		Sage>19
    	go

     (2).select Sname,SageBirthday
        	from Student
        	where Sage not between 18 and 19
        go

13 、 查询年龄不是18和19的学生的姓名和年龄 (两种方法)

    (1).select Sname,SageBirthday
    		from Student
    		where Sage !=18 and Sage !=19
    	go

    (2).select Sname,SageBirthday
    		from Student
    		where Sage <>18 and Sage <>19
    	go

14 、 查询信息系(IS)、数学系(MA)学生的姓名和性别

    select Sname,Ssex
    	from Student
    	where Sdept IN('IS','MA')
    go

15 、 查询所有不姓刘的学生姓名。

    select Sname
    	from Student
    	where Sname NOT LIKE '刘%'
    go

16 、 查询没有先行课的课程号和课程名

    select Cno,Cname
    	from Course
    	where Cpno is null
    go

17 、 显示年龄大于18岁的男女生人数,结果降序列

    select Ssex, COUNT(Ssex)人数
    	from student 
    	where  Sage> 18 
    	group by Ssex
    	order by COUNT(Ssex) desc 
    go

18 、 查询选修了2门以上课程的学生学号

	 select  Sno
    	from SC
    	group by Sno
    	Having COUNT(*)>2
    go

19 、 查询学生的学号、姓名、课程号和成绩

	select distinct Student.Sno,Sname,Cno,Grade 
		from Student,SC
    	where Student.Sno=SC.sno
	go

20 、 查询每一门课的间接先修课(即先修课的先修课)

    select First.Cno,Second.Cpno
    	from Course First,Course Second
    	where First.Cpno = Second.Cno
     go

21 、 查询除刘晨外其他与“刘晨”同龄的学生。

    select Sname
    	from Student
    	where Sage 
    	in(
    		select Sage 
    		from Student
    		where Sname='刘晨'
    		)
    	and Sname <>'刘晨'
    go

22 、 找出每个学生超过他选修课程平均成绩的课程号。

    select Sno,Cno
    	from SC x
    	where 
    	Grade>(
    		select AVG(Grade)
    		from SC y
    		where y.Sno=x.Sno
    		)
    go

23 、 查询其他系中比计算机科学某一学生年龄小的学生姓名和年龄

    select sname, Sage
    	from student 
   		where Sage 
	    <Any(
	    	select Sage 
    		from student 
    		where sdept='CS'
    		)
    	and sdept<>'CS'
    go

24 、 查询其他系中比计算机科学系所有学生年龄都小的学生姓名及年龄。

    select student.sname, Sage
    	from student 
    	where Sage 
    	<(
    		select min (Sage)
    		from student 
    		where sdept='CS'
    	)
    	and sdept<>'CS'

猜你喜欢

转载自blog.csdn.net/xujingran/article/details/83692681