千家信息网

SQLserver运维必备:T-SQL语句练习

发表于:2025-02-07 作者:千家信息网编辑
千家信息网最后更新 2025年02月07日,小生博客:http://xsboke.blog.51cto.com-------谢谢您的参考,如有疑问,欢迎交流案例一:表如下图所示实验需求:1.在products表中查询出厂日期晚于2014年4月的
千家信息网最后更新 2025年02月07日SQLserver运维必备:T-SQL语句练习

小生博客:http://xsboke.blog.51cto.com



-------谢谢您的参考,如有疑问,欢迎交流


案例一:表如下图所示

实验需求:

1.products表中查询出厂日期晚于20144月的水果信息

语句:select * from products where 出厂日期>'2014-04-30' and 种类='水果'

2.products表中分组查询所有水果、蔬菜、坚果的总成本

语句:select 种类,SUM(成本) as 总成本from products group by 种类

3.products表中查询所有水果的信息,并按照成本从高到低的顺序显示结果

语句:select * from products where 种类='水果' order by 成本 desc

4.products表中查询成本在1-5元之间的蔬菜信息

语句:select * from products where 种类='蔬菜' and 成本 between 1 and 5

5.products表中所有水果的名称、种类和出厂日期信息插入到新表products_new

语句:select 名称,种类,出厂日期 intoproducts_new from products where 种类='水果'

select *

from products

where出厂日期>='2011-04-01' and 种类='水果'

select SUM(成本) as 总成本,种类

from products

group by 种类

select *

from products

where种类='水果'

order by 成本 desc.

select *

from products

where种类='蔬菜' and 成本 between '1' and'5'

select名称,种类,出厂日期

into products_new

from products

where种类='水果'

案例二:表如下图所示

实验需求:

1、products表和sales表中查询产品的名称、种类、成本、销售地点和销售价格。

语句:select products.名称,products.种类,products.成本,sales.销售地点,sales.销售价格

From products,sales

Where products.名称=sales.名称

或者:select a.名称,a.种类,a.成本,b.销售地点,b.销售价格

From products as a inner join sales as b on a.名称=b.名称

2、products表和sales表中查询销往海南的产品名称、种类、成本和销售价格。

语句:select products.名称,products.种类,products.成本,sales.销售价格

From products,sales

Where products.名称=sales.名称 and 销售地点='海南'

select products.名称,products.种类,products.成本,sales.销售价格,sales.销售地点

from products inner join sales

on products.名称=sales.名称

select products.名称,products.种类,products.成本,sales.销售价格,sales.销售地点

from products inner join sales

on products.名称=sales.名称

where销售地点='海南'

案例三:表如下图所示

实验需求:

1、查询王明的成绩?结果:王明 90

Select 姓名,成绩 from student where 姓名='王明'

2、给王明加2分?

Update student set 成绩=成绩+2 where 姓名='王明'

3、查询备注不为空的学生?

Select * from student where 备注 is not null

4、查询成绩大于95分以上的学生?

Select * from student where 成绩>95

5、王明转学,把王明删掉?

Delete from student where 姓名='王明'

6、来了一位新同学,名字叫于美丽?

Insert into student values(20,'于美丽','110110110',3,100,'勤奋好学')

7、查询成绩大于95分,并且班级是2班的都有谁?

Select * from student where 成绩>95 and 所在班级=2

8、查询成绩大于95分,并且班级是12班的都有谁?

Select * from student where 成绩>95 and 所在班级 between1 and 2

9、查询平均成绩?

Select avg(成绩) as 平均成绩 from student

10、使用truncate删除所有的数据

Truncate table student

select成绩

from [student-1]

where姓名='王明'

update [student-1]

set成绩=成绩+2

where姓名='王明'

select *

from [student-1]

where备注 is not null

select *

from [student-1]

where成绩>'95'

delete

from [student-1]

where姓名='王明'

insert into [student-1]

(姓名) values ('于美丽')

select *

from [student-1]

where成绩>='95' and 所在班级='2'

select *

from [student-1]

where成绩>='95' and 所在班级 between '1' and'2'

select AVG(成绩) as 平均成绩

from [student-1]

truncate table Student

实验案例四:表如下图所示

实验需求:

1、显示表中全部内容

select * from 员工信息表

2、显示工资大于5000员工的所有信息

select * from 员工信息表

where 工资>=5000

3、显示所有在1950到1988年之间出生员工的所有信息

select * from 员工信息表

where 出生日期>='1950'and 出生日期<='1988'

4、查询出所有住在昌平的员工

select * from 员工信息表

where 家庭住址 like '%昌平%'

5、所有销售员的工资涨幅1000

update 员工信息表

set 工资=工资+1000

where 职位='销售员'

6、删除工资小于3000员工的记录

