復制代碼 代碼如下:
--執行順序 From Where Select
select * from
(select sal as salary,comm as commission from emp ) x where salary<5000
--得出 Name Work as a Job
select ename +' Work as a'+job as msg from emp where deptno=10
--如果員工工資小於2000返回UnderPaid 大於等於4k 返回OverPaid 之間返回OK
select ename,sal,
case when sal<2000 then 'UnderPaid'
when sal>=4000 then 'OverPaid'
else
'OK'
end
from emp
--從表中隨機返回N條記錄 newid()
--order by 字句中指定數字常量時,是要求根據select列表中相應位置的列排序
--order by 字句中用函數時,則按函數在沒一行計算結果排序
select top 5 ename from emp order by newid()
--找空值is null
select * from emp where comm is null
--將空值轉換為實際值
--解釋:返回其參數中第一個非空表達式
--coalesce 聯合,合並,結合.英音:[,kəuə'les]美音:[,koə'lɛs]
select coalesce(comm, 1),empNo from emp
--按模式搜索
--返回匹配特定子串或模式的行
select ename,job
from emp
where deptno in(10,20)
--按子串排序 按照職位字段的 最後兩個字符排序
select ename, job from emp order by substring(job,len(job)-2,2)
--select top 2 len(job)-2 from emp
--select top 2 job from emp
--☆☆☆☆☆處理排序空值☆☆☆☆☆ [只能是大於0]
select ename ,sal,comm
from emp
order by 1 desc
-- 以降序或升序方式排序非空值,將空值放到最後,可以用case
select ename,sal,comm from
(
select ename ,sal,comm ,
case when comm is null then 0 else 1 end as A
from emp
) x
order by A desc ,comm desc