面试题

一列数的规则如下:1、1、2、3、5、8、13、21、34.....求第30位数是多少,用递归算法实现。

  public class MainClass

  {

     public static void Main()

        {

           Console.WriteLine(Foo(30));

         }

      public static int Foo(int i)

       {

            if(i<=0)

             return 0;

            else if(i>0 && i<=2)

             return 1;

             else return Foo(i-1)+Foo(i-2);

        }

    }

求以下表达方式的值,写出您想到的一种或几种实现方法:1+2+3+4+........+m

     int Num=this.TextBox.Text.Tostring();

     int Sum=0;

     for(int i=0;i<Num+1;i++)

      {

          if((i % 2)==2)

             {

                 Sum+=i;

              }

           else

             {

                  Sum=Sum-1;

             }

          }

        System.Console.WriteLine(Sum.ToString());

        System.Console.ReadLine();

 

 

 

 

1,什么是CRS,CLS,CLR?

TS:通用语言系统。CLS:通用语言规范。CLR:公共语言运行库。

 

ref. out的区别
两者都是按地址传递的,使用后都将改变原来的数值。很多人在论坛上解释说out是按数值传递,

是错误的。简单的测试后可以知道out使用也能改变数值的,所以肯定是按照地址传递的。
其次:rel可以把参数的数值传递进函数,但是out是要把参数清空,就是说你无法把一个数
值从out传递进去的,out进去后,参数的数值为空,所以你必须初始化一次。这个就是两个的区别,
或者说就像有的网友说的,rel是有进有出,out是只出不进。经典!!!
2,一个用户控件的生命周期?
   
   初始化。。装载视图。。回传数据处理。。加载事件。。回传事件通知。。处理回发事件。。预呈现。。保存视图状态
   呈现。。卸载。
  
3,什么是方法的重载?

   重写是子类的方法覆盖父类的方法,要求方法名和参数都相同
   重载是在同一个类中的两个或两个以上的方法,拥有相同的方法名,但是参数却不相同,方法体也不相同,
   最常见的重载的例子就是类的构造函数,可以参考API帮助文档看看类的构造方法

1,C#,类的继承
  
2,JavaScript,对象的继承


3,i++,++i 返回值的区别?
   
  i++ 是先求表达式的值在加1,++i是先加1在求表达式的值;

1)有一个数组{1,2,3,4,5,6,7,8,9}的是反序,存到哪一个数据库(递归实现)?
class Program
    {
        static void Main(string[] args)
        {
            int[] array = { 1,2,3,4,5,6,7,8,9,10};
            Program pp = new Program();
            pp.Rs(array, 10);
            Console.Read();

        }
        public void Rs(int[] A, int n)
        {
            if (n >= 1)
            {
                Console.Write(A[n - 1]+",");
                Rs(A, n - 1);

            }
        }

    }

2)有两个字符串 abc 和 abcdef 求两者有最大相同的字符串,比如 abc 要需要效率的问题?

public void getResualt()
             {
               String str1 = "ashfjeudccckfjgiccccccjgurhd";
               String str2 = "dhfurjcccckgoymjdhccfi";
              String max="";
              for(int i=0;i<str2.Length;i++)
              {
               String temp1=str2.Substring(i);
               for(int y=temp1.Length-1;y>=0;y--)
               {
                String temp2=temp1.Substring(0,y);
                if(str1.IndexOf(temp2)!=-1 && temp2.Length>max.Length)
                 max=temp2;
               }
              }
             
            
              Response.Write(max);
             }

1)相关子查询
2)多表连接,分组
 
面试-------------------------------------------
1)Const 与 ReadOnly 的区别?

readonly 关键字与 const 关键字不同。 const 字段只能在该字段的声明中初始化。 readonly 字段可以在声明或构造函数中初始化。 因此,根据所使用的构造函数,readonly 字段可能具有不同的值。 另外,const 字段为编译时常数,而 readonly 字段可用于运行时常数

2)在不同的程序里,有不同的命名空间?在同一程序里,有不同的命名空间?有区别吗?
  
3) 用 sql 语句分页
    "select top " + 每页条数 + " * from 表 where id not in (select top " + ((当前页数 - 1) * 每页条数) + "id from lt_dcan order by id)";
  
