SQL查询效率

简介:

 

 
  1. select * from student inner join sc on student.sno = sc.sno  
  2.  
  3. set statistics io on 
  4.  
  5. select student.sno,student.sname,sc.cno,sc.grade from student join sc on student.sno=sc.sno where student.sdept='计算机' 
  6. --以下这两个sql实现的功能是一样的,只是写的方法不一样  
  7. select sname,cno,grade from student,sc where sc.sno = student.sno and student.sdept='计算机' 
  8. select student.sname,sc.cno,sc.grade from student join sc on student.sno = sc.sno where student.sdept='计算机'   
  9.  
  10. select cno,grade from sc where sc.sno<>990001  
  11. --以下三条sql语句第一句开销最小,说明效率最高,后面两条开销一样  
  12. select grade from sc where sno=(select sno from student where sname='张忠和'and cno = (select cno from course where cname='软件工程')  
  13. --T-SQL写法  
  14. select student.sname,student.sno,sc.grade,sc.cno,course.cname from student,sc,course where student.sno=sc.sno and sc.cno=course.cno and student.sname='张忠和' and course.cname='软件工程' 
  15. --ANSI SQL92写法  
  16. select student.sname,student.sno,sc.grade,sc.cno,course.cname from student   
  17. join sc on student.sno=sc.sno --这里要注意了join与join之间连接的时候没有任何的连接符也没有and之类的  
  18. join course on sc.cno=course.cno   
  19. where student.sname='张忠和' and course.cname='软件工程' 
  20.  
  21. select * into student1 from student  
  22.  
  23. select * into student2 from student union select * from student1  
  24.  
  25. select * from student where sno in (select sno from sc where cno='001')  
  26. select * from student where exists (select * from sc where sno=student.sno and cno='001')  
  27.  
  28. select * from student a where exists (select * from student b where a.sdept=b.sdept and b.sname='张三')  
  29.  
  30. select *from student where sno not in(select sno from sc where cno=001)  
  31.  
  32. select * from student where not exists (select sno from sc where student.sno=sc.sno and cno='001')  

 本文转自sucre03 51CTO博客,原文链接:http://blog.51cto.com/sucre/420273,如需转载请自行联系原作者

相关文章
|
1月前
|
SQL
sql语句加正则 简化查询
sql语句加正则 简化查询
16 0
sql语句加正则 简化查询
|
2月前
|
SQL
sql server链接查询
sql server链接查询
18 1
|
2月前
|
SQL
sql server简单查询
sql server简单查询
15 1
|
1月前
|
SQL 关系型数据库 MySQL
mysql一条sql查询出多个统计结果
mysql一条sql查询出多个统计结果
16 0
|
2月前
|
SQL
sql高级查询
sql高级查询
15 0
|
2月前
|
SQL 存储 数据可视化
10个高级的 SQL 查询技巧
10个高级的 SQL 查询技巧
|
3天前
|
SQL 前端开发
基于jeecgboot复杂sql查询的列表自定义列实现
基于jeecgboot复杂sql查询的列表自定义列实现
|
6天前
|
SQL 数据库
SQL数据库基础语法-查询语句
SQL数据库基础语法-查询语句
|
7天前
T-sql 高级查询( 5*函数 联接 分组 子查询)
T-sql 高级查询( 5*函数 联接 分组 子查询)
|
7天前
|
机器学习/深度学习
T-sql 各种查询命令
T-sql 各种查询命令