sql经典查询语句
查询:
select * from table1 where ⼯资>2500 and ⼯资<3000 //查⼀个⼯资区间
select 姓名 from table1 where 性别='0' and ⼯资='4000' //查性别和⼯资的条件
select * from table1 where not ⼯资= 3200 //查⼯资不等于3200的
select * from table1 order by ⼯资desc //将⼯资按照降序排列
select * from table1 order by ⼯资 asc //将⼯资按照升序排列
select * from table1 where year(出⾝⽇期)=1987 //查询table1 中所有出⾝在1987的⼈
select * from table1 where name like '%张' /'%张%' /'张%' //查询1,⾸位字‘张’3,尾位字‘张’2,模糊查询
select * from table1 order by money desc //查询表1按照⼯资的降序排列表1 (升序为asc)
select * from table1 where brithday is null //查询表1 中出⾝⽇期为空的⼈
select * into table2 from table3 //将表3中的所有数据转换成表2 (相当于复制)
删库和建库
use 数据库(aa) //使⽤数据库aa
create bb(数据库) //创建数据库bb
create table table3 ( name varchar(10),sex varchar(2),money money, brithday datetime) //创建⼀个表3中有姓名,性别,⼯资,出⾝⽇期(此表说明有四列)
insert into table3 values ('张三','男','2500','1989-1-5') //在表中添加⼀⾏张三的记录
alter table table3 add tilte varchar(10) //向表3 中添加⼀列“title(职位)”
alter table table3 drop column sex //删除table3中‘性别’这⼀列
drop database aa //删除数据库aa
常用的sql查询语句有哪些drop table table3 //删除表3
delete * from table3 //删除table3 中所有的数据,但table3这个表还在
delete from table1 where 姓名='倪涛' and ⽇期 is null
delete from table1 where 姓名='倪涛' and ⽇期='1971'
更改库表的数据
update table3 set money=money*1.2 //为表3所有⼈⼯资都增长20%
update table3 set money=money*1.2 where title='经理' //为表3中“职位”是经理的⼈⼯资增长20%
update table1 set ⼯资= 5000 where 姓名='孙⼋' //将姓名为孙⼋的⼈的⼯资改为5000
update table1 set 姓名='敬光' where 姓名='倪涛' and 性别=1 //将性别为男和姓名为倪涛的⼈改为敬光
经典查询语句之⼆
1显⽰系部编号为03的系部名称
Select departname From department Where departno=’03’
2.查询系部名称中含有'⼯程'两个字的系部的名称。
Select departname From department Where departnamelike‘%⼯程%’
3查询共有多少个系部。
Select count(departno) From department
4查询'01'年级共有多少个班级。
Select count(*) From class Where classname like‘01%’
5查询在'周⼆晚'上课的课程名称和⽼师。
select couname as '课程名称',teacher as'教师' from course where schooltime='周⼆晚'
6查询姓张,陈,黄同学的基本信息,要求按照姓名降序排序。
select * from Student,StuCou,Course where student.StuNo=stucou.stuno uno and
StuName like'张%'or StuName like'陈%'or stuname like'黄%' order by StuName desc
7按系部统计课程平均报名⼈数,要求显⽰系部编号、平均报名⼈数。
Select departno,avg(willnum) From course Group by departno
8.按系部统计课程的平均报名⼈数,要求显⽰系部名称、平均报名⼈数。
Select departname,avg(willnum) From course,department Where department.departno=course.departno Group by departname
9.按课程统计平均报名⼈数,要求给出课程的详细信息和课程平均报名⼈数。
SELECT Kind '课程类别',AVG(WillNum)'每类平均报名⼈数' FROM Course group BY Kind haveing avg(willnum)
10.按课程统计平均报名⼈数,要求给出课程名称、平均报名⼈数,按照平均报名⼈数降序排序。
SELECT Kind '课程类别',AVG(WillNum)'每类平均报名⼈数' FROM Course,Department where Course.DepartNo=department.DepartNo group BY Kind
11.统计各系的班级数,要求显⽰系部名称、班级数量。
Select departname,count(classno) From department,class Where department.departno=class.departno Group by departname
12.查询“⽢蕾”同学选修的课程名、学分、上课时间和课程门数,按学分排序查询结果。
Select couname,credit,schooltime From student,course,stucou
Where student.stuno=stucou.stuno uno and Stuname=’⽢蕾’ Order by credit desc
13.按班级分组显⽰学⽣选课的信息,包括班级名、学号、姓名、课程、学分、上课时间。统计各班同学选修课程的总数。
select classname,stuname,student.StuNO,CouName,credit,schooltime from class,student,course,StuCou,Department
where Student.StuNo=StuCou.StuNo order by ClassName
14.查询统计各系开设选修课程的情况,内容包括系名、课程编号、课程名称、学分、教师、上课时间、限选⼈数,并统计各系开课门数。select uno,couname,credit,teacher,schooltime,limitnum from department,course
where department.departno=course.departno order by departname compute count(couname) by departname
15. 显⽰“00电⼦商务”班的选修报名结果,要求有姓名、课程名称、志愿号,并按姓名、志愿号排序。
Select stuname,couname,willorder From stucou,class,course,student
uno and student.classno=class.classno and stucou.stuno=student.stuno and classname=’00电⼦商务’Order by stuname,willorder
--1.查询报名⼈数⼤于等于30并且⼩于等于40的课程信息,要求显⽰课程名称和报名⼈数(使⽤BETWEEN…AND)
select [CouName],[WillNum]
from [dbo].[Course]
where [WillNum] between 30 and 40
--2.统计不同学分各有多少门课程。显⽰credit,count(*)字段。(group by)
select [Credit],count(*) '课程门数'
from [dbo].[Course]
group by [Credit]
--3.统计不同学分各有多少门课程,并显⽰课程详细信息。(compute ...by)
select *
from Course
order by Credit
compute count(couno) by credit
--4.查询周⼆上课的课程名称和教师名,并显⽰课程门数。(compute)
select CouName,Teacher
from dbo.Course
where SchoolTime like '周⼆%'
compute count(couname)
-
-5.查询平均报名⼈数⼤于30⼈的课程类别和每类平均报名⼈数。(group having)
select kind,AVG(willnum)
from Course
group by Kind
having AVG(willnum)>30
--6.在课程表中,按所开设课程的系部编号分别统计总共报名⼈数。(group having)
select [DepartNo],COUNT([WillNum])
from [dbo].[Course]
group by [DepartNo]
having COUNT([WillNum])>0
--7思考题:显⽰课程个数⽐⼯程技术类
-
-提⽰:分三步做
--1求⼯程技术类课程的个数=n
--select count(*)
--from course
--where kind='⼯程技术'
--2求课程个数⼤于n的课程的kind信息m。--select kind
--from course
--group by kind
--having count(kind)>n
--3求课程全部信息,且课程类型为m的。--(⼦查询)
--select * from course
-
-where kind=m
select *
from course
where kind in (select kind
from course
group by kind
having count(kind)>(select count(*)
from course
where kind='⼯程技术' )
)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论