Advanced application techniques for SQL query statements

Source: Internet
Author: User
Tags variables query

First, simple query

A simple Transact-SQL query includes only a picklist, a FROM clause, and a WHERE clause. They describe the query column, the table or view of the query, and the search criteria, and so on.

For example, the following statement queries the nickname field and the email field whose name is "John" in the TestTable table.

SELECT Nickname,email

From TestTable

WHERE name= ' John '

(i) SELECT list

The select list (select_list) indicates the query column, which can be a list of column names, asterisks, expressions, variables (including local variables and global variables), and so on.

1. Select all Columns

For example, the following statement shows the data for all columns in the TestTable table:

SELECT *

From TestTable

2, select some columns and specify their order of display

The data in the query results collection is arranged in the same order as the column names specified in the select list.

For example:

SELECT Nickname,email

From TestTable

3. Change column headings

In the select list, you can specify the column headings again. The format is defined as:

column heading = column name

Column Name title

If you specify a column heading that is not a standard identifier format, use a quotation mark delimiter, for example, the following statement uses Chinese characters to display columns

Title:

SELECT nickname =nickname, e-mail =email

From TestTable

4. Delete duplicate rows

The SELECT statement uses the all or distinct option to display all rows in the table that meet the criteria, or to delete duplicate rows of data, default to all. When you use the DISTINCT option, only one row is left in the result set returned by SELECT for all duplicate rows of data.

5, limit the number of rows returned

Use the top n [PERCENT] option to limit the number of rows returned, top N to return n rows, and Top n PERCENT to indicate that n is a percentage, and that the number of rows returned is equal to a percentage of the Total row count.

For example:

SELECT Top 2 *

From TestTable

SELECT Top PERCENT *

From TestTable

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.