SQL Server query statement usage

Source: Internet
Author: User
Tags sql server query

1. query all information of employees whose second letter is t or
Copy codeThe Code is as follows:
Select *
From employees
Where firstname like '_ [t, a] %'

Note: In SQL, % indicates a string, so it cannot be annotated like matlab. The two double slashes do not seem to work either./**/Yes. Some netizens say that the single SQL line comment is --

2. Change the field name
Copy codeThe Code is as follows:
Select 'name' = firstname, 'surname' = lastname
From employees
Where firstname like '_ [t, a] %'

Or
Copy codeThe Code is as follows:
Select firstname as 'name', lastname as 'surname'
From employees
Where firstname like '_ [t, a] %'

Iii. top keywords
Copy codeThe Code is as follows:
/* Retrieve the first 70% matching records */
Select top 70 percent firstname as 'name', lastname as 'surname'
From employees
Where firstname like '_ [t, a] %' 1/* retrieve the first two matching records */
Select top 2 firstname as 'name', lastname as 'surname'
From employees
Where firstname like '_ [t, a] %'

Iv. union keywords
Note: standard SQL only supports concurrent operations, but does not support intersection and minus operations.
Copy codeThe Code is as follows:
Select *
From employees
Where title = 'sales manager'
Union
Select *
From employees
Where address is not null

Display:

Server: Message 8163, level 16, status 4, Row 1
The text, ntext, or image data type cannot be selected in DISTINCT mode.
Copy codeThe Code is as follows:
Select *
From employees
Where title = 'sales manager'
Union all
Select *
From employees
Where address is not null

The analysis query in the query and analysis is to check whether there is a syntax error (ctrl + F5) and execute the query (right triangle) (F5) yes. If a syntax error occurs, the result is not executed.

5. compute keywords

The compute clause requires the following information:
The optional By keyword can be used to calculate the specified row aggregation for a column.
Row aggregation functions: sum, avg, min, max, count
Columns for which row Aggregate functions are to be executed
When compute has an optional By clause, each group that meets the select condition has two result sets:
The first result set of each group is a detailed row set, which contains the selection list information of the group.
The second result set of each group has a row, which contains the Note of the aggregate function specified in the COMPUTE clause of this group.
Copy codeThe Code is as follows:
Select sex, sclass, score
From student
Order by sex
Compute sum (score) by sex

Note: order by is required, and the parameters after compute by should appear in the parameters after order
Copy codeThe Code is as follows:
Select sex, sclass, score
From student
Compute sum (score)

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.