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

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

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

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

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

 

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

对例1用自然连接完成

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

1 SELECT STUDENT.SNAME,STUDENT.SSEX,STUDENT.SAGE,STUDENT.SDEPT,SC.* 
2 FROM SC,STUDENT        
3 WHERE SC.SNO = STUDENT.SNO;


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

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

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

1 SELECT SY.CNO,SX.CPNO FROM COURSE SX,COURSE SY WHERE SX.CNO = SY.CPNO

 

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

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

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

1 SELECT STUDENT.SNAME,STUDENT.SSEX,STUDENT.SAGE,STUDENT.SDEPT,SC.* 
2 FROM   STUDENT LEFT JOIN SC ON(SC.SNO = STUDENT.SNO);


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

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

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

 

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

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

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

1 SELECT SX.* 
2 FROM STUDENT SX    
3 WHERE SX.SDEPT IN  ( 
4              SELECT SY.SDEPT FROM STUDENT SY WHERE SY.SNAME='刘晨' 
5 ); 


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

或者如下:

1 SELECT SX.* 
2 FROM STUDENT SX, STUDENT SY       
3 WHERE SX.SDEPT = SY.SDEPT AND SY.SNAME = '刘晨';  

或者:

1 SELECT * 
2 FROM STUDENT SX 
3 WHERE EXISTS ( 
4          SELECT * FROM STUDENT SY WHERE SY.SDEPT = SX.SDEPT AND SY.SNAME='刘晨'
5 );  

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

 

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

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

1 SELECT SNO, SNAME 
2 FROM STUDENT        
3 WHERE SNO IN  ( 
4         SELECT SNO FROM SC 
5         WHERE  CNO IN  ( 
6                     SELECT CNO FROM COURSE
7                     WHERE CNAME = '信息系统'
8                     ) 
9 );   


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

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

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

1 SELECT SX.SNO,SX.CNO 
2 FROM SC SX 
3 WHERE SX.GRADE  >  (
4                        SELECT AVG(GRADE) 
5                        FROM SC SY               
6                        WHERE SX.SNO = SY.SNO
7 );

 

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

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

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

1 SELECT SNAME, SAGE 
2 FROM STUDENT    
3 WHERE SAGE < ANY  (
4                       SELECT SAGE 
5                       FROM STUDENT  
6                       WHERE SDEPT='CS'
7                    )
8       AND SDEPT<>'CS';

 

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

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

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

1 SELECT SNAME, SAGE 
2 FROM STUDENT    
3 WHERE SAGE < ALL  (
4                       SELECT SAGE 
5                       FROM STUDENT  
6                       WHERE SDEPT='CS'  
7                    )
8       AND SDEPT<>'CS';


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

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

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

1 SELECT SNAME 
2 FROM STUDENT    
3 WHERE EXISTS   ( 
4                   SELECT * FROM SC            
5                   WHERE SC.SNO = STUDENT.SNO AND SC.CNO='1'           
6 );

 

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

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

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

 1 SELECT SNAME 
 2 FROM STUDENT 
 3 WHERE NOT EXISTS ( 
 4                     SELECT * 
 5                     FROM COURSE 
 6                     WHERE NOT EXISTS   ( 
 7                                           SELECT * 
 8                                           FROM SC 
 9                                           WHERE STUDENT.SNO = SC.SNO AND COURSE.CNO = SC.CNO
10                                         )
11 );


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

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

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

 1 SELECT DISTINCT SNO 
 2 FROM SC X     
 3 WHERE NOT EXISTS ( 
 4                     SELECT * 
 5                     FROM SC Y 
 6                     WHERE SNO='200215122' AND NOT EXISTS ( 
 7                                                             SELECT * 
 8                                                             FROM SC Z 
 9                                                             WHERE Z.SNO = X.SNO AND Y.CNO = Z.CNO              
10                                                           )
11 ); 


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

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

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

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

 

posted @ 2012-10-15 20:42  bluepoint2009  阅读(1247)  评论(0编辑  收藏  举报