sql基础笔试题

现在我们假设只有一个table,名为pages,有四个字段,id, url,title,body。里面储存了很多网页,网页的url地址,title和网页的内容,然后你用一个sql查询将url匹配的排在最前, title匹配的其次,body匹配最后,没有任何字段匹配的,不返回。

就是上面这道面试题,让我想了一个下午,在网上找资料,最后用下面方法实现

 

SELECT *
FROM page where url like '%baidu%' or title like '%baidu%' or like ''
ORDER BY CHARINDEX('baidu', url) DESC, CHARINDEX('baidu', title) DESC,
      CHARINDEX('baidu', body) DESC

 

 

但我感觉这种方法并不是最简单的,后来把这个方法发给面试的人,他给我了一种更简单方法,只要用基本的Sql语句就可以实现。代码如下

 

select a.[id],a.mark from
(
select [page].[id],100 as mark from [page] where [page].[url] like '%baidu%'
union
select [page].[id],50 as mark from [page] where [page].[title] like '%baidu%'
union
select [page].[id],10 as mark from [page] where [page].[body] like '%baidu%'
) as a  order by mark desc

 

 

用union 实现联合查询,在每个查询语句中定义一个临时变量mark 并给mark赋值,在最后的输出时采用mark来排序,这样实现,非常简单,我感觉这题更多考研我们的编程思想。

----------------------------------------------------------------------------------------------------------------------------------

题目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 SN,SD FROM  S
  
