JAVA企业面试题精选 数据库21-30

1.21.表名:高考信息表

准考证号     科目      成绩
2006001     语文      119
2006001     数学      108
2006002     物理      142
2006001     化学      136
2006002     数学      149
2006002     英语      110
2006002     语文      105
2006001     英语       98
2006002     化学      129
......
给出高考总分在600分以上的学生准考证号.
参考答案:

  在MySQL数据库中,创建测试表和测试数据,代码如下所示:

create table score(
    num int,
    name varchar(20),
    score numeric
);
insert into score(num,name,score) values(2006001,'语文',119);
insert into score(num,name,score) values(2006001,'数学',108);
insert into score(num,name,score) values(2006002,'物理',142);
insert into score(num,name,score) values(2006001,'化学',136);
insert into score(num,name,score) values(2006001,'物理',127);
insert into score(num,name,score) values(2006002,'数学',149);
insert into score(num,name,score) values(2006002,'英语',110);
insert into score(num,name,score) values(2006002,'语文',105);
insert into score(num,name,score) values(2006001,'英语',98);
insert into score(num,name,score) values(2006002,'化学',129);

  查询高考总分在600以上的学生准考证号的SQL语句如下所示:

select num from score group by num having sum(score)>600;

1.22.数据库有两张表一个学生表(id,name,sex),一个学生成绩表(id,chineses,English,math),要求查询学生基本信息以及各科成绩和总成绩,总成绩要求在200到300之间,学生姓名降序

参考答案:

  在MySQL数据库中,创建测试表和测试数据,代码如下所示:

create table student(
    id int primary key,
    name varchar(20),
    sex varchar(2)
);

create table grade(
    id int REFERENCES student(id),
    chineses numeric,
    english numeric,
    math numeric
);

insert into student(name,sex) values('张三','男');
insert into student(name,sex) values('李四','男');
insert into student(name,sex) values('王五','男');

insert into grade(id,chinese,english,math) values(1,80,80,60);
insert into grade(id,chinese,english,math) values(2,50,50,50);
insert into grade(id,chinese,english,math) values(3,80,80,70);

  查询学生基本信息以及各科成绩和总成绩,总成绩要求在200到300之间,学生姓名降旭的SQL语句如下所示:

select name,chinese,english,math,(chineses+english+math) sumscore from student join grade
where student.id=grade.id
and (chinese+english+math) between 200 and 300
order by name desc;

1.23.现有关系数据库表如下:

学生表(学号 char(6),姓名,性别,身份证号);
课程表(课号 char(6),名称);
成绩表(id,学号,课号,分数).
用SQL实现如下两道题:
1.检索姓马的女同学情况(姓名,身份证号);
2.检索有一门或一门以上课程成绩大于等于90的所有学生信息(学号,姓名).
参考答案:

  在MySQL数据库中,创建测试表和测试数据,代码如下所示:

create table stu(
    stuNo char(6),
    name varchar(20),
    gender varchar(4),
    idcard varchar(18)
);

insert into stu(stuno,name,gender,idcard) values('100001','马青青','女','232301198006013421');
insert into stu(stuno,name,gender,idcard) values('100002','马立军','男','232301198006013422');
insert into stu(stuno,name,gender,idcard) values('100003','王媛媛','女','232301198006013423');

create table course(
    cno char(6);
    cname varchar(20);
);

insert into course(cno,cname) values('1','语文');
insert into course(cno,cname) values('2','数学');
insert into course(cno,cname) values('3','英语');

create table score(
    id int primary key AUTO_INCREMENT,
    stuno char(6) REFERENCES stu(stuno),
    cno char(6) REFERENCES course(cno),
    score numeric
);

insert into score(stuno,cno,score) values('100001','1',80);
insert into score(stuno,cno,score) values('100001','2',90);
insert into score(stuno,cno,score) values('100001','3',95);

insert into score(stuno,cno,score) values('100002','1',70);
insert into score(stuno,cno,score) values('100002','2',80);
insert into score(stuno,cno,score) values('100002','3',85);

insert into score(stuno,cno,score) values('100003','1',60);
insert into score(stuno,cno,score) values('100003','2',70);
insert into score(stuno,cno,score) values('100003','3',80);

  1.检索姓马的女同学的姓名,身份证号的SQL语句如下所示:

select name,idcard from stu where name like '马%' and gender='女';

  2.检索有一门或一门以上课程成绩大于等于90的所有学生的学号,姓名,SQL语句如下所示:

select distinct stu.stuno,name from stu
join score
on stu.stuno=score.stuno
join course
on course.cno=score.cno
where score>=90;

1.24.有三张表,学生表Student,课程Course,学生课程表SC,学生可以选修多门课程,一门课程可以被多个学生选修,通过SC表关联,详细要求如下:

  1.写出建表语句;
  2.写出SQL语句,查询选修了所有选修课程的学生;
  3.写出SQL语言,查询选修了至少2门以上的课程的学生;

参考答案:

  1.在Oracle数据库中,创建表和测试数据的SQL语句如下所示:

create table student(
id number(10) primary key,
name varchar2(20));

create table course(
id number(10) primary key,
name varchar2(20));

create table sc(
sid number(10) references student(id),
cid number(10) references course(id),
grade number(4,2));

insert into student values(1,'wanwan');
insert into student values(2,'tutu');
insert into student values(3,'suisui');
insert into student values(4,'fofo');
insert into student values(5,'faiwu');

insert into courese values(1,'java');
insert into courese values(2,'c');
insert into courese values(3,'python');
insert into courese values(4,'c++');

insert into sc values(1,1,98);
insert into sc values(2,1,97);
insert into sc values(3,1,94);
insert into sc values(4,1,92);
insert into sc values(5,1,93);
insert into sc values(1,2,94);
insert into sc values(2,2,92);
insert into sc values(3,2,95);
insert into sc values(5,2,97);
insert into sc values(1,3,92);
insert into sc values(2,3,92);
insert into sc values(4,3,91);
insert into sc values(1,4,99);
insert into sc values(3,4,89);

  2.查询选修了所有选修课程的学生的SQL语句如下所示:

select name from student where id in(select sid sc group by sid having count(*)=(select count(*) from course));

  3.查询选修了至少2门以上的课程的学生的SQL语句如下所示:

select name from student where id in(select sid sc group by sid having count(*)>=2);

1.25.表class和student结构如下,请完成后续SQL语句

表class
属性类型(长度)默认值约束含义
CLASSNO数值(2)主键班级编号
CNAME变长字符(10)非空班级名称
表student
属性类型(长度)默认值约束含义
STUNO数值(8)主键学号
SNAME变长字符(12)非空姓名
SEX字符(2)性别
BIRTHDAY字符(8)生日
EMAIL变长字符(20)唯一电子邮件
SCORE数值(5,2)检查成绩
CLASSNO数值(2)外键,关联到表class的CLASSNO编辑编号
测试数据
STUNOSNAMESEXBIRTHDAYEMAILSCORECLASSNO
21tom19790203tom@163.net89.501
56jerry默认值2
1.修改表student的数据,将所有一班的学生成绩加10分.
2.删除表student的数据,将所有3班出生日期晚于1981年5月12日的记录删除.
3.按班级升序排序,成绩降序排序,查询student表的所有记录.
4.查询student表中所有三班成绩为空的学生记录.
5.表student与class联合查询,要求查询所有学生的学号,姓名,成绩,班级名称.
6.按班级编号分组统计每个班的人数,最高分,最低分,平均分,按平均分降序排序.
7.查询一班学生记录中所有成绩高于本班学生平均分的记录.
8.查询所有学生记录中成绩前十名的学生的学号,姓名,成绩,班级编号.
参考答案:

  在Oracle数据库中,创建测试表和测试数据,代码如下所示:

create table class(
    classno number(2) primary key,
    cname varchar2(10)
);

create table student(
    stuno number(18) primary key,
    sname varchar2(12),
    sex char(2) default '男',
    birthday char(8),
    email varchar2(20) unique,
    score number(5,2) check(score between 0 and 100),
    classno number(2) references class(classno)
);

insert student(stuno,sname,sex,birthday,email,score,classno) values(21,'tom','男','19790203','tom@163.net',89.50,1);

insert student(stuno,sname,birthday,email,score,classno) values(56,'jerry',null,null,null,2);

  1.修改student的数据,将所有一班的学生成绩加10分,SQL语句如下所示:

update student set score=score+10
where classno=(select classno from class where cname='一班');

  2.删除表student的数据,将所有3班出生日期晚于1981年5月12日的记录删除,SQL语句如下所示:

delete student
where to date(birthday,'yyyymmdd')<to date('19810512','yyyymmdd')
and classno=(select classno from class where cname='三班');

  3.按班级升序排序,成绩降序排序,查询student表的所有记录,SQL语句如下所示:

select * from student order by classno,score desc;

  4.查询student表中所有三班成绩为空的学生记录,SQL语句如下所示:

select * from student where classno=(select classno from class where cname='三班') and score is null;

  5.表student与class联合查询,要求查询所有学生的学号,姓名,成绩,班级名称,SQL语句如下所示:

select s.stuno,s.name,s.score,c.name from student s join class c on s.classno=c.classno;

  6.按班级编号分组统计每个班的人数,最高分,最低分,平均分,按平均分降序排序,代码如下所示:

select count(*) "人数",max(score) "最高分",min(score) "最低分",avg(nvl(score,0)) "平均分" from student group by classno order by avg(nvl(score,0));

  7.查询一班学生记录中所有成绩高于本班学生平均分的记录,代码如下所示:

select * from student
where score>(select avg(nvl(score,0)) from student where classno=(select classno from class where cname='一班'))
and classno=(select classno from class where cname='一班');

  8.查询所有学生记录中成绩前十名的学生的学号,姓名,成绩,班级编号,代码如下所示:

select stuno,sname,score,classno from
(select stuno,sname,score,classno from student order by score desc)
where rownum<=10;

1.26.有两张表student和score:

Student:学号,姓名,性别,年龄;
Score:学号,语文,数学,英语;
1.查询张三的学号,姓名,性别,语文,数学,英语;
2.查询语文比数学好的同学;
3.查出姓名相同的学生学号.
参考答案:

  在oracle数据库中,创建测试表和测试数据,代码如下所示:

create table student(
    stuno number(8) primary key,
    sname varchar2(12),
    sex char(2) default '男',
    age int
);

create table score(
    stuno number(8),
    chinese number(3),
    math number(3),
    english number(3)
);

insert into student(stuno,sname,sex,age) values(1,'张三','男',22);
insert into student(stuno,sname,sex,age) values(2,'李四','男',21);
insert into student(stuno,sname,sex,age) values(3,'王五','男',23);
insert into student(stuno,sname,sex,age) values(4,'王五','女',23);

insert into score(stuno,chinese,english,math) values(1,70,80,90);
insert into score(stuno,chinese,english,math) values(2,60,80,70);
insert into score(stuno,chinese,english,math) values(3,70,85,95);
insert into score(stuno,chinese,english,math) values(4,98,85,95);

  1.查询张三的学号,姓名,性别,语文,数学,英语,SQL语句如下所示:

select s.stuno,sname,sex,chinese,math,english from student s join score c on s.stuno=c.stuno and sname='张三';

  2.查询语文比数学好的同学,SQL语句如下所示:

select s.stuno,sname,sex,chinese,math from student s join score c on s.stuno=c.stuno and chinese>math;

  3.查出姓名相同的学生学号,SQL语句如下所示:

select stuno from student where sname in(select sname from student group by sname having count(sname)>1);

1.27.对一个用户登录模块,要求每个用户只允许3次登录错误,超过则将锁定此账户

参考答案:

  基于数据库,在数据库中增加两个字段,分别记录次数和第三次登录的时间,当大于3的那次登录时,判定当前时间和记录的第三次的时间,如果在10分钟内,则依然不能登录.如果超了10分钟,则使得次数和时间重置.
  锁定后可以通过以下方式解锁,可以给每个账户都设一把密钥,当其中任何一个账户被锁定时,系统会自动以短信+电子邮件的形式生成一个随机的密钥发送到对应的用户,该用户想解锁可以等待时间过时,也可以重新输入密钥解锁.也就是说,只要被锁定了以后,除非用户名+密码+密钥全部通过才能解锁.

1.28.数据脚本

create table test1(
    pici VARCHAR2(30),
    busicode VARCHAR2(50),
    amt NUMBER,
    flag VARCHAR2(1)
);
--Add comments to the columns comment on column test1.flag is '1表示成功 2表示失败';

