面试常考SQL语句

参考:https://www.cnblogs.com/diffrent/p/8854995.html

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

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

SQL> select distinct name from table where name not in (select distinct name from table where score<=80)
select name from table group by name having min(score)>80

2. 学生表 如下:

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

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

3.一个叫 team 的表,

里面只有一个字段name, 一共有4 条纪录,分别是a,b,c,d, 对应四个球队,现在四个球队进行比赛,用一条sql 语句显示所有可能的比赛组合.

select a.name, b.name
from team a, team b 
where a.name < b.name

4.面试题:怎么把这样一个表

year   month amount
1991   1     1.1
1991   2     1.2
1991   3     1.3
1991   4     1.4
1992   1     2.1
1992   2     2.2
1992   3     2.3
1992   4     2.4


查成这样一个结果
year     m1   m2   m3   m4
1991   1.1   1.2   1.3   1.4
1992   2.1   2.2   2.3   2.4 

答案一、

SQL> select year, 
(select amount from   aaa m where month=1   and m.year=aaa.year) as m1,
(select amount from   aaa m where month=2   and m.year=aaa.year) as m2,
(select amount from   aaa m where month=3   and m.year=aaa.year) as m3,
(select amount from   aaa m where month=4   and m.year=aaa.year) as m4
from aaa   group by year


5. 说明:复制表( 只复制结构, 源表名:a      新表名:b ) 
 

SQL>  select * into b from a where 1<>1       (where1=1,拷贝表结构和数据内容)

[   <>(不等于)(SQL Server Compact)

比较两个表达式。 当使用此运算符比较非空表达式时,如果左操作数不等于右操作数,则结果为 TRUE。 否则,结果为 FALSE。]


Oracle:

create table b

As

Select * from a where 1=2

 

6. 说明:拷贝表( 拷贝数据,  源表名:a     目标表名:b) 

SQL>  insert into b(a, b, c) select d,e,f from a; 


7. 说明:显示文章、提交人和最后回复时间

SQL> select a.title,a.username,b.adddate from table a,(select max(adddate) adddate 
from table where table.title=a.title) b


8. 说明:外连接查询( 表名1 :a   表名2 :b)
 

SQL> select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUTER JOIN b ON a.a = b.c

ORACLE:

SQL> select a.a, a.b, a.c, b.c, b.d, b.f from a ,b

where a.a = b.c(+)


9. 说明:

日程安排提前五分钟提醒

SQL> select * from 日程安排 where datediff('minute',f 开始时间,getdate())>5


10. 说明:

两张关联表,删除主表中已经在副表中没有的信息

SQL> delete from info where not exists 
(select * from infobz where info.infid=infobz.infid )

11.有两个表A 和B ,

均有key 和value 两个字段,如果B 的key 在A 中也有,就把B 的value 换为A 中对应的value

SQL> update b set b.value=(select a.value from a where a.key=b.key) 
where b.id in(select b.id from b,a where b.key=a.key);

12.高级sql 面试题

原表:
courseid coursename score

1 Java        70
2 oracle      90
3 xml         40
4 jsp         30
5 servlet     80


为了便于阅读, 查询此表后的结果显式如下( 及格分数为60):
courseid coursename score mark

1 Java     70   pass
2 oracle   90   pass
3 xml      40   fail
4 jsp      30   fail
5 servlet  80   pass


写出此查询语句:

SQL> select courseid, coursename ,score ,decode(sign(score-60),-1,'fail','pass') 
as mark from course

完全正确
 

SQL> desc course_v

Name Null? Type
----------------------------------------- -------- ----------------------------
COURSEID NUMBER
COURSENAME VARCHAR2(10)
SCORE NUMBER

 

SQL> select * from course_v;

COURSEID COURSENAME SCORE
---------- ---------- ----------
1 java 70
2 oracle 90
3 xml 40
4 jsp 30
5 servlet 80

 

SQL> select courseid, coursename ,score ,decode(sign(score-60),-1,'fail','pass') 
as mark from course_v;

COURSEID COURSENAME SCORE MARK
---------- ---------- ---------- ----
1 java 70 pass
2 oracle 90 pass
3 xml 40 fail
4 jsp 30 fail
5 servlet 80 pass

 

SQL面试题(1)

create table testtable1
(
id int IDENTITY,
department varchar(12) 
)

