SQL Learning notes four aggregate functions, sorting methods _mssql

Source: Internet
Author: User
Aggregate function Count,max,min,avg,sum ...
Select COUNT (*) from T_employee
Select Max (fsalary) from T_employee

Sort ASC Ascending desc Descending
SELECT * from T_employee ORDER by Fage

First in descending order of age. If you are of the same age, in ascending order of salary
SELECT * FROM T_employee-Fage desc,fsalary ASC

Order BY is placed after the WHERE clause

Wildcard Filter
Wildcard filter with like
Word wildcard with ' _ '
Multi-character wildcard with '% '
SELECT * from T_employee where FName like ' _erry '

NULL is not known meaning, and not without
Data queried for NULL with SQL statement cannot be null with = or <>, or is not NULL
SELECT * from T_employee where FName is NULL

In (23,25) matches two values at the same time. Equivalent to 25 or

Between and 30 matches numbers between 20 and 30

GROUP BY group
Select Fage, COUNT (*) from T_employee
Group by Fage
First, divide the same fage into groups, then count each group.

The GROUP BY clause is placed after the WHERE clause. If you want to take an age group of more than 1, you cannot use where count (*) > 1, because aggregate functions cannot be placed after the WHERE clause. To use the HAVING clause
Having is the filtering of the grouped columns, the same columns as the SELECT. In the following example, the having fsalary>2000 can only be used where fsalary>2000
Select Fage, COUNT (*) from T_employee
Group by Fage
Having count (*) > 1;

Limit the range of result sets
Select Top 3 * from T_employee
ORDER BY Fsalary DESC

You can use the Row_number function after selecting 3.2005 from sixth place.
Select Top 3 * from T_employee
where Fnumber not in (select Top 5 fnumber from T_employee order by Fsalary DESC)
ORDER BY Fsalary DESC

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.