insert into test1 values('20130201','020111',10,1);
insert into test1 values('20130201','020112',5,2);
insert into test1 values('20130201','020113',10,2);
insert into test1 values('20130201','020114',5,1);

insert into test1 values('20130202','020211',10,1);
insert into test1 values('20130202','020212',20,1);
insert into test1 values('20130202','020213',20,1);
insert into test1 values('20130202','020214',20,1);

insert into test1 values('20130203','020311',10,2);
insert into test1 values('20130203','020311',10,2);
insert into test1 values('20130203','020311',10,2);
实现要求:标识位flag,1表示扣款成功,2表示扣款失败,使用一个SQL查询出每天扣款成功笔数,成功金额,失败笔数,失败金额.
参考答案:

  查询每天扣款成功笔数,成功金额,失败笔数,失败金额的SQL语句如下所示:

select to_date(pici,'yyyymmdd') as "日期",
    sum(case when flag=1 then 1 else 0 end) as "成功笔数",
    sum(case when flag=1 then amt else 0 end) as "成功金额",
    sum(case when flag=2 then 1 else 0 end) as "失败笔数",
    sum(case when flag=2 then amt else 0 end) as "失败金额"
    from test1
    GROUP by to_date(pici,'yyyymmdd');

1.29.数据库题

1.创建sms表的语句;
2.写出users与sms左关联的查询语句.
参考答案:

  1.在oracle数据库中,创建sms表和users表的SQL语句如下:

create table sms(
    id number primary key,
    sname varchar2(20)
);

create talbe users(
    id number primary key,
    username varchar2(20),
    smsid number references sms(id)
);

  2.users与sms左关联的查询语句的SQL语句如下:

select sname,username from users u left join sms s on u.smsid=s.id;

1.30.书表(books)

books表字段及测试数据
book_idbook_namecreatdateLastmodifydatedecription
001三个人的世界2005-02-022005-07-07NULL
作者表(authors)字段及测试数据
a_ida_name
01王纷
02李尚
03泰和
部门表(depts)字段及测试数据
d_idd_name
001编辑一部
002编辑二部
003编辑三部
书和作者关联表(bookmap)字段及测试数据
d_ida_id
00101
00102
00103
  查询每个部门的所写的总书量,比如:一本书有3个人写,如果三个人在不同的部门,则每个部门的总书量则为1,最后的结果如下:

   部门  书量
  编辑一部  1
  编辑二部  1
  编辑三部  1

参考答案:

  在oracle数据库中,创建测试表和测试数据,代码如下所示:

create table books(
    book_id number(20) primary key,
    book_name varchar2(200),
    creatdate varchar2(200),
    Lastmodifydate varchar2(200),
    decription varchar2(200)
);

insert into books(book_id,book_name,creatdate,Lastmodifydate,decription) values('001','三个人的世界','2005-02-02','2005-07-07','NULL');

create table authors(
A_id number(20) primary key,
A_name varchar2(200)
);

insert into authors(a_id,a_name) values('01','王纷');
insert into authors(a_id,a_name) values('02','李尚');
insert into authors(a_id,a_name) values('03','泰和');

create table depts(
    d_id number(20) primary key,
    d_name varchar2(200)
);

insert into depts(d_id,d_name) values('001','编辑一部');
insert into depts(d_id,d_name) values('001','编辑二部');
insert into depts(d_id,d_name) values('001','编辑三部');

create table bookmap(
    book_id number(20) references books(book_id),
    a_id number(20) references authors(a_id)
);

insert into bookmap(book_id,a_id) values(001,01);
insert into bookmap(book_id,a_id) values(001,02);
insert into bookmap(book_id,a_id) values(001,03);

create table depmap(
    d_id number(20) references depts(d_id),
    a_id number(20) references authors(a_id)
);

insert into depmap(d_id,a_id) values(001,01);
insert into depmap(d_id,a_id) values(002,02);
insert into depmap(d_id,a_id) values(003,03);

  查询出每个部门的所写的总书量的SQL语句如下所示:

select a.d_name "部门",count(c.book_id) "书量"
from depts a,auhors b,books c,depmap d,bookmap e
where a.d id=d.d id
and d.a id=b.a id
and c.book id=e.book id
and e.a id=b.a id
GROUP BY a.d_name;
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值