About the use of SQL Server query statements _mssql

Source: Internet
Author: User
Tags sql server query

I. Querying the entire information of an employee with a second letter of T or a

Copy Code code as follows:

SELECT *
From Employees
where FirstName like ' _[t,a]% '

Note: In SQL,% of the string, so do not like MATLAB with its comments, two double slash seems not,/**/can, have netizens say SQL single note as-

Two. Change field name

Copy Code code as follows:

Select ' Name ' = FirstName, ' surname ' = LastName
From Employees
where FirstName like ' _[t,a]% '

Or
Copy Code code as follows:

Select FirstName as ' name ', LastName as ' surname '
From Employees
where FirstName like ' _[t,a]% '

three. Top keyword
Copy Code code as follows:

* * Retrieve the first 70% records of the qualifying * *
Select top percent FirstName as ' name ', LastName as ' surname '
From Employees
where FirstName like ' _[t,a]% ' 1 * Retrieves the first 2 records that meet the criteria * *
Select top 2 firstname as ' name ', LastName as ' surname '
From Employees
where FirstName like ' _[t,a]% '

four. Union keyword
NOTE: Standard SQL provides and operates only and does not provide a intersection (minus) operation.
Copy Code code as follows:

SELECT *
From Employees
where title = ' Sales Manager '
Union
SELECT *
From Employees
Where address isn't null

Show:

Server: Message 8163, Level 16, State 4, line 1
You cannot select text, ntext, or image data types in DISTINCT mode.

Copy Code code as follows:

SELECT *
From Employees
where title = ' Sales Manager '
UNION ALL
SELECT *
From Employees
Where address isn't null

Query analysis in which the analysis query (check mark) is to see if there is a syntax error (CTRL + F5), the execution of the query (right triangle) (F5) is the result of the display if there is a syntax error does not execute.

Five. Compute keyword

The COMPUTE clause requires the following information:
Optionally by keyword to calculate the specified row aggregation on a column
Row aggregate functions: Sum,avg,min,max,count
column on which to perform row aggregate functions
When compute with an optional by clause, each group that meets the select criteria has two result sets:
The first result set for each group is the detail rowset, which contains the selection list information for the group
There is one row for the second result set for each group, which contains the subtotal of the aggregate function specified in the COMPUTE clause of the group

Copy Code code as follows:

Select Sex,sclass,score
From student
ORDER BY sex
Compute SUM (score) by sex

Note: The order by is mandatory, and the compute by parameter should appear in the argument after the

Copy Code code as follows:

Select Sex,sclass,score
From student
Compute SUM (Score)

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.