首页 > 数据库技术 > 详细

sql加强练习

时间:2016-11-05 12:22:32      阅读:218      评论:0      收藏:0      [点我收藏+]

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

name kecheng fenshu
张三 语文 81
张三 数学 75
李四 语文 76
李四 数学 90
王五 语文 81
王五 数学 100
王五 英语 90

代码:只要找出存在小于80的not in就好了

 

技术分享
-----------------------------------
use handsomecui
create table Grade(
    name varchar(5),
    course varchar(5),
    score int
)
-----------------------------------
bulk insert Grade from C:\Users\Administrator\Desktop\分数.txt
with(
    fieldterminator=\t,
    rowterminator=\n
)

select * from Grade
-----------------------------------
--查询所有成绩在80分以上的学生姓名
select distinct name from Grade where name not in(
    select distinct name from Grade where score<=80
)
-----------------------------------
View Code

 

2.学生表 如下:
自动编号 学号 姓名 课程编号 课程名称 分数
1 2005001 张三 0001 数学 69
2 2005002 李四 0001 数学 89
3 2005001 张三 0001 数学 69
删除除了自动编号不同,其他都相同的学生冗余信息

代码:相同信息,肯定要分组了

 

技术分享
-----------------------------------
create table Grade1(
    自动编号 int,
    学号 varchar(10),
    姓名 varchar(5),
    课程编号 varchar(5),
    课程名称 varchar(50),
    分数 int
)
-----------------------------------
bulk insert Grade1 from C:\Users\Administrator\Desktop\分数.txt
with(
    fieldterminator=\t,
    rowterminator=\n
)
select * from Grade1
-----------------------------------
--删除除了自动编号不同,其他都相同的学生冗余信息
delete Grade1 where 自动编号 not in (select min(自动编号) from Grade1 group by 学号,姓名,课程编号,课程名称,分数)
View Code

 

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

代码:1991,1992分组

 

技术分享
-----------------------------------
create table date1105(
    styear int,
    stmonth int,
    stamount float
)
-----------------------------------
bulk insert date1105 from C:\Users\Administrator\Desktop\分数.txt
with (
    fieldterminator=\t,
    rowterminator=\n
)
-----------------------------------
select styear,
(select stamount from date1105 m where stmonth=1 and m.styear=date1105.styear)as m1,
(select stamount from date1105 m where stmonth=2 and m.styear=date1105.styear)as m2,
(select stamount from date1105 m where stmonth=3 and m.styear=date1105.styear)as m3,
(select stamount from date1105 m where stmonth=4 and m.styear=date1105.styear)as m4
from date1105 group by styear
View Code

 

 4.复制表结构,以及复制表:

-------------------------------
select * from date1105
--复制表结构,不复制数据
select * into date1105_1 from date1105 where 1<>1
select * from date1105_1
drop table date1105_1
select styear a,stmonth b,stamount c into date1105_1 from date1105
select * from date1105_1
-------------------------------

5.三种Select改变列名的方法:

 
select sum(quantity) as 别名 from order_list

select sum(quantity) 别名 from order_list

select 别名1=sum(quantity),别名2=... from order_list
如果列的名字不确定的话,还可以用 case when then end 语句进行循环取列名的。

 

参考博客:http://www.cnblogs.com/lidabo/articles/2030972.html

欢迎大家访问handsomecui的博客:http://www.cnblogs.com/handsomecui/p/6032574.html

sql加强练习

原文:http://www.cnblogs.com/handsomecui/p/6032574.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!