sql 题目一
表结构
1、表名:g_cardapply
字段(字段名/类型/长度):
apply_no varchar8; //申请单号(关键字)
apply_date date; //申请日期
state varchar2; //申请状态
2、表名:g_cardapplydetail
字段(字段名/类型/长度):
apply_no varchar8; //申请单号(关键字)
name varchar30; //申请人姓名
idcard varchar18; //申请人身份证号
state varchar2; //申请状态
其中,两个表的关联字段为申请单号。
题目
1、查询身份证号码为440401430103082的申请日期
2、查询同一个身份证号码有两条以上记录的身份证号码及记录个数
3、将身份证号码为440401430103082的记录在两个表中的申请状态均改为07
4、删除cardapplydetail表中所有姓李的记录
建表
create table g_cardapply( apply_no varchar2(8),apply_date date not null,state varchar2(2) not null,constraint PK_APPLY_NO PRIMARY KEY (apply_no) ); create table g_cardapplydetail( apply_no varchar2(8),apply_name varchar2(30) not null,idcard char(18) not null,constraint PK_APPLY_DETAIL_NO PRIMARY KEY (apply_no),constraint FK_CARDAPPLY_DETAIL foreign key (apply_no) references g_cardapply(apply_no) ); -- oracle字符串转日期,使用to_date函数 insert into g_cardapply values('00000010',to_date('2011-11-12','yyyy-mm-dd'),'01'); insert into g_cardapply values('00000011',to_date('2011-11-13','01'); insert into g_cardapply values('00000012',to_date('2011-11-14','02'); insert into g_cardapply values('00000013',to_date('2011-11-15','03'); insert into g_cardapply values('00000014',to_date('2011-11-16','03'); insert into g_cardapplydetail values('00000010','mary','440401430103082','01'); insert into g_cardapplydetail values('00000011','david','440401430103083','01'); insert into g_cardapplydetail values('00000012','02'); insert into g_cardapplydetail values('00000013','mike','440401430103084','03'); insert into g_cardapplydetail values('00000014','03'); commit;
求解
1、查询身份证号码为440401430103082的申请日期
select apply_date from g_cardapply a,g_cardapplydetail b where a.apply_no=b.apply_no and b.idcard='440401430103082';
或
select apply_date from g_cardapply a inner join g_cardapplydetail b on a.apply_no=b.apply_no and b.idcard='440401430103082';
2、查询同一个身份证号码有两条以上记录的身份证号码及记录个数
select idcard,count(idcard) as records from g_cardapplydetail group by idcard having count(idcard)>=2;
3、将身份证号码为440401430103082的记录在两个表中的申请状态均改为07
update g_cardapplydetail set state='07' where idcard='440401430103082'; -- 子查询 update g_cardapply set state='07' where apply_no in ( select apply_no from g_cardapplydetail where idcard='440401430103082' ); commit;
4、删除cardapplydetail表中所有姓李的记录
-- 模糊查询 delete from g_cardapplydetail where apply_name like '李%';
sql 题目二
有一个工厂,非常繁忙,同时在进行多个订单的生产任务。每个订单都有自己的订单编号(WORKORDER_ ID),每个订单要生产的物品要拆分成多个工序,这些工序并行进行,每个工序都有自己的编号STEP_ NBR,测试数据如下:
create table projects( workorder_id varchar2(10) not null,step_nbr int not null,step_status char(1) not null,constraint PK_PROJECTS PRIMARY KEY (workorder_id,step_nbr) ); -- C-完成 W-等待 insert into projects values('ww023','C'); insert into projects values('ww023',1,'W'); insert into projects values('ww023',2,'W'); insert into projects values('ww024','W'); insert into projects values('ww025','C'); insert into projects values('ww025','C');
C-完成 W-等待
请编写SQL语句,找出STEP_ NBR=0,其 STEP STATUS=‘C‘,同时本订单其它工序STEP_ STATUS均为W的订单,比如对以上数据的分析结果就是:
WORKORDER_ID
ww023
要求:至少实现2种写法(多写更好),语句越短越好。
一解:
-- 条件1: where step_nbr=0 and step_status='c' -- 条件2: 'W’= ALL (select step_status from XXX where step_nbr> = 1)
select workorder_id from projects p where p.step_nbr=0 and p.step_status='C' and 'W'= ALL ( select step_status from projects where step_nbr>=1 and workorder_id=p.workorder_id );
二解:
select workorder_id from projects p where step_status='C' group by workorder_id having sum(step_nbr)=0;
sql 题目三
Northwind商贸公司,业务日益发展,公司OA系统正不断推出新版本以紧跟公司的发展。在OA系统中,有一员工角色表,情况如下:
陈城
W
刘海
D
刘海
O
田亮
O
王晓刚
D
张玲
S
张天明
D
张天明
O
其中: W–搬运工人 D–主任 O–高级职员 S–秘书
#p#分页标题#e#
OA开发组的SQL程序员张明得到了上级的一个任务:领导要求得到的高级职员信息表如下:
刘海
B
田亮
O
王晓刚
D
张天明
B
要求:
1)只列出主任和高级职员的信息
2)如果即是高级职员又是主任,用B表示其角色,其它信息不用再显示 (只一条记录)。
你能不能用单条SQL语句帮助张明实现这个查询?
建表
create table roles( emp_name varchar2(20) not null,emp_role char(1) not null,constraint pk_roles primary key(emp_name,emp_role) ); -- 英文名 insert into roles values('Mary','W'); insert into roles values('David','D'); insert into roles values('David','O'); insert into roles values('Mike','O'); insert into roles values('Kate','D'); insert into roles values('Lucy','S'); insert into roles values('Nick','D'); insert into roles values('Nick','O'); -- 中文名 insert into roles values('陈城','W'); insert into roles values('刘海','D'); insert into roles values('刘海','O'); insert into roles values('田亮','O'); insert into roles values('王晓刚','D'); insert into roles values('张玲','S'); insert into roles values('张天明','D'); insert into roles values('张天明','O');
求解
一解:
select emp_name,'B' combine_role from roles where emp_role in ('D','O') group by emp_name having count(*)=2 union select emp_name,max(emp_role) combine_role from roles where emp_role in ('D','O') group by emp_name having count(*)=1;
二解:
select emp_name,case when count(*)=1 then max(emp_role) else 'B' end as emp_role from roles where emp_role in ('D','O') group by emp_name;
或
select emp_name,case when count(*)=2 then 'B' else max(emp_role) end as emp_role from roles where emp_role in ('D','O') group by emp_name;
三解:
select emp_name,substr('DOB',sum(instr('DO',emp_role)),1) combine_role from roles where emp_role in ('D','O')group by emp_name; -- instr:返回第二个参数在第一个参数中第一次出现的位置
sql 题目四
最近,经过你的努力,你得到了一份工作,成为了百货公司的一位经理。 到位后,你发现你的销售数据库中有两张表,一个是商店促销时间的日历,另一个是在促销期间的销售额列表。你需要编写一个查询,告诉我们在每次促销中哪位职员的销售额最高,这样可以给那个职员发绩效奖金。
找出在各次促销活动中,销售量最高的销售员。
请编制一条SQL来完成这个查询。(尽量考虑多种写法)
建表
-- 商店促销时间的日历 create table promotions ( promo_name varchar2(50) not null primary key,-- 促销活动名称 start_date date not null,-- 开始时间 end_date date not null,-- 终止时间 check(start_date<=end_date) ); -- 促销期间的销售额表 (注意:该表只是保存促销期间的销售额) create table sales ( ticket_nbr int not null primary key,--销售票据编号 (自增) clerk_name varchar2(20) not null,--销售员姓名 sale_date date not null,--销售日期 sale_amount number(9,2) not null --销售金额 ); insert into promotions values('spring sales',to_date('2009-2-1','yyyy/mm/dd'),to_date('2009-2-15','yyyy/mm/dd')); insert into promotions values('worker sale',to_date('2009-5-1',to_date('2009-5-4','yyyy/mm/dd')); insert into promotions values('children sale',to_date('2009-6-1','yyyy/mm/dd')); insert into promotions values('national day sale',to_date('2009-10-1',to_date('2009-10-7','yyyy/mm/dd')); create sequence seq_nbr; insert into sales values(seq_nbr.nextval,30); insert into sales values(seq_nbr.nextval,'tom',73); insert into sales values(seq_nbr.nextval,110); insert into sales values(seq_nbr.nextval,to_date('2009-2-2',190); insert into sales values(seq_nbr.nextval,92); insert into sales values(seq_nbr.nextval,to_date('2009-2-3',130); insert into sales values(seq_nbr.nextval,90); insert into sales values(seq_nbr.nextval,to_date('2009-2-4',70); insert into sales values(seq_nbr.nextval,9); insert into sales values(seq_nbr.nextval,to_date('2009-2-5',88); insert into sales values(seq_nbr.nextval,to_date('2009-2-13',50); insert into sales values(seq_nbr.nextval,170); insert into sales values(seq_nbr.nextval,to_date('2009-2-14',270); insert into sales values(seq_nbr.nextval,67.5); insert into sales values(seq_nbr.nextval,280.5); insert into sales values(seq_nbr.nextval,113); insert into sales values(seq_nbr.nextval,to_date('2009-5-2',35.5); insert into sales values(seq_nbr.nextval,125); insert into sales values(seq_nbr.nextval,to_date('2009-5-3',93); insert into sales values(seq_nbr.nextval,167); insert into sales values(seq_nbr.nextval,123.5); insert into sales values(seq_nbr.nextval,200); insert into sales values(seq_nbr.nextval,2); insert into sales values(seq_nbr.nextval,110.5); insert into sales values(seq_nbr.nextval,213); insert into sales values(seq_nbr.nextval,1123); insert into sales values(seq_nbr.nextval,780); insert into sales values(seq_nbr.nextval,310); insert into sales values(seq_nbr.nextval,to_date('2009-10-2',139); insert into sales values(seq_nbr.nextval,1110.5); insert into sales values(seq_nbr.nextval,998); insert into sales values(seq_nbr.nextval,to_date('2009-10-3',120); insert into sales values(seq_nbr.nextval,to_date('2009-10-4',10); insert into sales values(seq_nbr.nextval,234); insert into sales values(seq_nbr.nextval,to_date('2009-10-5',to_date('2009-10-6',23); insert into sales values(seq_nbr.nextval,10.5); insert into sales values(seq_nbr.nextval,'王海','刘万理','高春梅',10.5);