sql 练习
2013-01-25 12:29
85 查看
create table sc ( id int primary key, stuname varchar(20), course varchar(20), score int ) insert into sc values(1,'张三','语文',81) insert into sc values(2,'张三','数学',75) insert into sc values(3,'李四','语文',81) insert into sc values(4,'李四','数学',81) insert into sc values(5,'王五','语文',79) insert into sc values(6,'王五','数学',100) insert into sc values(7,'王五','英语',80) select distinct stuname from sc where score>=80 --查询出每门课都大于80分的学生姓名 select distinct A.stuname from sc A where A.stuname not in (select sc.stuname from sc where sc.score<80 ) select distinct stuname from sc where stuname not in (select distinct sc.stuname from sc where sc.score<80) insert into sc values(11,'张三','数学',75) insert into sc values(12,'张三','数学',75) insert into sc values(13,'张三','数学',75) select * from sc --查询id不同,其他字段都相同的记录 select * from sc as a where exists (select * from sc as b where a.id<>b.id and a.stuname=b.stuname and a.course=b.course and a.score=b.score) --删除除了Id不同其他都相同的记录 delete a from sc as a where stuname in ( select b.stuname from sc as b where a.id<>b.id and a.stuname=b.stuname and a.course=b.course and a.score=b.score) --删除除了Id不同其他都相同的记录,但保存Id最大的那条记录 delete sc where not exists( select 1 from sc as a where a.stuname=sc.stuname and a.course=sc.course and a.score=sc.score group by a.stuname,a.course,a.score having max(a.id)=sc.id) --统计某一列为某一个值的行数 select stuname,sum(case when course='语文' then 1 else 0 end) as '语文', sum(case when course='数学' then 1 else 0 end) as '数学' from sc group by stuname
相关文章推荐
- SQL 第七章 -- 各种练习
- SQL简单查询语句专项练习
- sql语句练习
- T-SQL :联接查询练习 (杂)
- SQL语句练习实例之四 找出促销活动中销售额最高的职员
- SQL练习之两个列值的交换
- SQL练习之求解填字游戏
- sql语句小练习二
- 关系代数和SQL练习(二)
- 【知识碎片】4:SQL练习,MPI编程
- Sql练习15题
- sql 练习(1)
- sql复习小练习
- SQL 数据小练习,呼叫中心 ----查询通话时间最长的5条记录 ----查询以0开头的通话总时,以秒为计算单位 ----查询2010年7月通话总时长最多的前两个呼叫员的编号 ----查询201
- oracle-sql分析练习
- 走向面试之数据库基础:一、你必知必会的SQL语句练习-Part 1
- Oracle练习:有关Oracle用户scott的sql练习
- sql练习
- 实验一、熟悉ORALCE环境并练习SQL操作
- SQL 图书借阅系统例题练习