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