SQL Basic article

Source: Internet
Author: User
Tags reserved sorts

1 . Some of the most important SQL commands

    • SELECT -Extract data from the database
    • Update-Updates the data in the database
    • Delete-deletes data from the database
    • INSERT INTO-inserts new data into the database
    • CREATE database-creating new databases
    • alter database-Modify Databases
    • CREATE TABLE-Creates a new table
    • ALTER TABLE -Change (change) database table
    • Drop Table-delete tables

2.Select

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

The result is stored in a result table, called the result set.

SQL Select Syntax Select column_name, column_name
From table_name;

With SELECT * FROM table_name; (if the query all fields are the same, if you query only some of the fields in the table, use the Select field: Much higher efficiency than SELECT *)

3. SELECT DISTINCT

In a table, a column may contain multiple duplicate values, and sometimes you might want to list only different (distinct) values.

DISTINCT keywords are used to return unique different values

InstanceSELECT DISTINCT country from Websites;

Output Result:

4. Select where

SELECT column_name,column_name
From table_name
WHERE column_name operator value;

Operators in the WHERE clause

The following operators can be used 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

5, SELECT or &and

If both the first condition and the second condition are true, the AND operator displays a record.

SELECT * from Websites WHERE Country='CN ' and Alexa > ;

If only one of the first and second conditions is true, the OR operator displays a record.

SELECT * from Websites WHERE Country='USA' OR Country='CN';

Combine with & OR

You can also combine and and or together (using parentheses to form complex expressions).

The following SQL statement selects all Web sites from the "Websites" table with Alexa ranking greater than "15" and the country as "CN" or "USA":

SELECT * from Websites where alexa > 15 Span class= "hl-reserved" >and ( Country= ' cn Span class= "Hl-code" > or country= "usa 6. Select Order byORDER by instance

The following SQL statement selects all sites from the "Websites" table and sorts them according to the "Alexa" column:

InstanceSELECT * from Websites ORDER by alexa;

Execution output:

SELECT * from Websites order by Alexa DESC; just follow the Alexa back.

The following SQL statement selects all sites from the "Websites" table and sorts them according to the "Country" and "Alexa" columns:

InstanceSELECT * from Websites ORDER by country,Alexa;

7. Insert INTO

The INSERT into statement can be written in two ways.

The first form does not need to specify the column name to insert the data, just provide the inserted value:

INSERT into table_name
VALUES ( value1, value2, Value3,...);

The second form requires specifying the column name and the value to be inserted:

INSERT into table_name( Column1, Column2, Column3,...)
VALUES ( value1, value2, Value3,...); 8, Updateupdate table_name
SET Column1= value1, Column2= value2,...
WHERE Some_column= some_value;
Note the WHERE clause in the SQL UPDATE statement!
The WHERE clause specifies which record or records need to be updated. If you omit the WHERE clause, all the records will be updated!

9. Delete

DELETE from table_name
WHERE Some_column= some_value;

Note the WHERE clause in the SQL DELETE statement!
The WHERE clause specifies which record or records need to be deleted. If you omit the WHERE clause, all records will be deleted!

SQL Basic 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.