ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

SqlServer基础训练

2021-05-08 16:32:35  阅读:277  来源: 互联网

标签:-- SqlServer dept 基础训练 Score where id Select


已知如下两个表
Create Table Department ( 
dept_id varchar(2) not null, -- 部门编号 
dept_name varchar(20) not null, -- 部门名称
dept_leader varchar(10) – 部门经理 ); 

Create Table Personnel ( 
id varchar(4) not null, --员工号 
name varchar(10) not null, --姓名 
dept_id varchar(2) not null, --部门编号 
age integer, --年龄 gzsj date, --参加工作时间
technical_post varchar(10), --职称 
salary integer – 薪水 );


写出表 Department 增加一条记录和更新一条记录的 SQL 语句,
增加记录值 (‘ 12’ , ‘ 研发部 ’ , ‘ 张三 ’ ) ;
更新 dept_id=’ 12’ 的记录 (‘ 12’ , ‘ 研发部 ’ , ‘ 张三新 ’ ) ;
insert into Department values(‘ 12’ , ‘ 研发部 ’ , ‘ 张三 ’);
update Department set dept_leader=’张三新’ where dept_id=’ 12’;

查找工资大于 2000元的员工记录,并按员工号 id 升序排列
select * from Personnel where salary>2000 order by id;

查找工资大于 2000元的员工所在部门、部门编号、部门经理、员工名称
select d.dept_name 所在部门,d.dept_id 部门编号, d.dept_leader 部门经理,p.name 员工名称 from  Personnel p inner join  Department d on p.dept_id=d.dept_id where p.salary>200;
查找张三和李四所在部门所有人员的姓名
select name from Personnel where dept_id in(select dept_id from Personnel where name =’张三’ or name=’李四’);

查看每个部门的部门经理和部门人数,按部门人数排序?
select d.dept_leader,count(p.id) from Department d left outer join Personnel p on p.dept_id=d.dept_id group by d.dept_leader order by count(p.id);

删除表 Department 中的所有记录
delete from Department;


//自建四张数据表--Student表、Course表、Score表和Teacher表(表中字段比较简单)

查询Student表中的所有记录的Sname、Ssex和Class列。
select Sname,Ssex,Class from student;

查询Score表中成绩在60到80之间的所有记录。
select * from Score where DEGREE between 60 and 80; 
或select * from Score where DEGREE>=60 and degree<=80;

查询和“李军”同性别的所有同学的Sname。
select sname from Student where SSEX=(select SSEX from student where sname='李军')

查询最低分大于70,最高分小于90的Sno列。
select sno from Score group by SNO having MIN(DEGREE)>70 and MAX(DEGREE)<90;

以Cno升序、Degree降序查询Score表的所有记录。
select * from Student order by CLASS desc;

查询“95031”班的学生人数。
select COUNT(*) from Student where CLASS='95031';

查询Score表中的最高分的学生学号和课程号。
select sno,cno,degree from Score where DEGREE=(select MAX(DEGREE) from Score);

查询‘3-105’号课程的平均分。
select AVG(DEGREE) from Score where CNO='3-105';

查询“男”教师及其所上的课程。
Select t.*,c.* from teacher t inner join Course c on
t.TNO= c.TNO where t.TSEX=’男’

查询所有未讲课的教师的Tname和Depart。
 Select Tname, Depart from Teacher where Tno not in 
(select Tno from Course)

查询选修某课程的同学人数多于5人的教师姓名
Select Tname from Teacher where Tno in (
Select Tno from Course where cno in (
Select cno from score group by cno having count(*)>5 )
)

查询教师所有的单位即不重复的Depart列。
Select distinct Depart from Teacher

查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
Select avg(DEGREE) from Score group by cno
 order by avg(DEGREE),cno desc

求选了课程的学生人数
Select count(distict sno) from Score

将“曾华”的班级调整为“95031”;
Update Student set class=’ 95031’
Where SNAME=’ 曾华’

删除“王丽”的考试成绩
Delect  from score where sno =(
Select sno from Student where sname=’ 王丽’
)

删除班级为“95031”班的所有学生。
Delete from Student where class=’ 95031’

查询Score表中数据电路科目分数大于80分的学生数量。
Select count(*) from score where cno=(
Select cno from Course where cname=’ 数据电路’
) and DEGREE>80

分组统计每个班级的学生人数。
Select count(*) from Student group by class

查询Student表中所有缺考的学生名单。
Select * from student where sno not in (
Select sno from Score
)

查询成绩低于平均分的所有同学的SNO。
Select sno from score where degree<( Select avg(degree) from score)

修改“数据电路”课程的成绩,给该科目的所有成绩加2分。
Update Score set Degree= Degree+2 where cno=(
  Select cno from course where cname=‘数据电路’
)

查询Score表中成绩在75到85之间(包含75和85)的所有记录。
Select * from score where degree between 75 and 85;

查询Student表中的班级为“95033”的所有男生的Sno、Sname和Class。
Select Sno,Sname,Class where Class=‘95033’and ssex=’男’

使用一条sql 语句查询Student表中重名的学生姓名。
Select count(*),sname from student group by sname
Having count(*)>1

批量删除编号101-109的学生信息。
  Delete from student where sno between 101 and 109

修改编号为108的学生课程编号为“3-105”课程的成绩为98分。
Update score set degree=98 where sno=108 and cno=‘3-105’

查询Score表中所有及格(不低于60分)的记录,并按成绩降序排序。
Select * from Score where degree>=60 order by degree desc

查询Score表中成绩在70到85之间(包含70和85)的所有记录。
Select * from score where degree between 75 and 85;

查询成绩高于平均分的所有同学的SNO。
Select sno from score where degree>(
Select avg(degree) from score
)
31)查询Score表中数据电路科目分数大于80分的学生数量。
Select count(*) from score where cno=(
Select cno from Course where cname=’ 数据电路’
) and DEGREE>80

查询employee表中根据id降序排序后第11到20的记录(主键id为自动增长列)。
Select top(10) * form employee where id not in (
Select top(10) id from employee 
) order by id desc

 

标签:--,SqlServer,dept,基础训练,Score,where,id,Select
来源: https://blog.csdn.net/qq_46127389/article/details/116531354

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有