SQL statement-Basic query

Source: Internet
Author: User
Tags aliases logical operators sorts

first, the grammatical structure
Select select_list []from  table_source[ ] [  ][][]

The main parameters in the Select query statement are described below

select_list: A comma-delimited list of columns or expressions that are queried.
New_table: The new table name.
Table_source: The table to query. If there are multiple tables, separate them with commas.
Search_condition: Query criteria.
Group_by_expression: Grouping expressions.
Order_by_expression: Sort an expression.
ASC: Sort in ascending order.
Desc: Sort in descending order.

List of functions for select query statement

Clause Key Features is required
Select Specify the columns returned by the query Is
From Specify the table to query Is
Into Create a new table and insert the result row into a new table Whether
where Query criteria Whether
GROUP BY Grouping Query Results Whether
ORDER BY Sort the results of a query Whether
Having Filter the results of a query Whether

Second, select the list

Select list to define columns in the result set of a SELECT statement

  1. * Query All columns:

Select *  from person

* Is the result set, which represents all columns in the query person table.

  2, distinct remove duplicate data:

   Distinct is the function of all columns, that is, all columns are the same to be counted as duplicate data.

Select distinct  from person

  3. Queries that contain functions:

For example:

Select Count (* from person
third, from clause

The FROM clause is actually a comma-delimited list of table names, view names, and join words. Use the FROM clause to achieve the following functions:

1. Lists the tables and views of the selection list and the columns referenced by the WHERE clause. You can use the AS clause to specify aliases for tables and views.

2. Type of connection. These types are qualified by the join condition specified in the ON clause.

You can use the following form when assigning table names

      • TABLE_NAME as Table alias
      • TABLE_NAME as Table_alias

It is important to note that if you assign an alias to a table, all display references to that table in the T-SQL statement must use aliases instead of aliases.

iv. WHERE clause

The WHERE clause can filter rows in the source table of the result set. The structure of a SELECT statement with a WHERE clause is as follows:

   Select < field List >from< table name >where< conditional expression  >      

Where the conditional expression is composed of various fields, constants, expressions, relational operators, logical operators, and special operators.

Operators in the WHERE clause:

1. Relational operators

A relational operator is used to represent a comparison relationship between two expressions.

Relational operators Meaning
= Equals
< Less than
> Greater than
! = (or <>) Not equal to
>= Greater than or equal
<= Less than or equal
!> No greater than
!< Not less than

2. Logical Operators

A logical operator is used to represent a logical relationship between two expressions:

logical operators Meaning
Not Non (NO)
and And
Or Or

3. Special operators

special operator meaning
% wildcard, usually with Lik E uses the
_ wildcard character to represent a strict character. Where name like ' _xxx ' will find all 4-letter names ending with xxx (sxxx,dxxx, etc.)
[] Specify the range ([a-f]) or any single in the collection ([ABCDEFG]) a character. where name like ' [A-f]xxxx ', the character that will be abcdef to start with the end of XXXX.
[^] Any single character that does not belong to the specified range ([a-f]) or collection ([ABCDEFG]).
between defines a range of values to use and separate. Between the start value with the and end value.
like string match
in the value of a field is within a defined set of values
exists the subquery has a result set returned (then the subquery returns True)
NOT exists subquery has no result set returned (then subquery returns true)
is null field is null
is not null field NOT null

Using exists in the WHERE clause, if used properly, can greatly improve performance. Because exists is used, SQL Server stops immediately whenever a record is found that matches the condition. Assuming there is a table with 1 million records, and a matching record is found in the third record, using the EXISTS option avoids reading 999,997 records! Not exists works the same way.

v. GROUP BY clause

See Address: http://www.cnblogs.com/kissdodog/p/3365789.html

vi. ORDER BY clause

The ORDER BY clause is used to specify the ordering of the result set

1. Grammatical structure:

    Select < list of field names > from database table name  [where < conditional expression >][  order by[< field name or expression > [Asc|desc],...]]        

The ORDER BY clause can be paired with a WHERE clause, or it can be used with SELECT, FROMD, without requiring a WHERE clause.

The syntax for the ORDER BY clause is as follows:

    [  ORDER BY {order_by_expression [ASC | desc][] ]

The main parameters are described below:

Order_by_espression: Specifies the column to sort, the alias of the column, an expression, or a negative integer that is specified as the location of the name, alias, or expression within the selection list.

ASC: Sorts the values in the specified column in ascending order.

Desc: Sorts the values in the specified column in descending order.

Seven, having a filter query

See address:http://www.cnblogs.com/kissdodog/p/3365789.html

eight, into query

The INTO clause generates a new table for the query results, and the structure of the new table consists of a list of query fields. You can also send the results of a query to a temporary table in the tempdb database so that the temporary table is automatically deleted after you shut down the server.

The syntax structure of the INTO query:

  < list of field names >[] from    database table name  [ ]    

SQL statement-Basic query

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.