码迷,mamicode.com
首页 > 数据库 > 详细

sql查询一个面试题的7步解法及语法区别分析讲解

时间:2015-03-29 14:49:28      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:

本篇讲述的是对一个sql面试题的细致语法讲解。关于执行流程(on where),内连接,外连接(左右)上实用。关于这些基本的语法知识请参考我前面的sql基本语法。

这张表的名字SC,SNO学号,CNO课程号,SCGRADE成绩。要求:列出“1”号课程成绩比“2”号课程成绩高的所有学生学号及其“1”号课程和“2”号课程的成绩

技术分享

要求:列出“1”号课程成绩比“2”号课程成绩高的所有学生学号及其“1”号课程和“2”号课程的成绩

1,分别查出1 2号课程的所有列表(包括学号,课程,成绩)。

2,条件1课程表的成绩>2课程表的成绩。两表连接查询。

3,隐士的条件,这两个表中的学号相等。两表连接查询。

4,select 表1或表2的序号,表1.成绩,表2.成绩。

5,分别起别名。

技术分享
SELECT  A.SNO as 学号,A.SCGRADE AS 1号课程,b.SCGRADE as 2号课程 FROM
(SELECT *FROM SC WHERE CNO=1)AS A CROSS JOIN (SELECT * FROM SC WHERE CNO=2)as b 
WHERE A.SCGRADE>b.SCGRADE AND A.SNO=b.SNO;


SELECT  A.SNO as 学号,A.SCGRADE AS 1号课程,b.SCGRADE as 2号课程 FROM
(SELECT SNO,SCGRADE FROM SC WHERE CNO=1)AS A CROSS JOIN (SELECT SNO,SCGRADE FROM SC WHERE CNO=2)as b 
WHERE A.SCGRADE>b.SCGRADE AND A.SNO=b.SNO;

SELECT  A.SNO as 学号,A.SCGRADE AS 1号课程,b.SCGRADE as 2号课程 FROM
(SELECT SNO,SCGRADE FROM SC WHERE CNO=1)AS A INNER JOIN (SELECT SNO,SCGRADE FROM SC WHERE CNO=2)as b 
ON A.SCGRADE>b.SCGRADE AND A.SNO=b.SNO;

SELECT  A.SNO as 学号,A.SCGRADE AS 1号课程,b.SCGRADE as 2号课程 FROM
(SELECT SNO,SCGRADE FROM SC WHERE CNO=1)AS A INNER JOIN (SELECT SNO,SCGRADE FROM SC WHERE CNO=2)as b 
ON A.SCGRADE>b.SCGRADE WHERE A.SNO=b.SNO;


SELECT  A.SNO as 学号,A.SCGRADE AS 1号课程,b.SCGRADE as 2号课程 FROM
(SELECT SNO,SCGRADE FROM SC WHERE CNO=1)AS A LEFT JOIN (SELECT SNO,SCGRADE FROM SC WHERE CNO=2)as b 
ON A.SCGRADE>b.SCGRADE AND A.SNO=b.SNO;

SELECT  A.SNO as 学号,A.SCGRADE AS 1号课程,b.SCGRADE as 2号课程 FROM
(SELECT SNO,SCGRADE FROM SC WHERE CNO=1)AS A LEFT JOIN (SELECT SNO,SCGRADE FROM SC WHERE CNO=2)as b 
ON A.SCGRADE>b.SCGRADE WHERE A.SNO=b.SNO;


SELECT  A.SNO as 学号,A.SCGRADE AS 1号课程,b.SCGRADE as 2号课程 FROM
(SELECT SNO,SCGRADE FROM SC WHERE CNO=1)AS A RIGHT JOIN (SELECT SNO,SCGRADE FROM SC WHERE CNO=2)as b 
ON A.SCGRADE>b.SCGRADE AND A.SNO=b.SNO;
所有方法

第一种:

