注:源自于《Java程序员面试秘笈》!

1.创建数据库MYDB

create database MYDB

2.创建学生表student (sno,sname,ssex,sage,sclass)

create table student(
sno int primary key,
sname varchar(8),
ssex varchar(3),
sage int,
scalss varchar(6)
)

3.创建课程表course(cno,cname,ccredit)

create table course(
cno int primary key,
cname varchar(20),
ccredit int
)

4.创建选课表SC(sno,cno,grade)

create table SC(
sno int foreign key references student(sno),
cno int foreign key references course(cno),
grade int
)

5.添加数据

insert into student values (1,'李勇','男',20, 'y01')
insert into student values (2,'刘晨','男',21, 'y02')
insert into student values (3,'王敏','女',19, 'y02')
insert into student values (4,'张力','男',20, 'y05')

6.添加数据

insert into course values (1, '数据库', 5)
insert into course values (2, 'C语言', 5)
insert into course values (3, '开发模式', 5)

7.添加数据

insert into SC values (1,1,5)
insert into SC values (1,2,5)
insert into SC values (1,1,5)
insert into SC values (1,3,5)

8.查询全体同学的学号、姓名

select sno, sname
from student

9.查询全体同学的姓名学号班级(按顺序输出)

select sname,snao,sclass
from student

10.查询全体同学的记录(显示所有行)

select * from student

11.查询全体同学的姓名及出生年份

select sname , 2017 - sage
from student

12.查询全体同学姓名、出生年份、班级(班级要用小写字母LOWER函数)

select sname , 2017 - sage 出生年份, lower(sclass)
from student

13.查询全体同学的姓名、出生年份、所在班级,列为YearOfBirth

select sname, 2017 - sage YearOfBirth, sclass
from student

14.查询选课中学员的学号,并且去掉重复行,用distinct

select distinct sno
from sc

15.查询Y02班全体同学名单

select *
from student
where sclass = 'Y02'

16.查询考试不及格的同学学号

select sno
from sc
where grade < 60

17.查询所有年龄在20岁以下的同学的姓名及年龄

select sname,sage
from student
where sage < 20

18.查询年龄在1920岁(包括1920)之间的同学姓名、班级、年龄。

select sname,sclass,sage
from student
where sage>=19 and sage<=20
where between 19 and 20

19.查询年龄不在19~20岁之间的同学的姓名、班级、年龄。

select sname,sclass,sage
from student
where sage not between 19 and 20

20.查询不是Y02班和Y05班的同学的姓名、性别。

select sname,ssex
from student
where not sclass = 'Y02' and not sclass = 'Y05'

21.查询Y02班和Y05班的同学姓名、性别

select sname , ssex
from student
where sclass = 'Y02' or sclass = 'Y05'

22.查询所有姓刘的同学的姓名、学号、性别。

select sname, sno, ssex
from student
where sname like '刘%'

23.查询所有姓张且全名为2个汉字的学生姓名

select sname
from student
where sname like '张_'

24.某些学生未考试,查询缺少成绩的同学的学号和课程号

select sno,cno
from sc
where grade is null

25.查询所有有成绩的同学的学号、课程号和成绩

select sno,cno
from sc
where grade is not null

26.查询Y02班年龄在20岁以下的学生的姓名和年龄

select sname, sage
from student
where sclass = 'Y02' and sage < 20

27.查询选修1号课程的同学的学号和成绩,按降序排列。

select sno,grade
from sc
where cno = 1
order by grade desc

28.查询全体同学信息查询结果,按所在班级的班级名称降序排列,同级同学按年龄升序排列。

select *
from student
order by sclass desc , sage asc

29.查询学员总人数

select count(*) from student

30.查询选修课程学员人数。

select count(*) from sc

31.统计1号课的学员平均成绩。

select avg(grade)
from sc
where cno = 1

32.求各个课程号及相应的选课人数 (重点)

select count(*)
from sc
group by cno

33.查询选修1号课的同学的最高成绩