Where [S#] IN Select [S#] FROM
 C,SC
                           
Where C.[C#]=SC.[C#]   AND CN=N'税收基础')


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

Select  S.SN,S.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. 使用标准SQL嵌套语句查询选修全部课程的学员姓名和所属单位  

网上流传的错误答案:
Select SN,SD FROM  S
  
Where [S#] IN  ( Select [S#] FROM SC RIGHT JOIN
 
                                                                        C  
ON SC.[C#]=C.[C#]
 
                                                           
GROUP BY [S#]

                                                      
HAVING COUNT(*)=COUNT([S#] ) )

经过调试验证的正确答案:
SELECT  SN, SD  FROM  S 
           
WHERE  S# 
                              
IN  ( SELECT  SC.S# 
                                                      
FROM  SC  RIGHT   JOIN  C 
                                                                   
ON  SC.C#  =  C.C#
                                                      
GROUP   BY  SC.S#   -- 在结果集中以学生分组,分组后的 SC.C#选课数=C.C#课程数 即为全部课程
                                                       HAVING     COUNT ( distinct (SC.C#))   -- 注意:一个学生同一门课程可能有多条成绩记录,需要distinct
                                                                           =  (  select   count ( * from  C )  -- 注意:HAVING条件不能用COUNT(distinct(SC.C#)) = COUNT(distinct(C.C#)
                                       )--子查询获得选修全部课程的学生学号


  5. 查询选修了课程的学员人数  

Select  学员人数 = COUNT ( DISTINCT   [ S# ] FROM  SC


  6. 查询选修课程超过5门的学员学号和所属单位

Select  SN,SD  FROM  S
  
Where   [ S# ]   IN  (  Select   [ S# ]   FROM  SC           
                                                           
GROUP   BY   [ S# ]
                                                  
HAVING    COUNT ( DISTINCT   [ C# ] > 5   )


题目2:
======

  已知关系模式:
  S (SNO,SNAME) 学生关系。SNO 为学号,SNAME 为姓名
  C (CNO,CNAME,CTEACHER) 课程关系。CNO 为课程号,CNAME 为课程名,CTEACHER 为任课教师
  SC(SNO,CNO,SCGRADE) 选课关系。SCGRADE 为成绩

  1. 找出没有选修过“李明”老师讲授课程的所有学生姓名 

Select  SNAME  FROM  S
  
Where   NOT   EXISTS  (  Select   *   FROM  SC,C
                          
Where  SC.CNO = C.CNO
                               
AND  CNAME = ' 李明 '
                               
AND  SC.SNO = S.SNO)

  
  2. 列出有二门以上(含两门)不及格课程的学生姓名及其平均成绩  

Select  S.SNO,S.SNAME,AVG_SCGRADE = AVG (SC.SCGRADE)
  
FROM   S ,  SC , 
                        (
Select  SNO  FROM  SC
                                 
Where  SCGRADE < 60
                                 
GROUP   BY  SNO
                                 
HAVING   COUNT ( DISTINCT  CNO) >= 2 )    A 
            Where  S.SNO = A.SNO  AND  SC.SNO = A.SNO
  
GROUP   BY  S.SNO,S.SNAME


  3. 列出既学过“1”号课程,又学过“2”号课程的所有学生姓名  

Select  S.SNO,S.SNAME
  
FROM  S,
                        (
Select  SC.SNO  FROM  SC,C
                           
Where  SC.CNO = C.CNO
                                        
AND  C.CNAME  IN ( ' 1 ' , ' 2 ' )
                           
GROUP   BY  SNO
                          
HAVING   COUNT ( DISTINCT  CNO) = 2
                  )SC 
            Where  S.SNO = SC.SNO


  4. 列出“1”号课成绩比“2”号同学该门课成绩高的所有学生的学号         

Select  S.SNO,S.SNAME
  
FROM  S,
                         (
Select  SC1.SNO FROM  SC SC1,C C1,SC SC2,C C2
                                               
Where  SC1.CNO = C1.CNO  AND  C1.NAME = ' 1 '
                                                           
AND  SC2.CNO = C2.CNO  AND  C2.NAME = ' 2 '
                                                          
AND  SC1.SCGRADE > SC2.SCGRADE  )  SC 
          Where  S.SNO = SC.SNO


  5. 列出“1”号课成绩比“2”号课成绩高的所有学生的学号及其“1”号课和“2”号课的成绩

Select  S.SNO,S.SNAME,SC. [ 1号课成绩 ] ,SC. [ 2号课成绩 ]
            
FROM  S,
                         ( 
Select  SC1.SNO, [ 1号课成绩 ] = SC1.SCGRADE, [ 2号课成绩 ] = SC2.SCGRADE
                                     
FROM  SC SC1,C C1,SC SC2,C C2
                                    
Where  SC1.CNO = C1.CNO  AND  C1.NAME = ' 1 '
                                                   
AND  SC2.CNO = C2.CNO  AND  C2.NAME = ' 2 '
                                                  
AND  SC1.SCGRADE > SC2.SCGRADE)   SC 
        
Where  S.SNO = SC.SNO



题目3:
======

有如下表记录:
ID        Name       EmailAddress                  LastLogon
100        test4       test4@yahoo.cn       2007-11-25 16:31:26
13          test1       test1@yahoo.cn       2007-3-22 16:27:07
19          test1       test1@yahoo.cn       2007-10-25 14:13:46
42          test1       test1@yahoo.cn       2007-11-20 14:20:10
45          test2       test2@yahoo.cn       2007-4-25 14:17:39
49          test2       test2@yahoo.cn       2007-5-25 14:22:36

用一句sql查询出每个用户最近一次登录的记录(每个用户只显示一条最近登录的记录)

方法一:
SELECT  a. *   from  users  a   inner   join
                              (
SELECT   [ Name ] , LastLogon = MAX (LastLogon)     FROM   users     GROUP   BY   [ Name ] )  b
                               
on  a. [ Name ] = b. [ Name ]   and  a. [ LastLogon ] = b. [ LastLogon ]

方法二:
SELECT  a. *   from  users  a    inner   join
                               (
SELECT   Name, MAX (LogonID) LogonID      FROM   users     GROUP   BY   [ Name ] )  b
                             
on  a.LogonID = b.LogonID
                             
-- where a.LogonId=b.LogonId

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值