SELECT A.SNO as ‘学号‘,A.SCGRADE AS ‘1号课程‘,b.SCGRADE as ‘2号课程‘ FROM
(SELECT *FROM SC WHERE CNO=1)AS A CROSS JOIN (SELECT * FROM SC WHERE CNO=2)as b 
WHERE A.SCGRADE>b.SCGRADE AND A.SNO=b.SNO;

技术分享

第二种:

SELECT  A.SNO as ‘学号‘,A.SCGRADE AS ‘1号课程‘,b.SCGRADE as ‘2号课程‘ FROM
(SELECT SNO,SCGRADE FROM SC WHERE CNO=1)AS A CROSS JOIN (SELECT SNO,SCGRADE FROM SC WHERE CNO=2)as b 
WHERE A.SCGRADE>b.SCGRADE AND A.SNO=b.SNO;

技术分享  

第三种

SELECT  A.SNO as ‘学号‘,A.SCGRADE AS ‘1号课程‘,b.SCGRADE as ‘2号课程‘ FROM
(SELECT SNO,SCGRADE FROM SC WHERE CNO=1)AS A INNER JOIN (SELECT SNO,SCGRADE FROM SC WHERE CNO=2)as b 
ON A.SCGRADE>b.SCGRADE AND A.SNO=b.SNO;

 技术分享

第四种:

SELECT  A.SNO as ‘学号‘,A.SCGRADE AS ‘1号课程‘,b.SCGRADE as ‘2号课程‘ FROM
(SELECT SNO,SCGRADE FROM SC WHERE CNO=1)AS A INNER JOIN (SELECT SNO,SCGRADE FROM SC WHERE CNO=2)as b 
ON A.SCGRADE>b.SCGRADE WHERE A.SNO=b.SNO;

技术分享  

第五种:

SELECT  A.SNO as ‘学号‘,A.SCGRADE AS ‘1号课程‘,b.SCGRADE as ‘2号课程‘ FROM
(SELECT SNO,SCGRADE FROM SC WHERE CNO=1)AS A LEFT JOIN (SELECT SNO,SCGRADE FROM SC WHERE CNO=2)as b 
ON A.SCGRADE>b.SCGRADE AND A.SNO=b.SNO;

技术分享  

第六种:

SELECT  A.SNO as ‘学号‘,A.SCGRADE AS ‘1号课程‘,b.SCGRADE as ‘2号课程‘ FROM
(SELECT SNO,SCGRADE FROM SC WHERE CNO=1)AS A LEFT JOIN (SELECT SNO,SCGRADE FROM SC WHERE CNO=2)as b 
ON A.SCGRADE>b.SCGRADE WHERE A.SNO=b.SNO;

技术分享 

第七种:

SELECT  A.SNO as ‘学号‘,A.SCGRADE AS ‘1号课程‘,b.SCGRADE as ‘2号课程‘ FROM
(SELECT SNO,SCGRADE FROM SC WHERE CNO=1)AS A RIGHT JOIN (SELECT SNO,SCGRADE FROM SC WHERE CNO=2)as b 
ON A.SCGRADE>b.SCGRADE AND A.SNO=b.SNO;

技术分享  

方法1和2之间区别是两个连接表的查询字段的多余与否。

方法3演示了inner jion和on的连接使用,并和cross jion的区别。

方法4和3演示了内连接流程顺序,先from内(包含on)走完,再where。内连接和on where的功能相同,但顺序不同。

方法5演示纯的左连接和on多条件使用。

方法6和5演示了左链接流程顺序,先from内(包含on)走完,再where。内连接和on where的功能相同,但顺序不同。

方法7右连接的on多条件使用。

 

这些区别已经区分非常清楚。具体语法请看我前几篇关于数据库的基本语法文章。

当然,这些不是什么前沿技术,但很细致,对不对?

 

  

 

sql查询一个面试题的7步解法及语法区别分析讲解

标签:

原文地址:http://www.cnblogs.com/leee/p/4375598.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!