表格代码

create table student
(
sno varchar(20) primary key,
sname varchar(20) not null,
ssex varchar(20) not null,
sbirthday datetime,
class varchar(20)
)
;
create table teacher
(
tno varchar(20) primary key,
tname varchar(20) not null,
tsex varchar(20) not null,
tbirthday datetime,
prof varchar(20),
depart varchar(20) not null
)
;
create table course
(
cno varchar(20) primary key,
cname varchar(20) not null,
tno varchar(20) not null,
foreign key(tno) references teacher(tno) )
;
create table score
(
sno varchar(20) not null,
cno varchar(20) not null,
degree decimal(4,1),
foreign key(sno) references student(sno),
foreign key(cno) references course(cno),
primary key(sno,cno)
)
; insert into student values('','曾华','男','1977-09-01','');
insert into student values('','匡明','男','1975-10-02','');
insert into student values('','王丽','女','1976-01-23','');
insert into student values('','李军','男','1976-02-20','');
insert into student values('','王芳','女','1975-02-10','');
insert into student values('','陆君','男','1974-06-03',''); insert into teacher values('','李诚','男','1958-12-02','副教授','计算机系');
insert into teacher values('','张旭','男','1969-03-12','讲师','电子工程系');
insert into teacher values('','王萍','女','1972-05-05','助教','计算机系');
insert into teacher values('','刘冰','女','1977-08-14','助教','电子工程系'); insert into course values('3-105','计算机导论','');
insert into course values('3-245','操作系统','');
insert into course values('6-166','数字电路','');
insert into course values('9-888','高等数学',''); insert into score values('','3-245','');
insert into score values('','3-245','');
insert into score values('','3-245','');
insert into score values('','3-105','');
insert into score values('','3-105','');
insert into score values('','3-105','');
insert into score values('','3-105','');
insert into score values('','3-105','');
insert into score values('','3-105','');
insert into score values('','6-166','');
insert into score values('','6-166','');
insert into score values('','6-166','');

题目答案