4)在同一类中,方法名相同,同一方法,但修饰权限不同可以吗?

 

只能从低往高变 反过来不行
如果一个方法原来的修饰符是 protected的 继承重载以后可以改成public的

   

5)方法相同,但返回类型不同,可以吗?
 

在同一个类中,不允许存在具有相同方法名和相同参数类型列表的方法,即使该方法的返回类型不相同


1)jQuery 选择器有哪些?

2)如何赛选择一个网页中的所有 checkbox ,用 jQuery 实现。
 
      $(document).ready(function () {
        //全选或全不选
        $("#chkAll").click(function () {//当点击全选框时
            var flag = $("#chkAll").attr("checked");//判断全选按钮的状态
            $("[id$='checkbox']").each(function () {//查找每一个Id以checkbox结尾的checkbox
                $(this).attr("checked", flag);//选中或者取消选中
            });
        });


1)数据中表与表之间的关系有哪些?
一对一,一对多,多对多
什么是一对一?主表,和附属表

一对多关系
一对多关系是最普通的一种关系。在这种关系中,A 表中的一行可以匹配 B 表中的多行,但是 B 表中的一行只能匹配 A 表中的一行。例如,publishers 和 titles 表之间具有一对多关系:每个出版社出版很多书,但是每本书名只能出自一个出版社。

只有当一个相关列是一个主键或具有唯一约束时,才能创建一对多关系。

多对多关系
在多对多关系中,A 表中的一行可以匹配 B 表中的多行,反之亦然。要创建这种关系,需要定义第三个表,称为结合表,它的主键由 A 表和 B 表的外部键组成。例如,authors 和 titles 表具有多对多关系,这是由于这些表都与 titleauthors 表具有一对多关系。titleauthors 表的主键是 au_id 列(authors 表的主键)和 title_id 列(titles 表的主键)的组合。

一对一关系
在一对一关系中,A 表中的一行最多只能匹配于 B 表中的一行,反之亦然。如果相关列都是主键或都具有唯一约束,则可以创建一对一关系。


 
2)主键 你喜欢用 int 类型 还是 varchar 类型
各有什么优缺?
创建效率
int 效率低
Guid 效率高
数据检索效率

50万的数据量下,int,varchar作为主键时,若以主键为查询条件,消耗时间几乎一样,但若以其他字段作为查询条件,两种主键的查询结果不太稳定,有时候是int快,有时候是varchar快。

 

可见在插入表时,对于无主键和有主键的表来说,无主键的表其插入效率最低,其次是Varchar,uniqueidentifier和int 从测试结果看,int类型和uniqueidentifier类型的插入时间相差无几,在2005下如果插入一两条记录的情况下应该是没有什么影响,而采用uniq这种类型的字段,在编程取ID和转移数据方面有着很大的优势,至于它占用空间比int类型要多一些,这对于本身就是超大的数据库而言应该不是太大问题。
如果是大批量的插入数据,应该是int的效率比varchar要高


 
1)在 。Net 中 LINQ 你用过吗?
2)var 属于强类型,还是弱类型?
3)使用“泛型”的好处?
 
1)你使用的 VS 是什么版本的?数据库是什么版本的?
 
1)你认为自己的优势在哪里?
 
1)你有什么问题要问我们吗?

请选择一种排序算法对数组[3,7,9,2,-1,22]进行排序。(10分)
数组冒泡排序
   Protected  int[]  bubbleUp(int[] Array)     {       
 for (int i = 0; i < Array.Length; i++)
  {       for (int j = i+1; j < Array.Length; j++)          
     {               
       if (Array[i] > Array[j])                
        {                  
        int temp=Array[i];                    
        Array[i]=Array[j];                    
        Array[j] = temp;               
         }           
      }        
  }       
  return Array;   
  }

 

值类型引用类型如何换分?

以它们在计算机内存中如何分配来划分

值类型与引用类型的区别?

1,值类型的变量直接包含其数据,
2,引用类型的变量则存储对象引用。
对于引用类型,两个变量可能引用同一个对象,因此对一个变量的操作可能影响另一个变量所引用的对象。对于值类型,每个变量都有自己的数据副本,对一个变量的操作不可能影响另一个变量。 
 

开发人员笔试题(请将题目答案写在答题纸上)

一、 概念题(20分)

  1. 请描述.net中值类型和引用类型的区别?

