一个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
相关文档:
数学函数
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 ......
USE master
GO
CREATE DATABASE testbase2
ON
PRIMARY
(NAME = prim_sub_dat1,
FILENAME = 'G:\SQL SERVER2000\prim_sub_dat.mdf',
SIZE = 5MB,
MAXSIZE = 50MB,
FILEGROWTH = 20%),
(NAME = prim_sub_dat2,
FILENAME = 'G:\SQL SERVER2000\prim_sub_dat.ndf'
SIZE = 5MB,
MAXSIZE = 50MB,
......
下面的这些脚本都可以找到引起磁盘排序的SQL。
SELECT /*+ rule */ DISTINCT a.SID, a.process, a.serial#,
TO_CHAR (a.logon_time, 'YYYYMMDD HH24:MI:SS') LOGON, a.osuser,TABLESPACE, b.sql_text
from v$session a, v$sql b, v$sort_usage c
WHERE a.sql_address = b.address AND a.saddr = c.session_addr;
......
如果是类似"select * from user where uid="+uid +" and pwd="+pwd 很容易出问题
使用 SQLParamenter
把你的SQL语句写成 类似存储过程 ......