1.select sname,ssex,class from student
2.select distinct depart from teacher
3.select * from student
4.select * from score where degree>60 and degree<80
5.select * from score where degree in(85,86,88)
6.select * from student where class='95031' or ssex='女'
7.select * from student order by class desc
8.select * from score order by cno asc,degree desc
9.select * from student where class='95031'
10.select * from score order by degree desc
11.我的方法:select avg(degree) from score where cno='3-245'
select avg(degree) from score where cno='3-105'
select avg(degree) from score where cno='6-166'
答案:select Cno,avg(Degree) from Score group by Cno
12.这个题不会
答案:select avg(Degree) from Score where Cno in (select Cno from Score group by Cno having count(*)>5) and Cno like '3%' group by Cno
13.select sno from score where degree>70 and degree<90
14.select sname,cno,degree from score join student on student.sno=score.sno
15.select sno,cname,degree from score join course on course.cno=score.cno
16.这个题做错了
我的答案:select sname,cname,degree from score join student,course on(student.sno=score.sno and course.cno=score.cno)
正确答案:select Sname,Cname,Degree from Score join Student on Student.Sno = Score.Sno join Course on Score.Cno = Course.Cno
不能写在一起
17.select avg(degree) from score where sno in(select sno from student where class='95033')
18.这个题写不出
答案:select Sno,Cno,rank from Score join grade on Score.Degree between low and upp
19.select * from score where cno ='3-105' and degree>(select degree from score where sno = '109' and cno = '3-105')
若果没写后面的cno = '3-105' 软件就会崩溃 不知道为什么 试了好几次 0v0
20.这题只会写前半部分 OTZ
答案:理解1
select * from Score a where Sno in (select Sno from Score group by Sno having count(*)>1 ) and Degree not in (select max(Degree) from Score b where b.Cno = a.Cno)
理解2
select * from Score a where Sno in (select Sno from Score group by Sno having count(*)>1 ) and Degree not in (select max(Degree) from Score a where Sno in (select Sno from Score group by Sno having count(*)>1 ))
21.弄不懂和19题有啥区别 理解不能
答案:select * from Score where Degree >(select Degree from Score where Sno = '109' and Cno = '3-105')
22.还是写不出...
答案:select Sno,Sname,Sbirthday from Student where YEAR(Sbirthday) = (select YEAR(Sbirthday) from Student where Sno = '108')
23.关联了3个表 想的出来 但写不出来
答案:select * from Score where Cno in(select Cno from Course where Tno =(select Tno from Teacher where Tname='张旭'))
24.select tname from teacher where tno in(select tno from course where cno in(select cno from score group by cno having count(*) > 5))
25.select * from Student where Class in('95031','95033')
突然又变简单了
26.select distinct Cno from Score where Degree > 85
27.select * from Score where Cno in(select Cno from Course where Tno in(select Tno from Teacher where depart = '计算机系'))
28.依旧写不出...
答案:select Tname,prof from Teacher where prof not in(select prof from Teacher where depart = '计算机系' and prof in(select prof from Teacher where depart='电子工程系' ))
select prof from Teacher where depart = '计算机系' and prof not in(select prof from Teacher where depart='电子工程系' ) union
select prof from Teacher where depart = '电子工程系' and prof not in(select prof from Teacher where depart='计算机系' )
29.一脸懵b中
答案:select * from Score where Cno='3-105' and Degree>any(select Degree from Score where Cno ='3-245') order by Degree desc
30.select * from score where cno='3-105' and degree>all(select degree from score where cno ='3-245')
31.select sname,ssex,sbirthday from student
union
select tname,tsex,tbirthday from teacher
32.select sname,ssex,sbirthday from student where ssex='女'
union
select tname,tsex,tbirthday from teacher where tsex='女'
33.select * from score a where degree<(select avg(degree) from score b where b.cno = a.cno)
34.我的答案:select tname,depart from teacher
答案:select Tname,depart from Teacher where Tno in (select Tno from Course )
结果是一样的
35.这个题脑子直接想不明白...
答案:select Tname,depart from Teacher where Tno in(select Tno from Course where Cno not in(select Cno from Score))
36.select class from student where ssex='男' group by class having count(*)>1
37.select * from student where sno not in(select sno from student where sname like '王%')
38.select Sname,YEAR(now())-YEAR(Sbirthday) from Student
不知道为什么我写的运行不出来 我没写year
39.select max(sbirthday) from student
select min(sbirthday) from student
40.select * from student order by class desc,sbirthday
41.脑子要炸了
答案:select * from Teacher join Course on Teacher.Tno = Course.Tno where Teacher.Tsex='男'
42.select * from score where degree = (select max(degree) from score )
43.select sname from student where ssex = (select ssex from student where sname='李军')
44.加了个条件就弄不出来了...
答案:select Sname from Student where Ssex = (select Ssex from Student where Sname='李军') and Class=(select Class from Student where Sname = '李军')
45.最后一题依旧写不出来
答案:select * from Score where Sno in(select Sno from Student where Ssex='男') and Cno in(select Cno from Course where Cname='计算机导论')

做完这45道题感觉脑子变得和浆糊一样   有三分之一写不出来的   甚至有的看答案都弄不明白不过感觉思路学到不少   就是高级查询一旦到了3层、4层就开始糊迷了   很多时候想

得到却写不出     看来还是too young too simple 还是缺练

另外感觉程序不能马虎啊   一点点问题  甚至一个分号  就会导致崩盘    要细心啊

