一个SQL面试题
题目要求
阿里baba的面试题
有三个表
学生表 S
SID SNAME
教师课表 T
TID TNAME TCL
成绩表 SC
SID TCL SCR
各字段的含义不用我标明了吧,大侠哥哥么!呵呵
现在要求写SQL查询
1、选修了A、B课程,并且A课程的成绩大于B成绩的学生姓名?
2、没有选修‘li’老师的课程的学生,要求不能用in,exists 等词?
create table SC(
SID varchar(64) ,
TCL varchar(64) ,
SCR int) ;
create table S (
SID varchar(64) ,
SNAME varchar(64)
) ;
create table T
(
TID varchar(64),
TNAME varchar(64),
TCL varchar(64)
) ;
insert into S VALUES ('1','11') ;
insert into S VALUES ('2','22') ;
insert into S VALUES ('3','33') ;
insert into S VALUES ('4','44') ;
insert into SC VALUES ('1','a',100) ;
insert into SC VALUES ('1','b',90) ;
insert into SC VALUES ('2','a',77) ;
insert into SC VALUES ('3','b',34) ;
insert into SC VALUES ('4','a',59) ;
insert into SC VALUES ('4','b',88) ;
insert into T VALUES ('5','li','a');
第一个问题的答案
select
s.sname,sc1.scr,sc2.scr
from
s,sc sc1,sc sc2
where s.sid=sc1.sid
and s.sid=sc2.sid
and sc1.tcl='a'
and sc2.tcl='b'
and sc1.scr>sc2.scr
运行结果:
11 ,100 , 90
第二题的答案
select sid from
(select s.sid,a.tcl from (select sc.sid,sc.tcl from sc,t where sc.tcl=t.tcl and t.tname='li' ) a
right join s
on a.sid=s.sid ) b
where b.tcl is null
运行结果
3
相关文档:
create PROCEDURE pagelist
@tablename nvarchar(50),
@fieldname nvarchar(50)='*',
@pagesize int output,--每页显示记录条数
@currentpage int output,--第几页
@orderid nvarchar(50),--主键排序
@sort int,--排序方式,1表示升序,0表示降序排列
......
数学函数
1.绝对值
S:select abs(-1) value
O:select abs(-1) value from dual
2.取整(大)
S:select ceiling(-1.001) value
O:select ceil(-1.001) value from dual
3.取整(小)
S:select floor(-1.001) value
O:select floor(-1.001) value from dual
4.取整(截取)
S:select cast(-1.002 as in ......
SQL触发器实例讲解
2008-10-31 21:06
SQL触发器实例1
定义: 何为触发器?在SQL Server里面也就是对某一个表的一定的操作,触发某种条件,从而执行的一段程序。触发器是一个特殊的存储过程。
常见的触发器有三种:分别应用于Insert , Update , Delete 事件。
&nbs ......
对日常工作中用到的感觉有用的sql语句做个归纳,用于今后温故知新。
*复制表:
create table tablename as select * from table_src;
create table tablename as select * from table_src where 1 <> 1; --只复制表结构 ......
用SQL语句添加删除修改字段
1.增加字段
alter table docdsp add dspcode char(200)
alter table tbl add meet_group int2
2.删除字段
ALTER TABLE table_NAME DROP COLUMN column_NAME
3.修改字段类型
&nbs ......