delete from 员工信息表

where 工资<=3000

7、所有经理的工资下调10%

update 员工信息表

set 工资=工资*0.9

where 职位 like '%经理'

select *

from员工信息表

select *

from员工信息表

where工资>='5000'

select *

from员工信息表

where出生日期 between '1950' and'1988'

select *

from员工信息表

where职位 like '%经理%'

update员工信息表

set工资=工资+1000

where职位='销售员'

delete

from员工信息表

where工资<'3000'

update员工信息表

set工资=工资*0.9

where职位 like '%经理'

案例五:表如下图所示

1、查询年龄大于35,小于 50,并且职位是员工?

语句:select * from 员工信息表

where 年龄>35 and 年龄<50 and 职位='员工'

2、查询员工的平均年龄?

语句:select AVG(年龄) as 平均年龄 from 员工信息表

3、查询各种职位的平均年龄,并且按照每种职位的平均年龄降序排序?

语句:select 职位,AVG(年龄) as 平均年龄 from 员工信息表

group by 职位

order by AVG(年龄) desc

4、查询年龄最大和最小的人是谁?

语句:select 员工姓名,年龄 from 员工信息表

where 年龄=(select MAX(年龄)

from 员工信息表)

5、查询每种职位有多少人?并且按每种职位的人数升序排序?

语句:select 职位,COUNT(员工id) as 人数

from 员工信息表

group by 职位

6、查询年龄大于等于50岁和小于30岁的都有哪些人?

语句:select * from 员工信息表

where 年龄>=50 or 年龄<30

select *

from [员工信息表-2]

where职位='员工' and 年龄 between '35' and'50'

select AVG(年龄) as 平均年龄

from [员工信息表-2]

select AVG(年龄) as 平均年龄,职位

from [员工信息表-2]

group by 职位

order by AVG(年龄) desc

select年龄,员工姓名

from [员工信息表-2]

where年龄=( select MAX(年龄) from [员工信息表-2])

or年龄=( select MIN(年龄) from [员工信息表-2])

select COUNT(职位) as 人数,职位

from [员工信息表-2]

group by 职位

order by COUNT(职位) asc

select *

from [员工信息表-2]

where年龄>='50' or 年龄<='30'

案例六:表如下图所示

实验需求:

1、两个表查询

select 学生表.姓名,学生表1.电话,学生表1.家庭住址

from 学生表,学生表1

where 学生表.学生id=学生表1.学生id

2、三个表的查询

select 学生表.姓名,课程表.课程名,成绩表.分数

from 学生表,课程表,成绩表

where 学生表.学生id=成绩表.学生id AND 成绩表.课程ID=课程表.课程ID

3、合并两个结果集

SELECT 姓名,学号,班级,国籍

FROM 二班

UNION

select +' '+,学号,班级,国籍

from 一班

order by 班级 desc,学号 asc

4、内联接(查找参加考试的学生的姓名和CNT成绩,电话)

select s.姓名,s.电话,c.CNT

FROM 学生基本信息表 AS S INNER JOIN 学生成绩表 AS C

ON S.姓名=C.姓名

5、左外连接(查找本班学生的姓名和CNT才成绩,电话)

select s.姓名,s.电话,c.CNT

FROM 学生基本信息表 AS S LEFT OUTER JOIN 学生成绩表 AS C

ON S.姓名=C.姓名

6、右外连接(查找参加本次考试的学生的姓名和CNT才成绩,电话)

select s.姓名,s.电话,c.CNT

FROM 学生基本信息表 AS S RIGHT OUTER JOIN 学生成绩表 AS C

ON S.姓名=C.姓名

7、完全连接(查找所有学生的考试情况)

select s.姓名,s.电话,c.CNT

FROM 学生基本信息表 AS S FULL OUTER JOIN 学生成绩表 AS C

ON S.姓名=C.姓名

8、自连接(查找每个员工的上司姓名)

SELECT Y.员工姓名,s.员工姓名 AS 上司姓名

from 员工信息表 as y inner join 员工信息表 as s

on y.上司ID=S.员工ID

9、子查询(查找每个季度的生产数量,百分比)

select 季度,sum(生产数量) AS 每个季度的生产数量,

str((sum(生产数量)/(select sum(生产数量) from 生产表))*100)+'%' AS 百分比

from 生产表

group by 季度

order by 季度

10、在where子句中使用SELECT ,查找课程ID=1并且成绩〉80的学生

select 学生表.姓名

from 学生表

where 80<(select 分数 from 成绩表

where 学生表.学生ID=成绩表.学生ID AND 成绩表.课程ID=1)

11、交叉连接

SELECT T.教师姓名,c.课程名

from 教员基本信息表 AS t cross join 课程表 AS C



0