基本查询
use Sales
go
[]里是可选的
select distinct
from 表名
[where 条件表达式]
[group by 分组列]
[having 条件表达式]
[order by 排序列 asc/desc]
[compute 聚合函数 by 分组列]
select * from Goods
select * from Employees
select 商品名称,进货价,零售价 from Goods
select 部门 from Employees
select distinct 部门 from Employees
select top 1 * from Employees
select top 5 * from Employees
select top 50 percent * from Employees
select * from Employees where 性别=1
select * from Employees where 部门='采购部' or 部门='销售部'
select * from Employees where 部门 in ('采购部','销售部')
select * from Goods where 零售价 >=2000
select * from Goods where 零售价 >=2000 and 零售价<=5000
select * from Goods where 零售价 between 2000 and 5000
select * from Employees where 姓名='李建国'
select * from Employees where 姓名 like '李%'
select * from Employees where 姓名 like '[李]%'
select * from Employees where 姓名 like '[李,王]%'
select * from Employees where 姓名 like '李_'
select * from Employees where 姓名 not like '李%'
select * from Employees where 姓名 like '[^李]%'
select * from Employees where 姓名 like '[^李,王]%'
select * from Employees where 姓名 like '燕%'
select top 1 * from Goods
select *from goods order by 零售价
select *from goods order by 零售价 desc
select top 1 *from goods order by 零售价
select top 1 *from goods order by 零售价 desc
select 部门, count (*) 人数 from Employees group by 部门
select 部门, count (*) as 人数 from Employees group by 部门
select 部门, 人数=count (*) from Employees group by 部门
select 部门, 人数=count (*) from Employees group by 部门 having count(*)>2
select sum(数量) from Sell
select 商品编号 ,sum(数量) 总计 from Sell group by 商品编号
select 商品编号 ,sum(数量) 总计 from Sell group by 商品编号 having sum(数量)>=3
select top 3 商品编号 ,sum(数量) 总计 from Sell group by 商品编号 order by sum(数量) desc
select top 3 商品编号 ,sum(数量) 总计 from Sell group by 商品编号 order by sum(数量) asc
select top 3 商品编号 ,sum(数量) 总计 from Sell group by 商品编号 order by 总计 desc
count(*/ all/distinct 列名)
sum(列名)
avg(列名)
max() min()
use Sales
go
select count(*) 总人数 from Employees
select count(姓名) 总人数 from Employees
select count(distinct 部门) 部门数目 from Employees
select sum(进货价) 总价 ,avg(进货价) 平均 ,max(进货价) 进货最大值 ,min(进货价) 进货最小值 from Goods
select 商品名称, 进货价, 零售价, 数量 , (零售价-进货价)*数量 盈利 from Goods
select sum((零售价-进货价)*数量 ) 总盈利 from Goods
select 性别 ,count(性别) 人数 from Employees group by 性别
select 部门, count(部门) 各部门人数 from Employees group by 部门
select 进货员工编号 , sum((零售价-进货价)*数量 ) 销售盈利 from Goods group by 进货员工编号
select top 1 进货员工编号 , sum((零售价-进货价)*数量 ) 销售盈利 from Goods group by 进货员工编号 order by 销售盈利 desc
select 进货员工编号 , sum((零售价-进货价)*数量 ) 销售盈利 from Goods group by 进货员工编号 having sum((零售价-进货价)*数量 )>=20000
select * from Goods
select *from Goods where 进货时间<='2005-01-01' and 进货价<1000
select * from Employees [compute] count (部门)
select * from Employees order by 部门 compute count (部门) by 部门
select *,(零售价-进货价)*数量 盈利 from Goods order by 进货员工编号 compute sum((零售价-进货价)*数量 ) by 进货员工编号
连接查询
use Sales
go
select * from Goods
select * from Sell
select * from Goods G inner join Sell S on G.商品编号=S.商品编号
select 零售价,S.数量 ,售货员工编号 from Goods G inner join Sell S on G.商品编号=S.商品编号
select 零售价,S.数量 ,零售价*S.数量 销售额 ,售货员工编号 from Goods G inner join Sell S on G.商品编号=S.商品编号
select '1301' 售货员工编号, sum(零售价*S.数量) 销售总额 from Goods G inner join Sell S on G.商品编号=S.商品编号 where 售货员工编号='1301'
select '1302' 售货员工编号, sum(零售价*S.数量) 销售总额 from Goods G inner join Sell S on G.商品编号=S.商品编号 where 售货员工编号='1302'
select '1303' 售货员工编号, sum(零售价*S.数量) 销售总额 from Goods G inner join Sell S on G.商品编号=S.商品编号 where 售货员工编号='1303'
select 售货员工编号,sum(零售价*S.数量) 销售额 from Goods G inner join Sell S on G.商品编号=S.商品编号 group by 售货员工编号
select 售货员工编号,sum(零售价*S.数量) 销售额 from Goods G , Sell S where G.商品编号=S.商品编号 group by 售货员工编号
select 售货员工编号, 姓名,sum(零售价*S.数量) 销售额 from Goods G inner join Sell S on G.商品编号=S.商品编号 inner join Employees E on S.售货员工编号=E.编号 group by 售货员工编号, 姓名
select 售货员工编号, 姓名,sum(零售价*S.数量) 销售额 from Goods G , Sell S , Employees E
where G.商品编号=S.商品编号 and S.售货员工编号=E.编号 group by 售货员工编号, 姓名
select * from Employees e inner join Sell s on e.编号=s.售货员工编号
select * from Employees e left join Sell s on e.编号=s.售货员工编号 where 售货员工编号 is null
select e.* from Employees e left join Sell s on e.编号=s.售货员工编号 where 售货员工编号 is null
select 编号,姓名 from Employees e left join Sell s on e.编号=s.售货员工编号 where 售货员工编号 is null
select * from Sell s right join Employees e on e.编号=s.售货员工编号
select * from Sell s full join Employees e on e.编号=s.售货员工编号
select 11*13
select * from Employees ,Sell
select * from Employees cross join Sell
select * from Employees cross join Sell where 性别=1
select * from Employees cross join Sell order by 编号 desc
select count(*) 超过5门的人数 from (select StuNo 学号,StuName 姓名 from V_StuCou group by StuNo,StuName having count(CouNo)>=5 ) 超过5门的人数
子查询、合并查询
use Sales
go
select * from Employees
select * from Goods
select * from Sell
select 部门 from Employees where 姓名='赵飞燕'
select 姓名 from Employees where 部门='采购部' and 姓名 != '赵飞燕'
select 姓名 from Employees where 部门='采购部' and 姓名 <> '赵飞燕'
select 姓名 from Employees where 部门=(select 部门 from Employees where 姓名='赵飞燕') and 姓名 <> '赵飞燕'
select distinct 售货员工编号, 姓名 from Employees E inner join Sell s on E.编号=S.售货员工编号
select distinct 售货员工编号, 姓名 from Employees E , Sell s where E.编号=S.售货员工编号
select distinct 售货员工编号 ,姓名 from Employees E left join Sell S on E.编号=S.售货员工编号 where 售货员工编号 is not null
select distinct 售货员工编号, 姓名 from Employees E cross join Sell S where E.编号=S.售货员工编号
select 售货员工编号 from Sell
select distinct 售货员工编号 from Sell
select 编号, 姓名 from Employees where 编号 in (select distinct 售货员工编号 from Sell )
select 编号, 姓名 from Employees where 编号 not in (select distinct 售货员工编号 from Sell )
select 编号, 姓名 from Employees where 编号 in (select distinct 售货员工编号 from Sell )
select 编号, 姓名 from Employees where exists (select * from Sell where 售货员工编号=Employees.编号)
if exists(select * from sysdatabases where name='Sales')
use Sales
go
select 编号, 姓名 from Employees where not exists (select * from Sell where 售货员工编号=Employees.编号)
insert into 表名
select 各列值 union
select 各列值 union
select 各列值 union
select 各列值 union
go
use Sales
go
select 编号 ,姓名 from Employees where 编号 in(select distinct 售货员工编号 from sell)
union
select 编号 ,姓名 from Employees where 编号 NOT in(select distinct 售货员工编号 from sell)
select 编号,姓名 from Employees where 编号 in(select distinct 售货员工编号 from sell)
intersect
select 编号 ,姓名 from Employees where 编号 NOT in(select distinct 售货员工编号 from sell)
select 编号,姓名 from Employees
intersect
select 编号,姓名 from Employees where 编号 NOT in(select distinct 售货员工编号 from sell)
select 编号,姓名 from Employees
except
select 编号 ,姓名 from Employees where 编号 in(select distinct 售货员工编号 from sell)
select 编号,姓名 from Employees
except
select 编号,姓名 from Employees inner join sell on Employees.编号=Sell.售货员工编号
XK数据库的查询
use XK
go
select * from Department
select * from Class
select * from Student
select * from Course
select * from StuCou
select * from Class
select count(ClassNo) 班级数 from Class
select count(ClassNo) 班级数 from Class where ClassNo like '2001%'
select Departname from Department where DepartNo='03'
select Departname from Department where Departname like '%工程%'
select CouName , Teacher from Course where SchoolTime like '周二晚%'
select * from Student where StuName like '[张,陈,黄]%' order by StuName desc
select DepartName 部门名称 ,convert(decimal(5,0),avg(WillNum)) 平均报名人数 from Department D inner join Course C on D.DepartNo=C.DepartNo group by DepartName
select D.DepartNo 部门编号, DepartName 部门名称, count(ClassNo) 班级数量 from Department D , Class C where D.DepartNo=C.DepartNo group by D.DepartNo ,DepartName
select D.DepartNo 部门编号, DepartName 部门名称, count(ClassNo) 班级数量 from Department D , Class C where D.DepartNo=C.DepartNo group by D.DepartNo ,DepartName having count(ClassNo)>=5
select CouName , Credit ,SchoolTime ,WillOrder from Student Sd , StuCou SC , Course C where Sd.StuNo=SC.StuNo and SC.CouNo=C.CouNo and StuName='甘蕾' order by WillOrder
select S.StuNo ,StuName, SC.CouNo , CouName,WillOrder from Student S , StuCou SC , Course C
where S.StuNo=SC.StuNo and SC.CouNo=C.CouNo and S.ClassNo= (select ClassNo from Class where ClassName='00电子商务' ) order by StuNo,WillOrder asc
select S.StuNo ,StuName, SC.CouNo ,CouName,WillOrder from Student S , StuCou SC , Course C
where S.StuNo=SC.StuNo and SC.CouNo=C.CouNo and S.ClassNo= (select ClassNo from Class where ClassName='00电子商务' )
order by S.StuNo,WillOrder asc compute count (S.StuNo) by S.StuNo
select S.StuNo ,StuName, SC.CouNo ,CouName,WillOrder from Class Cl,Student S , StuCou SC , Course C
where Cl.ClassNo=S.ClassNo and S.StuNo=SC.StuNo and SC.CouNo=C.CouNo and ClassName='00电子商务'
order by S.StuNo,WillOrder asc compute count (S.StuNo) by S.StuNo
select D.DepartNo 部门编号,min(WillNum) 最少报名人数 ,max(WillNum) 最多报名人数,round(avg(WillNum) ,2)平均报名人数,sum(WillNum) 报名总人数 from Department D,Course C where D.DepartNo=C.DepartNo group by D.DepartNo
select StuName 姓名,count(CouNo) 选修门数 from Student S ,StuCou SC where S.StuNo=SC.StuNo group by StuName having count(CouNo) =(select count(CouNo) 选修门数 from Student S ,StuCou SC where S.StuNo=SC.StuNo and StuName='陈婷' group by StuName )
select S.StuNo 学号 ,StuName 姓名 from Student S left join StuCou SC on S.StuNo=SC.StuNo where CouNo is null
select StuNo , StuName from Student where StuNo in (select distinct StuNo from StuCou )
select StuNo , StuName from Student where exists (select StuNo from StuCou where StuNo=Student.StuNo )
select StuNo , StuName from Student
except
select StuNo , StuName from Student where StuNo in (select distinct StuNo from StuCou )
select S.StuNo 学号 ,StuName 姓名 from Student S left join StuCou SC on S.StuNo=SC.StuNo where CouNo is null
union
select StuNo , StuName from Student where StuNo in (select distinct StuNo from StuCou )