值类型(Value Type)(如 char、int 和 float)、枚举类型和结构类型。
引用类型(Reference Type) 包括类 (Class) 类型、接口类型、委托类型和数组类型。

如何来划分它们?
以它们在计算机内存中如何分配来划分


1,值类型的变量直接包含其数据,
2,引用类型的变量则存储对象引用。

  1. 请写出C#语言中可以修饰一个方法的所有关键字以及用法?
  2. 请描述C#语言中定义方法参数的三种方式ref,out,params之间的区别?
  3. 请描述C#中Dictionary<,>和Hashtable的区别,用Key取值时需要注意的地方?

Hashtable 和 Dictionary <K, V> 类型
 1:单线程程序中推荐使用 Dictionary, 有泛型优势, 且读取速度较快, 容量利用更充分.
 2:多线程程序中推荐使用 Hashtable, 默认的 Hashtable 允许单线程写入, 多线程读取, 对 Hashtable 进一步调用 Synchronized() 方法可以获得完全线程安全的类型. 而 Dictionary 非线程安全, 必须人为使用 lock 语句进行保护, 效率大减.
 3:Dictionary 有按插入顺序排列数据的特性 (注: 但当调用 Remove() 删除过节点后顺序被打乱), 因此在需要体现顺序的情境中使用 Dictionary 能获得一定方便.

Hashtable 类和 Dictionary<(Of <(TKey, TValue>)>) 泛型类实现 IDictionary 接口

Dictionary<(Of <(TKey, TValue>)>) 泛型类还实现 IDictionary<(Of <(TKey, TValue>)>) 泛型接口。因此,这些集合中的每个元素都是一个键/值对。

Dictionary<(Of <(TKey, TValue>)>) 类与 Hashtable 类的功能相同
对于值类型,特定类型(不包括 Object)的 Dictionary<(Of <(TKey, TValue>)>) 的性能优于 Hashtable,这是因为 Hashtable 的元素属于 Object 类型,所以在存储或检索值类型时通常发生装箱和取消装箱操作。

 

 