select * from testtable1
insert into testtable1 values('设计')
insert into testtable1 values('市场')
insert into testtable1 values('售后')

/*
结果
id     department
1     设计
2     市场
3     售后 
*/

create table testtable2
(
id int IDENTITY,
dptID int,
name varchar(12)
)
insert into testtable2 values(1,'张三')
insert into testtable2 values(1,'李四')
insert into testtable2 values(2,'王五')
insert into testtable2 values(3,'彭六')
insert into testtable2 values(4,'陈七')


/*
用一条SQL语句,怎么显示如下结果
id dptID department name
1   1      设计        张三
2   1      设计        李四
3   2      市场        王五
4   3      售后        彭六
5   4      黑人        陈七
*/

SELECT testtable2.* , ISNULL(department,'黑人')
FROM testtable1 right join testtable2 on testtable2.dptID = testtable1.ID

 

sql面试题(2)

有表A,结构如下: 
A:    p_ID    p_Num    s_id 
       1           10           01 
       1           12           02 
       2             8           01 
       3           11           01 
       3            8            03 

其中:p_ID为产品ID,p_Num为产品库存量,s_id为仓库ID。请用SQL语句实现将上表中的数据合并,合并后的数据为: 
p_ID    s1_id    s2_id    s3_id 
1         10         12            0 
2          8           0             0 
3          11         0             8 


其中:s1_id为仓库1的库存量,s2_id为仓库2的库存量,s3_id为仓库3的库存量。如果该产品在某仓库中无库存量,那么就是0代替。

结果:

select p_id ,
sum(case when s_id=1 then p_num else 0 end) as s1_id
,sum(case when s_id=2 then p_num else 0 end) as s2_id
,sum(case when s_id=3 then p_num else 0 end) as s3_id
from myPro group by p_id

SQL面试题(3)

 

1.为管理业务培训信息,建立3个表:

     S(S#,SN,SD,SA)    S#,  SN,  SD,  SA  分别代表学号,学员姓名,所属单位,学员年龄

     C(C#,CN)              C#,  CN  分别代表课程编号,课程名称

     SC(S#,C#,G)         S#,  C#,  G  分别代表学号,所选的课程编号,学习成绩

 

    (1)使用标准SQL嵌套语句查询选修课程名称为  ‘税收基础’  的学员学号和姓名

select s# ,sn from s where S# in(select S# from c,sc where c.c#=sc.c# and cn='税收基础')

      (2) 使用标准SQL嵌套语句查询选修课程编号为’C2’的学员姓名和所属单位?

select sn,sd from s,sc where s.s#=sc.s# and sc.c#=’c2’

      (3) 使用标准SQL嵌套语句查询不选修课程编号为’C5’的学员姓名和所属单位?

select sn,sd from s where s# not in(select s# from sc where c#=’c5’)

       (4)查询选修了课程的学员人数

select 学员人数=count(distinct s#) from sc

       (5) 查询选修课程超过5门的学员学号和所属单位?

select sn,sd from s where s# in
(select s# from sc group by s# having count(distinct c#)>5)

 

SQL面试题(4)

1.查询A(ID,Name)表中第31至40条记录,ID作为主键可能是不是连续增长的列,完整的查询语句如下:
 

select top 10 * from A where ID >(select max(ID) 
from 
(select top 30 ID from A order by A ) T) order by A


2.查询表A中存在ID重复三次以上的记录,完整的查询语句如下:

select * from
(select count(ID) as count from table group by ID)T 
where T.count>3

 

SQL面试题(5)


原表: 
courseid coursename score 
------------------------------------- 
1     java       70 
2     oracle    90 
3     xml        40 
4     jsp         30 
5     servlet   80 
------------------------------------- 

为了便于阅读,查询此表后的结果显式如下(及格分数为60): 
courseid coursename score mark 
--------------------------------------------------- 
1   java        70   pass 
2   oracle     90   pass 
3   xml         40   fail  
4   jsp          30   fail 
5   servlet    80   pass 
--------------------------------------------------- 
写出此查询语句 

 

select courseid, coursename ,score ,(case when score<60 then 'fail' else 'pass' end) 
as mark from course
ORACLE : 
select courseid, coursename ,score ,decode(sign(score-60),-1,'fail','pass') 
as mark from course

(DECODE函数是ORACLE PL/SQL是功能强大的函数之一,目前还只有ORACLE公司的SQL提供了此函数)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值