当前位置:首页 > 数据库 > Sqlserver

SQL Server 具体查询要点,自己整理

            1,distinct 对某一列进行去重
检索有职工的工资大于或等于wh1仓库中任何一名职工工资的仓库号
selectdistinct cno from zhigongbiao where gongzi >= (selectMin(gongzi) from zhigongbiao where cno in (wh1))
2 ,betweenand   在...之间
检索出工资在1220元到1240元范围内的职工信息。
select*from zhigongbiao where gongzi between1220and12403,orderbydesc ,asc  将一列进行升序或降序排列
先按仓库号排序,再按工资排序并输出全部职工信息
select*from zhigongbiao orderby cno asc,gongzi desc4,joinon     表连接,表示横向表连接
找出工作在面积大于400的仓库的职工号以及这些职工工作所在的城市
select zno ,city from cangkubiao join zhigongbiao on cangkubiao.cno=zhigongbiao .cno where mianji>4005, union  表连接,表示纵向表链接
union 中的所有选择列表必须具有相同的列数,相似的数据类型和相同的顺序
结果集中的列名来自第一个select 语句
6, 相关子查询, 
列出每个职工经手的具有最高总金额的订购单信息   --重点题目,相关子查询 select*from dingdanbiao a where dingdanzongjia  notin (selectMAX(dingdanzongjia) from dingdanbiao b where a.zno=b.zno) 
7, 聚合函数
count ,avg ,sum, max,min8,groupby  对制定列进行分组   having 在分组的基础上进行进一步筛选,一般与 groupby 子句配合使用 
求至少有两个职工的每个仓库的平均工资
select cno,AVG (gongzi) 平均工资 from zhigongbiao groupby cno havingCOUNT(*)>=19,子查询(嵌套查询)
使用查询语句查询一列数据出来,然后作为其他查询条件中参数来使用
检索有职工的工资大于或等于wh1仓库中所有职工工资的仓库号。
select cno from zhigongbiao where gongzi >=(selectMax(gongzi) from zhigongbiao where cno in (wh1))
10, 时间和日期函数
datediff  功能是获取两个日期之间的值
查询Student表中每个学生的姓名和年龄。
select sname,DATEDIFF(YEAR,sbirthdy,getdate()) age from student
day(date),取指定日期的日    month(date),取制定日期的月  year(date) 取制定日期的年
查询和学号为3的同学同年出生的所有学生的Sno、Sname和Sbirthday列。 --重点题目select sno,sname,sbirthdy from student whereYEAR(sbirthdy)=(selectYEAR(sbirthdy)from student where sno=3)
11,in ,like,=,>,< 等,还有 top 子句,查询前几行
  查询Score表中的最高分的学生学号和课程号。(子查询或者排序)
select sno,cno from score where degree=(selectMAX(degree) from score)
selecttop1* sno,cno from score orderby degree desc

 

原文:http://www.cnblogs.com/275147378abc/p/4457431.html


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