SQL 查询45题的更多相关文章

  1. sql查询50题

    一个项目涉及到的50个Sql语句问题及描述:--1.学生表Student(S#,Sname,Sage,Ssex) --S# 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别--2 ...

  2. MySQL查询 45道练习题

    SQL查询45道练习题 1.查询Student表中的所有记录的Sname.Ssex和Class列.select sname,ssex,class from student2.查询教师所有的单位即不重复 ...

  3. 【T-SQL基础】01.单表查询-几道sql查询题

    概述: 本系列[T-SQL基础]主要是针对T-SQL基础的总结. [T-SQL基础]01.单表查询-几道sql查询题 [T-SQL基础]02.联接查询 [T-SQL基础]03.子查询 [T-SQL基础 ...

  4. sql 经典查询50题 思路(一)

    因为需要提高一下sql的查询能力,当然最快的方式就是做一些实际的题目了.选择了这个sql的50题,这次大概做了前10题左右,把思路放上来,也是一个总结. 具体题目见: https://zhuanlan ...

  5. MySQL 笔记整理(1) --基础架构,一条SQL查询语句如何执行

    最近在学习林晓斌(丁奇)老师的<MySQL实战45讲>,受益匪浅,做一些笔记整理一下,帮助学习.如果有小伙伴感兴趣的话推荐原版课程,很不错. 1) --基础架构,一条SQL查询语句如何执行 ...

  6. Oracle常用SQL查询(2)

    三.查看数据库的SQL 1 .查看表空间的名称及大小 select  t.tablespace_name,  round ( sum (bytes / ( 1024 * 1024 )), 0 ) ts ...

  7. ORACLE 常用SQL查询

    一.ORACLE的启动和关闭 1 .在单机环境下 要想启动或关闭ORACLE系统必须首先切换到ORACLE用户,如下 su  -  oracle a.启动ORACLE系统 oracle > sv ...

  8. 学生选课数据库SQL语句45道练习题整理及mysql常用函数(20161019)

    学生选课数据库SQL语句45道练习题: 一.            设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四 ...

  9. 【T-SQL进阶】02.理解SQL查询的底层原理

    本系列[T-SQL]主要是针对T-SQL的总结. [T-SQL基础]01.单表查询-几道sql查询题 [T-SQL基础]02.联接查询 [T-SQL基础]03.子查询 [T-SQL基础]04.表表达式 ...

随机推荐

  1. 【干货】Laravel --Validate (表单验证) 使用实例

    前言 : Laravel 提供了多种方法来验证应用输入数据.默认情况下,Laravel 的控制器基类使用ValidatesRequests trait,该trait提供了便利的方法通过各种功能强大的验 ...

  2. 采用get的方式提交数据到服务器

    1  效果演示:

  3. ECSHOP后台商品列表显示商品缩略图

    ECSHOP后台商品列表显示商品缩略图 ECSHOP教程/ ecshop教程网(www.ecshop119.com) 2013-11-06   ecshop 后台商品列表显示商品缩略图,大楷步凑如下: ...

  4. ecshop后台导航修改教程说明

    ecshop后台导航修改教程说明 ECSHOP教程/ ecshop教程网(www.ecshop119.com) 2014-06-25   需要操作的文件为: 1.修改admin\includes\in ...

  5. How to Write and Publish a Scientific Paper: 7th Edition(科技论文写作与发表教程)(11.04更新)

    How to Write and Publish a Scientific Paper: 7th Edition(科技论文写作与发表教程)(11.04更新) 重要通知: 最近开题报告已差不多告一段落, ...

  6. SQL注入攻击技巧总结

    0×01 你要知道目前有哪些数据库 微软公司旗下的: Microsoft SQL server 简称 MS-SQL 或者 SQL SERVER (大型数据库操作,功能和性能异常强大)(一般也是ASP或 ...

  7. Windows 2008远程多用户登录的配置方法(转载)

    在使用Windows2008远程登录功能时,如果需要进行多用户登录,可以采用以下配置方法: 首先要启用远程桌面这一功能:右击“我的电脑”→属性→远程配置→远程桌面,就可以配置相应的远程桌面功能了.下 ...

  8. Asp.net面试题

    Asp.net核心技术思想 1.概述反射和序列化 反射:程序集包含模块,而模块包含类型,类型又包含成员.反射则提供了封装程序集.模块和类型的对象.您可以使用反射动态地创建类型的实例,将类型绑定到现有对 ...

  9. cpu和内存的关系

    CPU是负责运算和处理的,内存是交换数据的.当程序或者操作者对CPU发出指令,这些指令和数据暂存在内存里,在CPU空闲时传送给CPU,CPU处理后把结果输出到输出设备上,输出设备就是显示器,打印机等. ...

  10. svn报错 400 Bad Request

    MyEclipse中的svn,commit经常报错 Error: Commit failed (details follow):  Error: At least one property chang ...