select max(grade)
from sc
where cno = 1

34.查询选取1门以上课程的同学学号

select sno
from sc
group by sno having count(cno) > 1

35.查询每个学员及其选修课程情况

select sno,cno
from sc

36.查询每个学员及其选修课情况,对没有选课的也要输出其名字、学号、性别、班级

select a.sno, a.sname, a.sage, a.ssex, a.sclass, sc.cno, sc.grade
from student a left outer join sc
on a.sno = sc.sno order by a.sname

37.查询选取2号课程且成绩在90分以上的同学。

select *
from sc
where cno = 2 and grade > 90

38.查询每个同学学号姓名、选修课程名及成绩。

select a.sno, a.sname, sc.cno, sc.grade
from student a left outer join sc
on a.sno = sc.sno
order by sc.sno

39.查询与刘晨在一个班的同学

select sname from student
where sclass in (
select sclass
from student
where sname = '刘晨'
)

40.选取C语言的同学学号和姓名

select sno , sname
from student
where sno in (
select sno
from sc
where cno in (
select cno
from course
where cname = 'C语言'
)
)

41.查询其他班中比Y02班某一同学大的同学姓名和年龄。

select sname , sage
from student
where sage > any (
select sage
from student
where sclass = 'Y02'
) and sclass <> 'Y02'

42.查询其他班中比Y02班同学全部都大的同学姓名和年龄。

select sname , sage
from student
where sage > all (
select sage
from student
where sclass = 'Y02'
) and sclass <> 'Y02'

43.查询选取1号课程的学员的姓名。

select sname
from student
where sno in(
select sno
from sc
where cno = 1
)

44.查询没有选取1号课程的学员的姓名。

select sname
from student
where sno not in (
select sno
from sc
where cno = 1
)

45.查询Y02班同学及年龄不大于19的学员(union)。

select *
from student
where sclass = 'Y02' union
select *
from student
where sage <= 19

46.查询选取1号课程或者2号课程的同学学号

select distinct sno
from sc
where cno = 1 or cno = 2

47.将4号学员的年龄改为23岁。

update student set sage = 23 where sno = 4

48.将所有同学的年龄增加1岁

update student set sage = sage + 1

49.Y02班同学的成绩改为100分

update sc set grade = 100
where sno in (
select sno
from student
where sclass = 'y02'
)

50.删除学号为1的同学记录。

delete from student where sno = 1

编写SQL语句(快速回顾)的更多相关文章

  1. 使用Sql语句快速将数据表转换成实体类

    开发过程中经常需要根据数据表编写对应的实体类,下面是使用sql语句快速将数据表转换成对应实体类的代码,使用时只需要将第一行'TableName'引号里面的字母换成具体的表名称就行了: declare ...

  2. 缓存策略 半自动化就是mybaitis只支持数据库查出的数据映射到pojo类上,而实体到数据库的映射需要自己编写sql语句实现,相较于hibernate这种完全自动化的框架我更喜欢mybatis

    springboot入门(三)-- springboot集成mybatis及mybatis generator工具使用 - FoolFox - CSDN博客 https://blog.csdn.net ...

  3. 如果一条SQL语句太长,我们可以通过回车键来创建一个新行来编写SQL语句,SQL语句的命令结束符为分号(;)。

    1.如果一条SQL语句太长,我们可以通过回车键来创建一个新行来编写SQL语句,SQL语句的命令结束符为分号(;). 2.select查询的多个字段之间要用逗号“,”分割,如果查询涉及多个表,那多个表之 ...

  4. [转载]编写SQL语句查询出每个各科班分数最高的同学的名字,班级名称,课程名称,分数

    [转载]编写SQL语句查询出每个各科班分数最高的同学的名字,班级名称,课程名称,分数 转载自:https://blog.csdn.net/one_money/article/details/56921 ...

  5. mysql 常用 sql 语句 - 快速查询

    Mysql 常用 sql 语句 - 快速查询 1.mysql 基础 1.1 mysql 交互         1.1.1 mysql 连接             mysql.exe -hPup    ...

  6. SQL 语句快速参考

    来自 W3CSchool 的 SQL 快速参考 SQL 语句 语法 AND / OR SELECT column_name(s)FROM table_nameWHERE conditionAND|OR ...

  7. Mysql编写sql语句的小技巧

    1.查询数据(保证查询性能) SELECT * 和 SELECT t.id , t.name:后者性能其实总体优于前者. 2.在查询的时候最好给表起个 别名,方便找到表中要查询的字段.执行sql的进行 ...

  8. MSSql使用SQL语句快速查看表对的就说明,及表字段描述及字段类型

    --表描述 SELECT tbs.name 表名,ds.value 描述 FROM sys.extended_properties ds LEFT JOIN sysobjects tbs ON ds. ...

  9. 用sql语句,快速备份表数据

    1.SqlServer数据库 --DataTable 原数据表 --DataTable_20150717 要备份的新表名 select * into DataTable_20150717 from D ...

