Sql常见面试题(总结)

1.用一条SQL语句 查询出每门课都大于80分的学生姓名 

name   kecheng   fenshu
张三     语文       81
张三     数学       75
李四     语文       76
李四     数学       90
王五     语文       81
王五     数学       100
王五     英语       90

A: select distinct name from table  where  name not in (select distinct name from table where fenshu<=80)

2.学生表 如下:
自动编号   学号   姓名 课程编号 课程名称 分数
1        2005001  张三  0001      数学    69
2        2005002  李四  0001      数学    89
3        2005001  张三  0001      数学    69
删除除了自动编号不同,其他都相同的学生冗余信息

A: delete tablename where 自动编号 not in(select min(自动编号) from tablename group by 学号,姓名,课程编号,课程名称,分数)

3. 表A(单位名称,单位帐号), 表B(单位编号,个人账号)

列出各单位的名称,账号,以及单位的人数

select A.name, A.dwzh, isnull(Ct.Quantity,'0') as Quantity from A
left join

(select dwzh, count(*) as Quantity from B
group by dwzh) as Ct

on A.dwzh = Ct.dwzh


4. 股票表(股票代码,买卖类型,数量)

按照股票代码列出,买的数量,卖的数量。

select isnull(a.StockID, b.StockID), isnull(a.S,'0'), isnull(b.B,'0') from (
select StockID,sum(quantity) as S from stocks
where sType = 's'
group by StockID ) a

full join (

select StockID,sum(quantity) as B from stocks
where sType = 'b'
group by StockID ) b

on a.StockID = b.StockID


5. select * from tempT where ','+ tempT.description + ',' like '%,1,%'

posted @ 2005-07-19 11:21  Ready!  阅读(8426)  评论(9编辑  收藏  举报