SQL statement (i)

Source: Internet
Author: User
Tags logical operators

SELECT

The SELECT statement is used to select data from the database.

SELECT Columna_name,columnb_name  from table_name;

Select the data from the table that selects columns A and B columns.

SELECT *  from table_name;

Selects all data for the table.

First, DISTINCT keyword filtering repetition

SELECT DISTINCT Filter Duplicate results.

SELECT DISTINCT Column_name,column_name  from table_name;

Do not repeat the selection, plus distinct will filter out the same results.

second, where clause

The WHERE clause is used to extract records that meet the specified criteria, and it selects Records.

SELECT Column_name,column_name  from table_name WHERE column_name operator value;

Common operators in the WHERE clause:

operator Description
= Equals
<> Not equal to. Note: in some versions of SQL, this operator can be written in! =
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
Between Within a range
Like Search for a pattern
Inch Specify multiple possible values for a column

operator when comparing, if the number field does not need to add ', if the non-numeric field needs to be added '.

The classification by operator is as follows:

1. Comparison operators

comparison operator Inclusions: = > < >=, <=,!=,<> (not equal)

SELECT *  fromEmpWHEREEname='SMITH';SELECT *  fromEmpWHEREename!='SMITH';SELECT *  fromEmpWHEREEage= -;SELECT *  fromEmpWHEREEage> -;

2. Logical operation

The logical operators are: and (and) or(or ) not (non).

SELECT *  fromEmpWHERESal>  -  andSal<  the;SELECT *  fromEmpWHERESal>  - OREname= 'SMITH';SELECT *  fromEmpWHERE  notSal>  -;
Logical operator Precedence:()> not> and>OR   

3. Special conditions

    • NULL evaluation: IS NULL
SELECT *  from WHERE  is NULL;

Columns that return null values

    • Between ... and (returns the value within the interval)
SELECT *  from WHERE between  the  and ;

Returns the value of the interval, which can be a number, can be an English character, or it can be a date. For example:

SELECT *  fromWebsitesWHEREName not between 'A'  and 'H';SELECT *  fromAccess_logWHEREDatebetween '2016-05-10'  and '2016-05-14';

Not between ... and (returns a value that is not within the interval)

    • Inch
SELECT *  from WHERE inch (In the",");

Returns a column with a value equal to () , in can specify multiple values,= Specifies a value

    • Fuzzy query like
SELECT *  from WHERE  like ' M% ';

% represents more than one word value, and an underscore denotes one character :

M% : For the wildcard, the regular expression, meaning that the fuzzy query information for the beginning of M.

%M% : Indicates that the query contains all the contents of M.

%m_ : Represents all the contents of the query with M in the penultimate position.

ThreeORDER by

The order by keyword is used to sort the result set by one column or multiple columns.

ASC Ascending,desc Descending. The default is descending. ASC Ascending, growing larger.

SELECT Column_name,column_name  from table_name ORDER  by ASC | DESC;

Sort By column B sequentially, and then arrange a on the basis of column B.

Fourtop keyword, LIMIT, rownum clause

The SELECT TOP clause is used to limit the number of records to be returned.

Note: Not all database systems support the SELECT TOP clause.

SQL server/ms Access Syntax

SELECT TOP  Number | percent column_name (s)  from table_name;

In SQL Server, you can use a percentage display: floating-point numeric +percent

MySQL syntax

SELECT column_name (s)  from  number;

Oracle syntax

SELECT column_name (s)  from table_name WHERE <= number;

SQL statement (i)

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.