Home > Database > Mysql Tutorial > SQL Server 数据查询 --- 复杂查询

SQL Server 数据查询 --- 复杂查询

WBOY
Release: 2016-06-07 17:49:29
Original
995 people have browsed it

SQL Server 数据查询 --- 复杂查询 有需要的朋友可参考一下。

/********************************************

例1查询每个学生及其选修课程的情况

*********************************************/

 代码如下 复制代码

SELECT STUDENT.*,SC.* FROM SC,STUDENT WHERE SC.SNO = STUDENT.SNO;

/********************************************

对例1用自然连接完成

*********************************************/

 代码如下 复制代码
 SELECT STUDENT.SNAME,STUDENT.SSEX,STUDENT.SAGE,STUDENT.SDEPT,SC.*
 FROM SC,STUDENT       
 WHERE SC.SNO = STUDENT.SNO;

/*****************************************

查询每一门课程的间接先修课

*****************************************/

 

 代码如下 复制代码
SELECT SY.CNO,SX.CPNO FROM COURSE SX,COURSE SY WHERE SX.CNO = SY.CPNO

/********************************************

对例1用左外连接连接完成

*********************************************/

 代码如下 复制代码
 SELECT STUDENT.SNAME,STUDENT.SSEX,STUDENT.SAGE,STUDENT.SDEPT,SC.*
 FROM   STUDENT LEFT JOIN SC ON(SC.SNO = STUDENT.SNO);

/********************************************

查询每个学生的学号、姓名、选修的课程名及成绩 *********************************************/

 代码如下 复制代码

 SELECT STUDENT.SNO,STUDENT.SNAME,COURSE.CNAME,COURSE.CCREDIT
 FROM SC,STUDENT,COURSE   
 WHERE SC.CNO =COURSE.CNO AND SC.SNO = STUDENT.SNO;

/********************************************  

查询与刘晨在同一个系学习的学生

*********************************************/

 代码如下 复制代码

 SELECT SX.*
 FROM STUDENT SX   
 WHERE SX.SDEPT IN  (
              SELECT SY.SDEPT FROM STUDENT SY WHERE SY.SNAME='刘晨'
 );
/************   

或者如下:

 代码如下 复制代码

 SELECT SX.*
 FROM STUDENT SX, STUDENT SY      
 WHERE SX.SDEPT = SY.SDEPT AND SY.SNAME = '刘晨';  或者:

 SELECT *
 FROM STUDENT SX
 WHERE EXISTS (
          SELECT * FROM STUDENT SY WHERE SY.SDEPT = SX.SDEPT AND SY.SNAME='刘晨'
 );  **************/

 

/********************************************  

查询选修了课程名为"信息系统"的学生学号和姓名 *********************************************/

 

 代码如下 复制代码
 SELECT SNO, SNAME
 FROM STUDENT       
 WHERE SNO IN  (
         SELECT SNO FROM SC
         WHERE  CNO IN  (
                     SELECT CNO FROM COURSE
                     WHERE CNAME = '信息系统'
                    )
 );  

/********************************************  

找出每个学生超过他选修课程平均成绩的课程号

*********************************************/

 

 代码如下 复制代码

SELECT SX.SNO,SX.CNO
 FROM SC SX
 WHERE SX.GRADE  >  (
                        SELECT AVG(GRADE)
                       FROM SC SY              
                        WHERE SX.SNO = SY.SNO
);

/*****************************************************  

查询其他系中比计算机科学系某一学生年龄小的学生姓名和年龄

*******************************************************/

 代码如下 复制代码


 SELECT SNAME, SAGE
 FROM STUDENT   
 WHERE SAGE                        SELECT SAGE
                       FROM STUDENT 
                       WHERE SDEPT='CS'
                    )
       AND SDEPT'CS';

/*****************************************************  

查询其他系中比计算机科学系所有学生年龄小的学生姓名和年龄

*******************************************************/

 代码如下 复制代码

 SELECT SNAME, SAGE
 FROM STUDENT   
 WHERE SAGE                        SELECT SAGE
                      FROM STUDENT 
                       WHERE SDEPT='CS' 
                    )
       AND SDEPT'CS';

/*****************************************************  

查询所有选修了1号课程的学生姓名

*******************************************************/

 代码如下 复制代码


 SELECT SNAME
 FROM STUDENT   
 WHERE EXISTS   (
                   SELECT * FROM SC           
                   WHERE SC.SNO = STUDENT.SNO AND SC.CNO='1'          
 );

/******************************************

查询选修了全部课程的学生姓名

*******************************************/

 代码如下 复制代码

 SELECT SNAME
 FROM STUDENT
 WHERE NOT EXISTS (
                     SELECT *
                     FROM COURSE
                     WHERE NOT EXISTS   (
                                           SELECT *
                                           FROM SC
                                           WHERE STUDENT.SNO = SC.SNO AND COURSE.CNO = SC.CNO
                                        )
 );

/*****************************************************

查询至少选修了学生200215122选修的全部课程的学生号码

*****************************************************/

 代码如下 复制代码

 SELECT DISTINCT SNO
 FROM SC X    
 WHERE NOT EXISTS (
                     SELECT *
                     FROM SC Y
                    WHERE SNO='200215122' AND NOT EXISTS (
                                                            SELECT *
                                                            FROM SC Z
                                                             WHERE Z.SNO = X.SNO AND Y.CNO = Z.CNO             
                                                           )
 );

/**********************************************************

从自身表中选择一条记录,修改某个字段再回插到自身表中

这里的INSERT INTO 可以用来插入子查询 *********************************************************/

 代码如下 复制代码

 INSERT INTO COURSE(CNO,CNAME,CPNO,CCREDIT)
       SELECT '8',CNAME,CPNO,CCREDIT FROM  COURSE WHERE  CNO='2';

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template