随机推荐

  1. 一个简单的spring boot程序

    搭建一个spring boot项目十分的方便,网上也有许多,可以参考 https://www.cnblogs.com/ityouknow/p/5662753.html 进行项目的搭建.在此我就不详细介 ...

  2. Java框架之Spring01-IOC-bean配置-文件引入-注解装配

    Spring 框架,即framework.是对特定应用领域中的应用系统的部分设计和实现的整体结构.就相当于让别人帮你完成一些基础工作,它可以处理系统很多细节问题,而且框架一般是成熟,稳健的. Spri ...

  3. creator 2.0版本对于preloadScene函数获取加载进度

    有时候,当我们场景上挂载的资源过多时,我们使用cc.director.loadScene切换场景时会等一段时间才会切换过去,这对游戏的体验是相当不好的.所以我们可以使用cc.director.prel ...

  4. P1828 香甜的黄油 Sweet Butter 最短路 寻找一个点使得所有点到它的距离之和最小

    P1828 香甜的黄油 Sweet Butter 闲来无事 写了三种最短路(那个Floyed是不过的) 题目描述 农夫John发现做出全威斯康辛州最甜的黄油的方法:糖.把糖放在一片牧场上,他知道N(1 ...

  5. 逆元(inv)

    推荐博客 : http://blog.csdn.net/baidu_35643793/article/details/75268911 通常我们在计算除法取模时,并不能直接的取模后再去相除,答案会有问 ...

  6. VMware Workstation CentOS7 Linux 学习之路(3)--.net coreWeb部署

    1.首先创建一个文件夹,命名为core mkdir core cd core 2.我这里用FlashFXP连接Linux 把我发布的项目上传到CentOS7的core文件夹下 此时我输入命令 dotn ...

  7. 三分钟网络基础-IP地址分类

    IP 地址的编址方法共经过了三个历史阶段: 分类的 IP 地址 子网的划分 超网 这篇文章首先介绍,最初始的 IP 地址分类方法. 分类的 IP 将 IP 地址划分为若干个固定类,每一类地址都由两个固 ...

  8. Ubuntu阿里镜像

    ubuntu 14.04: http://mirrors.aliyun.com/ubuntu-releases/14.04/ ubuntu 16.04: http://mirrors.aliyun.c ...

  9. LeetCode动画 | 1038. 从二叉搜索树到更大和树

    今天分享一个LeetCode题,题号是1038,标题是:从二分搜索树到更大和数. 题目描述 给出二叉搜索树的根节点,该二叉树的节点值各不相同,修改二叉树,使每个节点 node 的新值等于原树中大于或等 ...

  10. HttpApplication IHttpAsyncHandler, IHttpHandler, IComponent, IDisposable ps url System.Web.dll

    // 摘要:     //     定义 ASP.NET 应用程序中的所有应用程序对象共有的方法.属性和事件.此类是用户在 Global.asax 文件中所定义的应用程序的基类.     [Toolb ...