常用且有用的sql练习(还在更新中。。。)

                                           今天也要努力学习

sql语句的掌握对于从事数据工作或者开发工作又或者是无关人员都是非常重要的  ,因为多掌握一门技术你就有机会做更多的事,哈哈。

我练习的数据库是mysql(数据库都是大同小异的)

  提供练习的数据:
  --学生表
CREATE TABLE `Student`(
    `s_id` VARCHAR(20),
    `s_name` VARCHAR(20) NOT NULL DEFAULT '',
    `s_birth` VARCHAR(20) NOT NULL DEFAULT '',
    `s_sex` VARCHAR(10) NOT NULL DEFAULT '',
    PRIMARY KEY(`s_id`)
);
  --课程表
CREATE TABLE `Course`(
    `c_id`  VARCHAR(20),
    `c_name` VARCHAR(20) NOT NULL DEFAULT '',
    `t_id` VARCHAR(20) NOT NULL,
    PRIMARY KEY(`c_id`)
);
  --教师表
CREATE TABLE `Teacher`(
    `t_id` VARCHAR(20),
    `t_name` VARCHAR(20) NOT NULL DEFAULT '',
    PRIMARY KEY(`t_id`)
);
  --成绩表
CREATE TABLE `Score`(
    `s_id` VARCHAR(20),
    `c_id`  VARCHAR(20),
    `s_score` INT(3),
    PRIMARY KEY(`s_id`,`c_id`)
);
  --插入学生表测试数据
insert into Student values('01' , 'wuyue' , '1997-01-01' , '男');
insert into Student values('02' , '你' , '1997-12-21' , '男');
insert into Student values('03' , '我' , '1997-05-20' , '男');
insert into Student values('04' , '他' , '1997-08-06' , '男');
insert into Student values('05' , '隔壁的wuyue' , '1997-12-01' , '女');
insert into Student values('06' , '隔壁的你' , '1997-03-01' , '女');
insert into Student values('07' , '隔壁的我' , '1987-07-01' , '女');
insert into Student values('08' , '隔壁的他' , '1997-01-20' , '女');
--课程表测试数据
insert into Course values('01' , '体育' , '02');
insert into Course values('02' , '影视' , '01');
insert into Course values('03' , '音乐' , '03');

  --教师表测试数据
insert into Teacher values('01' , '科比');
insert into Teacher values('02' , '胡歌');
insert into Teacher values('03' , '周杰伦');

  --成绩表测试数据
insert into Score values('01' , '01' , 80);
insert into Score values('01' , '02' , 90);
insert into Score values('01' , '03' , 99);
insert into Score values('02' , '01' , 70);
insert into Score values('02' , '02' , 60);
insert into Score values('02' , '03' , 80);
insert into Score values('03' , '01' , 80);
insert into Score values('03' , '02' , 80);
insert into Score values('03' , '03' , 80);
insert into Score values('04' , '01' , 50);
insert into Score values('04' , '02' , 30);
insert into Score values('04' , '03' , 20);
insert into Score values('05' , '01' , 76);
insert into Score values('05' , '02' , 87);
insert into Score values('06' , '01' , 31);
insert into Score values('06' , '03' , 34);
insert into Score values('07' , '02' , 89);
insert into Score values('07' , '03' , 98);

 

开始练习:
-- 1查询"01"课程比"02"课程成绩高的学生的信息及课程分数    
select a.*,b.c_id,b.s_score,c.c_id,c.s_score
from student a
LEFT JOIN score b on a.s_id = b.s_id and b.c_id='01'
left JOIN score c on a.s_id = c.s_id and c.c_id='02'
WHERE b.s_score > c.s_score

测试结果:

-- 2查询平均成绩大于等于60的学生编号和学生姓名和学生成绩

select student.s_id,student.s_name,Round(AVG(score.s_score),2) avgscore
from student,score
WHERE score.s_id=student.s_id
group by student.s_id,student.s_name
HAVING AVG(score.s_score)>60

测试结果:

-- 3查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩
        -- (包括有成绩的和无成绩的)
    select b.s_id,b.s_name,ROUND(AVG(a.s_score),2) as avgscore from
    student b
    left join score a on b.s_id = a.s_id
    GROUP BY b.s_id,b.s_name HAVING avgscore <60
    UNION
    SELECT c.s_id,c.s_name,0 as avgscore
    from student c
    where c.s_id not in (
    SELECT DISTINCT s_id from score
    )

  测试结果:

-- 4查询学过"胡歌"老师授课的同学的信息
select a.*
from student a
JOIN score b
on a.s_id = b.s_id
where b.c_id in(
SELECT c_id from course where t_id =(
select t_id from teacher where t_name = '胡歌'
))

测试结果:

-- 5查询没学过"胡歌"老师授课的同学的信息
select * from
student c
where c.s_id not in(
select a.s_id from student a
join score b on a.s_id=b.s_id where b.c_id in(
select a.c_id from
course a
join teacher b
on a.t_id = b.t_id where t_name ='胡歌'));

测试结果:

 -- 6查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息 
select a.* from
student a
where a.s_id in (select s_id from score where c_id='01' )
and a.s_id not in(select s_id from score where c_id='02')

测试结果:


           

-- 7查询没有学全所有课程的同学的信息
select *
from student
where s_id not in(
select s_id from score t1  
group by s_id having count(*) =(select count(distinct c_id)  from course))

测试结果:

-- 8查询不同老师所教不同课程平均分从高到低显示         
select a.t_id,c.t_name,a.c_id,ROUND(avg(s_score),2) as avg_score
from course a
left join score b on a.c_id=b.c_id
left join teacher c on a.t_id=c.t_id
GROUP BY a.c_id,a.t_id,c.t_name ORDER BY avg_score DESC;

测试结果:

-- 9查询至少有一门课与学号为"01"的同学所学相同的同学的信息

select * from student where s_id in(
    select distinct a.s_id
    from score a where a.c_id in(
    select a.c_id from score a where a.s_id='01')
)

-- 10查询不同老师所教不同课程平均分从高到低显示
    select a.t_id,c.t_name,a.c_id,ROUND(avg(s_score),2) as avg_score
    from     course a
    left join score b on a.c_id=b.c_id
    left join teacher c on a.t_id=c.t_id
    GROUP BY a.c_id,a.t_id,c.t_name ORDER BY avg_score DESC;

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQL是高级的非过程化编程语言,是沟通数据库服务器和客户端的重要工具,允许用户在高层数据结构上工作。它不要求用户指定对数据的存放方法,也不需要用户了解具体的数据存放方式,所以,具有完全不同底层结构的不同数据库系统,可以使用相同的SQL语言作为数据输入与管理的SQL接口。 它以记录集合作为操作对象,所有SQL语句接受集合作为输入,返回集合作为输出,这种集合特性允许一条SQL语句的输出作为另一条SQL语句的输入,所以SQL语句可以嵌套,这使它具有极大的灵活性和强大的功能,在多数情况下,在其他语言需要一大段程序实现的功能只需要一个SQL语句就可以达到目的,这也意味着用SQL语言可以写出非常复杂的语句。    结构化查询语言(Structured Query Language)最早是IBM的圣约瑟研究实验室为其关系数据库管理系统SYSTEM R开发的一种查询语言,它的前身是SQUARE语言。SQL语言结构简洁,功能强大,简单易学,所以自从IBM公司1981年推出以来,SQL语言得到了广泛的应用。如今无论是像Oracle、Sybase、DB2、Informix、SQL Server这些大型的数据库管理系统,还是像Visual Foxpro、PowerBuilder这些PC上常用的数据库开发系统,都支持SQL语言作为查询语言。    美国国家标准局(ANSI)与国际标准化组织(ISO)已经制定了SQL标准。ANSI是一个美国工业和商业集团组织,负责开发美国的商务和通讯标准。ANSI同时也是ISO和International Electrotechnical Commission(IEC)的成员之一。ANSI 发布与国际标准组织相应的美国标准。1992年,ISO和IEC发布了SQL国际标准,称为SQL-92。ANSI随之发布的相应标准是ANSI SQL-92。ANSI SQL-92有时被称为ANSI SQL。尽管不同的关系数据库使用SQL版本有一些差异,但大多数都遵循 ANSI SQL 标准。SQL Server使用ANSI SQL-92的扩展集,称为T-SQL,其遵循ANSI制定的 SQL-92标准。    SQL语言包含4个部分:    数据定义语言(DDL),例如:CREATE、DROP、ALTER等语句。    数据操作语言(DML),例如:INSERT(插入)、UPDATE(修改)、DELETE(删除)语句。    数据查询语言(DQL),例如:SELECT语句。    数据控制语言(DCL),例如:GRANT、REVOKE、COMMIT、ROLLBACK等语句。    SQL语言包括三种主要程序设计语言类别的语句:数据定义语言(DDL),数据操作语言(DML)及数据控制语言(DCL)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值