简单查询
1.查询课程表中的课程号和Cname。
select Cid,Cname from C;2.查询学生表中的全体学生记录。
select*from S;3.查询学生表的全体学生的姓名和入校年份。
select sname,year(entrydate)from S;4.查询学生表中全体学生的姓名及入校年份,为无标题列加标题。
select sname,year(entrydate)'入校年份'from S;
或者:
select sname, DATEPART(yy,entrydate)as'入校年份'from S;5.查询学生表中全体学生的姓名、性别及出生年份,添加标题为birth
select S.sname,S.sex,year(S.birthday)'birth'from S
6.查询选修了课程的学生学号。
selectdistinct Sid from SC;7.查询教师表中的前条记录。
selecttop3*from T;8.查询课程表中的前30%的记录。
selecttop30percent*from C;9.查询工龄小或等于35岁的教师信息。
select*from T
whereyear(getdate())-year(workingtime)<=35;
或者:
select*from T
where datediff(year,workingtime,getdate())<=35;10.查询工作时间在1990-2003年的教师编号、姓名和工作时间。
select Tid,name,workingtime
from T
whereyear(workingtime)between1990and2003;
或者:
select Tid,name,workingtime from T
where T.workingtime between'1990.1.1'and'2003.12.31'
或者
select Tid,name,workingtime from T
where DATEPART(yy, workingtime)between1990and2003;select Tid,name,workingtime from T
where DATEPART(yy, workingtime)>=1990and DATEPART(yy, workingtime)<=2003;11.查询计算机系、软件系的教师。
select*from T where department='计算机系'unionselect*from T where department='软件'
或者:
select*from T where department='计算机系'or department='软件'
或者:
select*from T where department in('计算机系','软件');12.查询学生表中姓张的学生的信息。
select*from S
where sname like'张%';
或者:
select*from S whereleft( sname,1)='张'13.查询课程表的“C语言”的课程号。
select Cid from C where Cname='C语言';14.查询选课表中没有得分的考生号。
select Sid from SC where score isnull;15.查询男教师的基本信息,按职称降序排列
select*from T
where sex='男'orderby title Desc;16.查询教师表中信息,按职称降序排序,工作时间升序排序。
select*from T
orderby title Desc,workingtime asc;17.查询学生表中学生总数。
select'学生总数',count(*)from S;18.查询学生的平均年龄。
selectavg(age)as'平均年龄'from S;19.查询选课表中课程选课人数3人以上的各个课程号和相应的选课人数。
select Cid,count(Sid)from SC
groupby Cid
havingcount(*)>3;20.创建选课表的一个副本。
select*into copyC
from SC;21.查询姓张或姓刘的学生信息,按姓名降序排序。
select*from S
where sname like'刘%'or sname like'张%'orderby sname desc;
或者:
select*from S
where sname like'[张刘]%'orderby sname desc;
或者
select*from S whereleft(sname,1)in('张','刘')orderby sname desc22.查询选课表中每个学生的总分数,并按分数降序排序。
select Sid,sum(score)as'总分'from SC
groupby Sid
orderbysum(score)desc;23.查询学生的学号、姓名、性别、入学年份、年级(学号前4位)、专业(学号第5-6))SELECT Sid,sname,sex, DATEPART(yy,entrydate)AS'入学年份,
left(Sid,4) AS '年级' ,SUBSTRING(Sid,5,2) as '专业' FROM S
24.查询含有db_课程信息,(-默认为通配符,这里取消转移用escape)
select * from C where Cname like 'db\_%' escape '\'
简单查询1.查询课程表中的课程号和Cname。select Cid,Cname from C;2.查询学生表中的全体学生记录。select * from S;3.查询学生表的全体学生的姓名和入校年份。select sname,year(entrydate) from S;4.查询学生表中全体学生的姓名及入校年份,为无标题列加标题。select sname,year(entrydate) '入校年份'from S;或者:select sname, DATEPART(yy,entr