Database SQL query

Source: Internet
Author: User

Inquire

1. Simple query

SELECT * FROM info (table name)--Check all data

Select Code (column name), name (column name) from table name--Check the data for the specified column

Select Code (column name) as ' code ', name (column name) as ' name ' from info (change column name)--Assign alias to column

2. Conditional query

SELECT * FROM info (table name) where code (condition) = ' p001 '

SELECT * FROM info (table name) where sex (condition) = ' true ', and nation= ' n001 '--multi-conditional and relational

SELECT * FROM info (table name) where sex (condition) = ' true ', or nation= ' n001 '--multi-condition or relationship

3. Scope Query

SELECT * from table name where range

SELECT * from car where price>40 and price<50

SELECT * from car where price between and 50

4. Discrete query

SELECT * FROM car (table name) where code (column name) in (' c001 ', ' c005 ', ' c010 ', ' c015 ')

SELECT * from car where code not in (' c001 ', ' c005 ', ' c010 ', ' c015 ')

5. Fuzzy query

SELECT * FROM car (table name) where name (column name) like '% BMW (keyword)% '-check contains BMW's

SELECT * from car where name like ' BMW% '--look at the beginning of the BMW

SELECT * from car where name like '% BMW-check end of BMW '

SELECT * from car where name like ' BMW '--check etc with BMW's

SELECT * from car where name like '-e% '--check the third character is E's

% represents any number of characters

-Delegate is a character

6. Sort queries

Select *from Car (table name) Order BY price ASC--in ascending order of prices

Select *from Car (table name) Order BY price desc– in descending order

Select *from Car ORDER BY 0il DESC, price ASC-sorted in two fields, preceded by a secondary condition after the main condition

7. Paging Query

Select Top 5 * from car

Select Top 5 * from car where code not in (select Top 5 code from car)

Current page: page = 2 row= 10

Select top row *from car where code not in (select Top (page-1) * Row code from CAR)

8. Go to re-query

Select distinct brand from car

9. Group queries

Select brand from Car GROUP by Brand has count (*) >2

10. Aggregation function (statistical query)

How much data from car– query all data bars after select COUNT (*) query is complete

Select count (code) from car--query all data bars

Select SUM (Price (column name)) Sum from car (table name)

Select SUM (price) from car sum

Select SUM (price) from car sum

Select SUM (price) from car sum

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