二、 算法题(用C# 或 Javascript 实现)(20分)

  1. 请选择一种排序算法对数组[3,7,9,2,-1,22]进行排序。(10分)
  2. 请找出一个数组中连续相加为0的起始、结束索引。如数组[3,-2, 1,1,-2,4]中结果是索引1至3、索引2至4。(请考虑时间复杂度,如实现O(n)可另加10分)(10分)

三、 编程题(40分)

  1. 请回答下面javascript程序中的问题并解释原因(10分)

fn = function () { alert('****') };         

function foo() {

       foo.fn = function () { alert('@@@@') }

       this.fn = function () { alert('####') }

       fn = function () { alert('&&&&') };

       var fn = function () { alert('$$$$$$') }

}

foo.prototype.fn = function () { alert('1111'); }

foo.fn = function () { alert('2222'); }

var f = new foo();

f.fn();//问题1      $$$$$

foo.fn();//问题2     2222

fn();      //问题3&&&&

 

输出结果是:

####

@@@@

****

原因:

JavaScript的所有function类型的对象都有一个prototype属性。这个prototype属性本身又是一个object类型的对象,因此我们也可以给这个prototype对象添加任意的属性和方法。既然prototype是对象的“原型”,那么由该函数构造出来的对象应该都会具有这个“原型”的特性。事实上,在构造函数的prototype上定义的所有属性和方法,都是可以通过其构造的对象直接访问和调用的。也可以这么说,prototype提供了一群同类对象共享属性和方法的机制。 

 

  1. 请写出以下程序的输出。(10分)

<div id="divOne" οnclick="alert('我就是不知道');">

         <div id="divTwo" οnclick="alert('我还是不知道!')">

             <a id="hr_three" href="# " οnclick="alert('我不知道!')">点击我</a>

         </div>

</div>

弹出三个对话框,第一个对话框是“我不知道!”,点击第一个对话框的确定按钮,弹出第二个对话框“我还是不知道!”,点击第二个对话框的确定按钮,弹出第三个对话框“我就是不知道”。

 

 

  1. 请写出以下程序的输出内容,以及原因(20分)

static void Main()

        {

A a = newA() {Age=20,Name="Jack" };

ArefA = newA() { Age = 20, Name = "Jack" };

Test(a, refrefA, 30, "Bill");

Console.WriteLine("a的名称:{0},a的年龄{1}",a.Name,a.Age);

Console.WriteLine("refA的名称:{0},refA的年龄{1}", refA.Name, refA.Age);        

        }

 

static void Test(A a,ref A refA,int age,string name)

       {

           a = new A();

           a.Age = age;

           a.Name = name;

 

refA = new A();

refA.Name = name;

refA.Age = age++;

       }

 

a的名称:Jack,a的年龄20

refA的名称:Bill,refA的年龄30

 

四、 SQL题(20分)

1       写出两种分页SQL语句.(10分)

2       写出下列语句各个部分的执行顺序(10分)

SELECT[NAME],COUNT([ACCOUNT])as[COUNT]FROM[STUDENTS]

WHERESCORE>80 GROUPBY[NAME]HAVING(COUNT([ACCOUNT])>2)ORDERBY 2

 

FROM

WHERE

GROUP BY

聚合函数计算

HAVING

SELECT

 

1·删除重复数据

 例如:
id           name         value
1               a                 pp
2               a                 pp
3               b                 iii
4               b                 pp
5               b                 pp
6               c                 pp
7               c                 pp
8               c                 iii
id是主键
要求得到这样的结果
id           name         value
1               a                 pp
3               b                 iii
4               b                 pp
6               c                 pp
8               c                 iii

方法1
delete   YourTable  
where   [id]   not   in   (
select   max([id])   from   YourTable  
group   by   (name   +   value))

方法2
delete   a
from   表   a   left   join(
select   id=min(id)   from   表   group   by   name,value
)b   on   a.id=b.id
where   b.id   is   null

 

选择:select * from table1 where 范围

  插入:insert into table1(field1,field2) values(value1,value2)

  删除:delete from table1 where 范围

  更新:update table1 set field1=value1 where 范围

  查找:select * from table1 where field1 like ’%value1%’ ---like的语法很精妙,查资料!

  排序:select * from table1 order by field1,field2 [desc]

  总数:select count * as totalcount from table1

  求和:select sum(field1) as sumvalue from table1

  平均:select avg(field1) as avgvalue from table1

  最大:select max(field1) as maxvalue from table1

  最小:select min(field1) as minvalue from table1

 多表查询

 

问题及描述:
--1.学生表
Student(S#,Sname,Sage,Ssex) --S# 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别
--2.课程表
Course(C#,Cname,T#) --C# --课程编号,Cname 课程名称,T# 教师编号
--3.教师表
Teacher(T#,Tname) --T# 教师编号,Tname 教师姓名
--4.成绩表
SC(S#,C#,score) --S# 学生编号,C# 课程编号,score 分数
*/


--1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
--
1.1、查询同时存在"01"课程和"02"课程的情况
select a.* , b.score [课程'01'的分数],c.score [课程'02'的分数]from Student a , SC b , SC c
where a.S# = b.S# and a.S# = c.S# and b.C# ='01'and c.C# ='02'and b.score > c.score
--1.2、查询同时存在"01"课程和"02"课程的情况和存在"01"课程但可能不存在"02"课程的情况(不存在时显示为null)(以下存在相同内容时不再解释)
select a.* , b.score [课程"01"的分数],c.score [课程"02"的分数]from Student a
leftjoin SC b on a.S# = b.S# and b.C# ='01'
leftjoin SC c on a.S# = c.S# and c.C# ='02'
where b.score >isnull(c.score,0)

--2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
--
2.1、查询同时存在"01"课程和"02"课程的情况
select a.* , b.score [课程'01'的分数],c.score [课程'02'的分数]from Student a , SC b , SC c
where a.S# = b.S# and a.S# = c.S# and b.C# ='01'and c.C# ='02'and b.score < c.score
--2.2、查询同时存在"01"课程和"02"课程的情况和不存在"01"课程但存在"02"课程的情况
select a.* , b.score [课程"01"的分数],c.score [课程"02"的分数]from Student a
leftjoin SC b on a.S# = b.S# and b.C# ='01'
leftjoin SC c on a.S# = c.S# and c.C# ='02'
whereisnull(b.score,0) < c.score

--3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
select a.S# , a.Sname , cast(avg(b.score) asdecimal(18,2)) avg_score
from Student a , sc b
where a.S# = b.S#
groupby a.S# , a.Sname
havingcast(avg(b.score) asdecimal(18,2)) >=60
orderby a.S#

--4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩
--
4.1、查询在sc表存在成绩的学生信息的SQL语句。
select a.S# , a.Sname , cast(avg(b.score) asdecimal(18,2)) avg_score
from Student a , sc b
where a.S# = b.S#
groupby a.S# , a.Sname
havingcast(avg(b.score) asdecimal(18,2)) <60
orderby a.S#
--4.2、查询在sc表中不存在成绩的学生信息的SQL语句。
select a.S# , a.Sname , isnull(cast(avg(b.score) asdecimal(18,2)),0) avg_score
from Student a leftjoin sc b
on a.S# = b.S#
groupby a.S# , a.Sname
havingisnull(cast(avg(b.score) asdecimal(18,2)),0) <60
orderby a.S#

--5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
--
5.1、查询所有有成绩的SQL。
select a.S# [学生编号], a.Sname [学生姓名], count(b.C#) 选课总数, sum(score) [所有课程的总成绩]
from Student a , SC b
where a.S# = b.S#
groupby a.S#,a.Sname
orderby a.S#
--5.2、查询所有(包括有成绩和无成绩)的SQL。
select a.S# [学生编号], a.Sname [学生姓名], count(b.C#) 选课总数, sum(score) [所有课程的总成绩]
from Student a leftjoin SC b
on a.S# = b.S#
groupby a.S#,a.Sname
orderby a.S#

--6、查询"李"姓老师的数量
--
方法1
selectcount(Tname) ["李"姓老师的数量]from Teacher where Tname like N'李%'
--方法2
selectcount(Tname) ["李"姓老师的数量]from Teacher whereleft(Tname,1) = N''
/*
"李"姓老师的数量  
-----------
1
*/

--7、查询学过"张三"老师授课的同学的信息
selectdistinct Student.*from Student , SC , Course , Teacher
where Student.S# = SC.S# and SC.C# = Course.C# and Course.T# = Teacher.T# and Teacher.Tname = N'张三'
orderby Student.S#

--8、查询没学过"张三"老师授课的同学的信息
select m.*from Student m where S# notin (selectdistinct SC.S# from SC , Course , Teacher where SC.C# = Course.C# and Course.T# = Teacher.T# and Teacher.Tname = N'张三') orderby m.S#

--9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
--
方法1
select Student.*from Student , SC where Student.S# = SC.S# and SC.C# ='01'andexists (Select1from SC SC_2 where SC_2.S# = SC.S# and SC_2.C# ='02') orderby Student.S#
--方法2
select Student.*from Student , SC where Student.S# = SC.S# and SC.C# ='02'andexists (Select1from SC SC_2 where SC_2.S# = SC.S# and SC_2.C# ='01') orderby Student.S#
--方法3
select m.*from Student m where S# in
(
 
select S# from
  (
   
selectdistinct S# from SC where C# ='01'
   
unionall
   
selectdistinct S# from SC where C# ='02'
  ) t
groupby S# havingcount(1) =2
)
orderby m.S#

--10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
--
方法1
select Student.*from Student , SC where Student.S# = SC.S# and SC.C# ='01'andnotexists (Select1from SC SC_2 where SC_2.S# = SC.S# and SC_2.C# ='02') orderby Student.S#
--方法2
select Student.*from Student , SC where Student.S# = SC.S# and SC.C# ='01'and Student.S# notin (Select SC_2.S# from SC SC_2 where SC_2.S# = SC.S# and SC_2.C# ='02') orderby Student.S#

--11、查询没有学全所有课程的同学的信息
--
11.1、
select Student.*
from Student , SC
where Student.S# = SC.S#
groupby Student.S# , Student.Sname , Student.Sage , Student.Ssex havingcount(C#) < (selectcount(C#) from Course)
--11.2
select Student.*
from Student leftjoin SC
on Student.S# = SC.S#
groupby Student.S# , Student.Sname , Student.Sage , Student.Ssex havingcount(C#) < (selectcount(C#) from Course)

--12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
selectdistinct Student.*from Student , SC where Student.S# = SC.S# and SC.C# in (select C# from SC where S# ='01') and Student.S# <>'01'

--13、查询和"01"号的同学学习的课程完全相同的其他同学的信息
select Student.*from Student where S# in
(
selectdistinct SC.S# from SC where S# <>'01'and SC.C# in (selectdistinct C# from SC where S# ='01')
groupby SC.S# havingcount(1) = (selectcount(1) from SC where S#='01'))

--14、查询没学过"张三"老师讲授的任一门课程的学生姓名
select student.*from student where student.S# notin
(
selectdistinct sc.S# from sc , course , teacher where sc.C# = course.C# and course.T# = teacher.T# and teacher.tname = N'张三')
orderby student.S#

--15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
select student.S# , student.sname , cast(avg(score) asdecimal(18,2)) avg_score from student , sc
where student.S# = SC.S# and student.S# in (select S# from SC where score <60groupby S# havingcount(1) >=2)
groupby student.S# , student.sname

--16、检索"01"课程分数小于60,按分数降序排列的学生信息
select student.* , sc.C# , sc.score from student , sc
where student.S# = SC.S# and sc.score <60and sc.C# ='01'
orderby sc.score desc 

--17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
--
17.1 SQL 2000 静态
select a.S# 学生编号 , a.Sname 学生姓名 ,
      
max(case c.Cname when N'语文'then b.score elsenullend) [语文],
      
max(case c.Cname when N'数学'then b.score elsenullend) [数学],
      
max(case c.Cname when N'英语'then b.score elsenullend) [英语],
      
cast(avg(b.score) asdecimal(18,2)) 平均分
from Student a
leftjoin SC b on a.S# = b.S#
leftjoin Course c on b.C# = c.C#
groupby a.S# , a.Sname
orderby 平均分 desc
--17.2 SQL 2000 动态
declare@sqlnvarchar(4000)
set@sql='select a.S# '+ N'学生编号'+' , a.Sname '+ N'学生姓名'
select@sql=@sql+',max(case c.Cname when N'''+Cname+''' then b.score else null end) ['+Cname+']'
from (selectdistinct Cname from Course) as t
set@sql=@sql+' , cast(avg(b.score) as decimal(18,2)) '+ N'平均分'+' from Student a left join SC b on a.S# = b.S# left join Course c on b.C# = c.C#
group by a.S# , a.Sname order by
'+ N'平均分'+' desc'
exec(@sql)

 

 

 

 

 

 

 

--24、查询学生平均成绩及其名次
--
24.1 查询学生的平均成绩并进行排名,sql 2000用子查询完成,分平均成绩重复时保留名次空缺和不保留名次空缺两种。
select t1.* , px = (selectcount(1) from
(
 
select m.S# [学生编号] ,
         m.Sname
[学生姓名] ,
        
isnull(cast(avg(score) asdecimal(18,2)),0) [平均成绩]
 
from Student m leftjoin SC n on m.S# = n.S#
 
groupby m.S# , m.Sname
) t2
where 平均成绩 > t1.平均成绩) +1from
(
 
select m.S# [学生编号] ,
         m.Sname
[学生姓名] ,
        
isnull(cast(avg(score) asdecimal(18,2)),0) [平均成绩]
 
from Student m leftjoin SC n on m.S# = n.S#
 
groupby m.S# , m.Sname
) t1
orderby px

select t1.* , px = (selectcount(distinct 平均成绩) from
(
 
select m.S# [学生编号] ,
         m.Sname
[学生姓名] ,
        
isnull(cast(avg(score) asdecimal(18,2)),0) [平均成绩]
 
from Student m leftjoin SC n on m.S# = n.S#
 
groupby m.S# , m.Sname
) t2
where 平均成绩 >= t1.平均成绩) from
(
 
select m.S# [学生编号] ,
         m.Sname
[学生姓名] ,
        
isnull(cast(avg(score) asdecimal(18,2)),0) [平均成绩]
 
from Student m leftjoin SC n on m.S# = n.S#
 
groupby m.S# , m.Sname
) t1
orderby px
--24.2 查询学生的平均成绩并进行排名,sql 2005用rank,DENSE_RANK完成,分平均成绩重复时保留名次空缺和不保留名次空缺两种。
select t.* , px = rank() over(orderby[平均成绩]desc) from
(
 
select m.S# [学生编号] ,
         m.Sname
[学生姓名] ,
        
isnull(cast(avg(score) asdecimal(18,2)),0) [平均成绩]
 
from Student m leftjoin SC n on m.S# = n.S#
 
groupby m.S# , m.Sname
) t
orderby px

select t.* , px = DENSE_RANK() over(orderby[平均成绩]desc) from
(
 
select m.S# [学生编号] ,
         m.Sname
[学生姓名] ,
        
isnull(cast(avg(score) asdecimal(18,2)),0) [平均成绩]
 
from Student m leftjoin SC n on m.S# = n.S#
 
groupby m.S# , m.Sname
) t
orderby px
 
--25、查询各科成绩前三名的记录
--
25.1 分数重复时保留名次空缺
select m.* , n.C# , n.score from Student m, SC n where m.S# = n.S# and n.score in
(
selecttop3 score from sc where C# = n.C# orderby score desc) orderby n.C# , n.score desc
--25.2 分数重复时不保留名次空缺,合并名次
--
sql 2000用子查询实现
select*from (select t.* , px = (selectcount(distinct score) from SC where C# = t.C# and score >= t.score) from sc t) m where px between1and3orderby m.c# , m.px
--sql 2005用DENSE_RANK实现
select*from (select t.* , px = DENSE_RANK() over(partition by c# orderby score desc) from sc t) m where px between1and3orderby m.C# , m.px

--26、查询每门课程被选修的学生数
select c# , count(S#)[学生数]from sc groupby C#

--27、查询出只有两门课程的全部学生的学号和姓名
select Student.S# , Student.Sname
from Student , SC
where Student.S# = SC.S#
groupby Student.S# , Student.Sname
havingcount(SC.C#) =2
orderby Student.S#

--28、查询男生、女生人数
selectcount(Ssex) as 男生人数 from Student where Ssex = N''
selectcount(Ssex) as 女生人数 from Student where Ssex = N''
selectsum(casewhen Ssex = N''then1else0end) [男生人数],sum(casewhen Ssex = N''then1else0end) [女生人数]from student
selectcasewhen Ssex = N''then N'男生人数'else N'女生人数'end[男女情况] , count(1) [人数]from student groupbycasewhen Ssex = N''then N'男生人数'else N'女生人数'end

--29、查询名字中含有"风"字的学生信息
select*from student where sname like N'%风%'
select*from student wherecharindex(N'' , sname) >0

--30、查询同名同性学生名单,并统计同名人数
select Sname [学生姓名], count(*) [人数]from Student groupby Sname havingcount(*) >1

--31、查询1990年出生的学生名单(注:Student表中Sage列的类型是datetime)
select*from Student whereyear(sage) =1990
select*from Student wheredatediff(yy,sage,'1990-01-01') =0
select*from Student wheredatepart(yy,sage) =1990
select*from Student whereconvert(varchar(4),sage,120) ='1990'

--32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
select m.C# , m.Cname , cast(avg(n.score) asdecimal(18,2)) avg_score
from Course m, SC n
where m.C# = n.C#   
groupby m.C# , m.Cname
orderby avg_score desc, m.C# asc

--33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
select a.S# , a.Sname , cast(avg(b.score) asdecimal(18,2)) avg_score
from Student a , sc b
where a.S# = b.S#
groupby a.S# , a.Sname
havingcast(avg(b.score) asdecimal(18,2)) >=85
orderby a.S#

--34、查询课程名称为"数学",且分数低于60的学生姓名和分数
select sname , score
from Student , SC , Course
where SC.S# = Student.S# and SC.C# = Course.C# and Course.Cname = N'数学'and score <60

--35、查询所有学生的课程及分数情况;
select Student.* , Course.Cname , SC.C# , SC.score 
from Student, SC , Course
where Student.S# = SC.S# and SC.C# = Course.C#
orderby Student.S# , SC.C#

--36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数;
select Student.* , Course.Cname , SC.C# , SC.score 
from Student, SC , Course
where Student.S# = SC.S# and SC.C# = Course.C# and SC.score >=70
orderby Student.S# , SC.C#

--37、查询不及格的课程
select Student.* , Course.Cname , SC.C# , SC.score 
from Student, SC , Course
where Student.S# = SC.S# and SC.C# = Course.C# and SC.score <60
orderby Student.S# , SC.C#

--38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名;
select Student.* , Course.Cname , SC.C# , SC.score 
from Student, SC , Course
where Student.S# = SC.S# and SC.C# = Course.C# and SC.C# ='01'and SC.score >=80
orderby Student.S# , SC.C#

--39、求每门课程的学生人数
select Course.C# , Course.Cname , count(*) [学生人数]
from Course , SC
where Course.C# = SC.C#
groupby  Course.C# , Course.Cname
orderby Course.C# , Course.Cname

--40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
--
40.1 当最高分只有一个时
selecttop1 Student.* , Course.Cname , SC.C# , SC.score 
from Student, SC , Course , Teacher
where Student.S# = SC.S# and SC.C# = Course.C# and Course.T# = Teacher.T# and Teacher.Tname = N'张三'
orderby SC.score desc
--40.2 当最高分出现多个时
select Student.* , Course.Cname , SC.C# , SC.score 
from Student, SC , Course , Teacher
where Student.S# = SC.S# and SC.C# = Course.C# and Course.T# = Teacher.T# and Teacher.Tname = N'张三'and
SC.score
= (selectmax(SC.score) from SC , Course , Teacher where SC.C# = Course.C# and Course.T# = Teacher.T# and Teacher.Tname = N'张三')

--41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
--
方法1
select m.*from SC m ,(select C# , score from SC groupby C# , score havingcount(1) >1) n
where m.C#= n.C# and m.score = n.score orderby m.C# , m.score , m.S#
--方法2
select m.*from SC m whereexists (select1from (select C# , score from SC groupby C# , score havingcount(1) >1) n
where m.C#= n.C# and m.score = n.score) orderby m.C# , m.score , m.S#

--42、查询每门功成绩最好的前两名
select t.*from sc t where score in (selecttop2 score from sc where C# = T.C# orderby score desc) orderby t.C# , t.score desc

--43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 
select Course.C# , Course.Cname , count(*) [学生人数]
from Course , SC
where Course.C# = SC.C#
groupby  Course.C# , Course.Cname
havingcount(*) >=5
orderby[学生人数]desc , Course.C#

--44、检索至少选修两门课程的学生学号
select student.S# , student.Sname
from student , SC
where student.S# = SC.S#
groupby student.S# , student.Sname
havingcount(1) >=2
orderby student.S#

--45、查询选修了全部课程的学生信息
--
方法1 根据数量来完成
select student.*from student where S# in
(
select S# from sc groupby S# havingcount(1) = (selectcount(1) from course))
--方法2 使用双重否定来完成
select t.*from student t where t.S# notin
(
 
selectdistinct m.S# from
  (
   
select S# , C# from student , course
  ) m
wherenotexists (select1from sc n where n.S# = m.S# and n.C# = m.C#)
)
--方法3 使用双重否定来完成
select t.*from student t wherenotexists(select1from
(
 
selectdistinct m.S# from
  (
   
select S# , C# from student , course
  ) m
wherenotexists (select1from sc n where n.S# = m.S# and n.C# = m.C#)
) k
where k.S# = t.S#
)

--46、查询各学生的年龄
--
46.1 只按照年份来算
select* , datediff(yy , sage , getdate()) [年龄]from student
--46.2 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一
select* , casewhenright(convert(varchar(10),getdate(),120),5) <right(convert(varchar(10),sage,120),5) thendatediff(yy , sage , getdate()) -1elsedatediff(yy , sage , getdate()) end[年龄]from student

--47、查询本周过生日的学生
select*from student wheredatediff(week,datename(yy,getdate()) +right(convert(varchar(10),sage,120),6),getdate()) =0

--48、查询下周过生日的学生
select*from student wheredatediff(week,datename(yy,getdate()) +right(convert(varchar(10),sage,120),6),getdate()) =-1

--49、查询本月过生日的学生
select*from student wheredatediff(mm,datename(yy,getdate()) +right(convert(varchar(10),sage,120),6),getdate()) =0

--50、查询下月过生日的学生
select*from student wheredatediff(mm,datename(yy,getdate()) +right(convert(varchar(10),sage,120),6),getdate()) =-1

droptable  Student,Course,Teacher,SC

 

转载于:https://www.cnblogs.com/lrdekuaile/p/3147763.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值