本篇讲述的是对一个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多条件使用。
这些区别已经区分非常清楚。具体语法请看我前几篇关于数据库的基本语法文章。
当然,这些不是什么前沿技术,但很细致,对不对?
时间: 2024-11-02 18:36:47