Common SQL query statements

Source: Internet
Author: User

1. Obtain the first row of data in the table

select top 1 * from Dept

2. display a specific row in the first row

select DeptNO,DeptName,Phone,Manager,Deputy,Remark from dept where parentID='3' order by case when deptNO='3' then 0 else 1 end

3. SQL nested Query

select a,b,c from tableD where a=(select a from tableD)

select * from table1 where field1 in    (select field1 from table2 where field2 in      (select field2 from table3 where field4 in        ...................       )    )  

4. SQL sorting Query

Select * from Dept order by parentid desc -- select * from Dept order by parentid ASC in ascending order, deptno ASC

5. Time fuzzy query in SQL

Select * from t where convert (varchar, sendtime, 120) Like '2017-12-2007 '-- t is the database table, and sendtime is the field name of the time field in the database

6. query unique

select distinct Pname from planweek

7. Remove leading and trailing Spaces

use Performance select  ltrim(rtrim(dutyname)) as dutyname from [user] where uid=10

8. Joint Query

Select dept2.deptname as 'department ', Dept. deptname as 'group', planweek. pname as 'name', [user]. dutyname as 'post', left (convert (varchar, planweek. [years], 120), 7) as 'Planning month', -- convert to convert (varchar, planweek. werks, 120) as 'number of planned weeks', -- convert to the string planweekdetails. task as 'work key', planweekdetails. target as 'work target', planweekdetails. weights as 'weight', planweekdetails. datesp as 'scheduled start time', planweekdetails. datesa as 'actual start time', planweekdetails. dateep as 'planned completion time', planweekdetails. dateea as 'actual End Time', planweekdetails. suggestion as 'complete', planweekdetails. reason as 'unfinished reason ', planweekdetails. summary as 'work summary', planweekdetails. remark as 'note' from planweek left join planweekdetailson planweek. id = planweekdetails. idleft join [user] On planweek. uid = [user]. UID left join (select * from Dept where parentid <> 0) as dept on [user]. deptid = Dept. idleft join (select * from Dept where parentid = 0) as dept2 on [user]. deptid = dept2.id or Dept. parentid = dept2.idwhere planweek. pname like '%' and planweek. years like '%' and planweek. werks like '%' order by planweek. werks, dept2.deptname, Dept. deptname, planweek. pname, [user]. dutyname, planweek. [years]

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.