SQL Server basic knowledge data retrieval, query sort statement _mssql

Source: Internet
Author: User
Copy Code code as follows:

--Execution order from Where Select
SELECT * FROM
(select Sal as salary,comm as commission from EMP) x where salary<5000
--Draw Name Work as a Job
Select Ename + ' Work as a ' +job as msg from EMP where deptno=10
--If the employee pay less than 2000 returns underpaid greater than or equal to 4k return OK between overpaid
Select Ename,sal,
Case when sal<2000 then ' underpaid '
When sal>=4000 then ' overpaid '
Else
' OK '
End
From EMP
--Random return of N records from a table newid ()
When specifying a numeric constant in the--order by clause, it requires that the column be sorted according to the corresponding position in the select list
--order a function in the By clause, the result is sorted by function
Select Top 5 ename from emp Order by NEWID ()
--NULL for null value
SELECT * FROM EMP where comm is null
--Converts a null value to an actual value
--Explanation: Returns the first non-empty expression in its argument
--coalesce Unite, combine, combine. English: [, kəuə ' Les] American: [, koə ' lɛs]
Select COALESCE (comm, 1), EmpNo from EMP
--Search by mode
--Returns rows that match a specific substring or pattern
Select Ename,job
From EMP
where Deptno in (10,20)
--Sort by substring by the last two characters of the position field
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
--☆☆☆☆☆ processing sort null value ☆☆☆☆☆[can only be greater than 0]
Select Ename, Sal,comm
From EMP
ORDER BY 1 Desc
--Sort non-null values in descending or ascending order, putting null values at the end, you can use the case
Select Ename,sal,comm from
(
Select Ename, Sal,comm,
Case when comm are null then 0 else 1 end as A
From EMP
) x
ORDER BY A DESC, Comm Desc

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.