sql语句小练习

1、用一条SQL 语句 查询出每门课都大于80 分的学生姓名

name kecheng fenshu

张三 语文 81

张三 数学 75

李四 语文 76

李四 数学 90

王五 语文 81

王五 数学 100

王五 英语 90

A:select distinct name from table where name not in (select distinct name from table where fenshu<=80)     关键词 DISTINCT 用于返回唯一不同的值

B:select name from table group by name having min(fenshu)>80                     关键词 HAVING 可以与聚合函数(aggregate functions such as COUNT, SUM, AVG, MIN, or MAX)函数一起使用

C:select name from table group by name having count(kecheng)>=3 and min(fenshu)>=80

2、删除除了自动编号不同, 其他都相同的学生冗余信息

自动编号 学号 姓名 课程编号 课程名称 分数

1 2005001 张三 0001 数学 69

2 2005002 李四 0001 数学 89

3 2005001 张三 0001 数学 69

A: delete tablename where 自动编号 not in(select min( 自动编号) from tablename group by 学号, 姓名, 课程编号, 课程名称, 分数)

3、怎么把这样一个表儿

year month amount

1991 1 1.1

1991 2 1.2

1991 3 1.3

1991 4 1.4

1992 1 2.1

1992 2 2.2

1992 3 2.3

1992 4 2.4

查成这样一个结果

year m1 m2 m3 m4

1991 1.1 1.2 1.3 1.4

1992 2.1 2.2 2.3 2.4

A:select year,

(select amount from aaa m where month=1 and m.year=aaa.year) as m1,

(select amount from aaa m where month=2 and m.year=aaa.year) as m2,

(select amount from aaa m where month=3 and m.year=aaa.year) as m3,

(select amount from aaa m where month=4 and m.year=aaa.year) as m4

from aaa group by year

4、拷贝表( 拷贝数据, 源表名:a 目标表名:b)

A:insert into b(a, b, c) select d,e,f from a;

5、有一张表,里面有3个字段:语文,数学,英语。

大于或等于80表示优秀,大于或等于60表示及格,小于60分表示不及格。

显示格式:

语文 数学 英语

及格 优秀 不及格

A:select

(case when 语文>=80 then '优秀'

when 语文>=60 then '及格'

else '不及格') as 语文,

(case when 数学>=80 then '优秀'

when 数学>=60 then '及格'

else '不及格') as 数学,

(case when 英语>=80 then '优秀'

when 英语>=60 then '及格'

else '不及格') as 英语,

from table

6、表的创建修改删除

创建一张学生表,包含以下信息,学号,姓名,年龄,性别,家庭住址,联系电话

A:

Create table stu (学号 int ,

姓名 varchar(8),

年龄 int,

性别 varchar(4),

家庭地址 varchar(50),

联系电话 int

);

修改学生表的结构,添加一列信息,学历

Alter table stu add 学历 varchar(6);

修改学生表的结构,删除一列信息,家庭住址

Alter table stu drop column 家庭地址

向学生表添加如下信息:

学号 姓名年龄性别联系电话学历

1A22男123456小学

2B21男119中学

3C23男110高中

4D18女114大学

Insert into stu values(1,’A’,22,’男’,123456,’小学’)

Insert into stu values(2,’B’,21,’男’,119,’中学’)

Insert into stu values(3,’C’,23,’男’,110,’高中’)

Insert into stu values(4,’D’,18,’女’,114,’大学’)

修改学生表的数据,将电话号码以11开头的学员的学历改为“大专”

Update stu set 学历=’大专’ where 联系电话 like ‘11%’

删除学生表的数据,姓名以C开头,性别为‘男’的记录删除

Delect from stu where 性别=’男’ and 姓名 like ‘c%’

查询学生表的数据,将所有年龄小于22岁的,学历为“大专”的,学生的姓名和学号示出来

Select 姓名,学号 from stu where 年龄<22 and 学历=’大专’

查询学生表的数据,查询所有信息,列出前25%的记录

Select top 25 percent * from stu

查询出所有学生的姓名,性别,年龄降序排列

Select 姓名,性别 from stu order by 年龄 desc

按照性别分组查询所有的平均年龄

Select avg(年龄) from stu group by 性别

7、查询A(ID,Name)表中第31至40条记录,ID作为主键可能是不是连续增长的列,完整的查询语句如下:

A:select top 10 * from A where ID >(select max(ID) from (select top 30 ID from A order by A ) T) order by A

8、查询表A中存在ID重复三次以上的记录,完整的查询语句如下:

select * from(select count(ID) as count from table group by ID)T where T.count>3

9、说出以下聚合数的含义:avg ,sum ,max ,min , count ,count(*)

AVG:求平均值

SUM:求和

MAX:求最大值

MIN:求最小值

COUNT(*):返回所有行数

关键字含义

事务 Transaction 触发器 TRIGGER 继续 continue 唯一 unqiue

主键 primary key 标识列 identity 外键 foreign key 检查 check

约束 constraint

10、随机取出10条数据

select top 10 * from tablename order by newid()

11、查询平均成绩大于60分的同学的学号和平均成绩;

select stuId,avg(score)

from Scores

group by stuId having avg(score) >60;

12、子查询部分

A :with nickname1 as (sql1) ,nickname2 as (sql2) ...select * from nickname1,nickname2...

 https://www.toutiao.com/a6400624100788601090/

 

 

转载于:https://www.cnblogs.com/huasky/p/8308930.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值