sql server 基础

1 .左连接 

select a.* ,b.*

from student as a
left join

hobby as b
on a.hobbyid=b.hobbyid

2. 右 连接

select a.* ,b.*

from student as a
right join

hobby as b
on a.hobbyid=b.hobbyid

查询结果

3.    用group by, having 实现  

  ------ (1)查询各个商品 售出多少件

   -----(2)查询卖出大于100元的商品 (使用 having)

   ----(3)统计各个用户销售后金额

 

   (1) 查询各个商品卖出多少件 

          select a.productId,sum(a.quantity) as 商品个数

           from shoping  as a

          group by a.productId 

      

 

(2)查询卖出100元的商品 

(3)

4. 求 学生的平均成绩,最高分,最低分

5.开窗函数 over()   。    在聚合函数后面 加over 

 

显示 各位同学的成绩,以及平均分

 select * ,  AVG(score) o ver ()as 平均分

 

from studentscore

6. 查询名字 出现次数 大于 2 的 名字

7. case when ...then ...else.. end语法

 没有 加 case when then else end 时 

select a.stuname,a.sex

from student as a  

加了之后 

select a.stuname as 姓名,

case when a.sex='m'then '男' else '女' end as 性别

from student as a

8.distinct 语句的用法 

加了 distinct 后

 8. top语句的用法 

 9.排序是最后执行的 。在 from,where 之后 。 order by

降序排序

10 top语句 和 order by语句的配合 

 例: 取年级最小的 5个学生的信息

select top 5 * from fenshu order by age

 

一般 sql 语句的执行顺序 

from---> where -->group by --> having-->select -->order by 。 order by 可以和 top语句一起用。

一般来来说:from--> group  by-->having --->select 这个顺序用的朵颐写

原文地址:https://www.cnblogs.com/bingyizhihun/p/9522459.html