Simple summary of queries in SQL Server

Source: Internet
Author: User

first, the execution order of SQL statements

The query order of an SQL statement is the most used operation in the database at query time

1, from TB1 [join on] Get query data source

2. Where to filter data (filtering on a single data)

3. Group BY Data Grouping

4. Having a screening group (filtering on the group)

5. Select distinct Get result set

6. Order BY to sort the result set

second, the common query knowledge

Directly on the code

--1. Null value query        --Example:query for users with age (or not) null    Select *  fromTb_userinfowhereAge is NULL    Select *  fromTb_userinfowhereAge is  not NULL    --2. Conditional Query    --Note: Where is used for single record filtering    --Example:query for users aged 21~23    Select *  fromTb_userinfowhereAge>= +  andAge<= at    Select *  fromTb_userinfowhereAgeinch( +, A, at)    Select *  fromTb_userinfowhereAgebetween  +  and  at--3. Aggregation Functions and Grouping    --Note: These two should be used together, when used alone, without much meaning, when the group by group is not explicitly grouped, all data is a set of    --Common aggregate functions count avg min max sum;having to filter groups        --Example:groups that query the number of people who use the role and who filter the role by more than 3    SelectRoleid asrole Number,COUNT(*) asnumber fromTb_userinfoGroup  byRoleid having COUNT(*)>3        --Query the number of people per role and the average age of each role user    SelectRoleid asrole Number,COUNT(*) asnumber,AVG(age) asaverage age fromTb_userinfoGroup  byRoleid--4. Fuzzy Query    --Note:-Represents a single character;% denotes any character; [ABC] means A/B/C; ^a represents a character that is not a        --Example:querying users with a user name    Select *  fromTb_userinfowhereUsername like '%a%'    Select *  fromTb_userinfowhereUserName like '_a%'--username The second character of A is a user--5. Distinct, top and order by    --Note: Top and order by are used together, first order to have ' first few ' records        --Example:query for the top 3 records in a non-repeating user, sorted by user ID    Select distinct Top 3  *  fromTb_userinfoOrder  byUid

Simple summary of queries in SQL Server

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.