聚合函數 count,max,min,avg,sum...
select count (*) from T_Employee
select Max(FSalary) from T_Employee
排序 ASC升序 DESC降序
select * from T_Employee order by Fage
先按年齡降序排列。如果年齡相同,則按薪水升序排列
select * from T_Employee order by FAge DESC,FSalary ASC
order by 要放在 where 子句之後
通配符過濾
通配符過濾用like
單字符通配符‘_'
多字符通配符‘%'
select * from T_Employee where FName like '_erry'
NULL 是不知道的意思,而不是沒有
用SQL語句查詢NULL的數據不能用=或<> 而用is NULL或者is not NULL
select * from T_Employee where FName is NULL
in(23,25)同時匹配兩個值。相當於 23 or 25
between 20 and 30 匹配介於20到30之間的數
group by分組
select FAge, count(*) from T_Employee
Group by Fage
先把相同的Fage分一組,再統計每一組的個數
group by子句要放在where子句之後。如果想取某個年齡段人數大於1的,不能用where count(*) > 1 ,因為聚合函數不能放在where子句之後。要用having子句
Having是對分組後的列進行過濾,能用的列和select中的一樣。如下例中則不能用having Fsalary>2000 只能用where Fsalary>2000
select FAge, count(*) from T_Employee
Group by FAge
having count(*) > 1;
限制結果集的范圍
select Top 3 * from T_Employee
order by FSalary DESC
從第六名開始選3個.2005後可以用Row_Number函數
select Top 3 * from T_Employee
where FNumber not in(select TOP 5 FNumber from T_Employee order by FSalary DESC)
order by FSalary DESC