Showing posts with label SQL syntax. Show all posts
Showing posts with label SQL syntax. Show all posts
SQL ORDER BY keyword

SQL ORDER BY keyword

The ORDER BY keyword is used to sort the result set.

SQL ORDER BY keyword

The ORDER BY keyword is used to sort the result set by a column or columns.
The ORDER BY keyword sorts the records by default. If you need to sort the records in descending order, you can use the DESC keyword.

SQL ORDER BY syntax

SELECT column_name , column_name
FROM table_name
ORDER BY column_name , column_name ASC|DESC;


Demo database

In this tutorial, we will use the RUNOOB sample database.
The following is the data selected from the "Websites" table:
 + ---- + -------------- + --------------------------- + - ------ + --------- +
 | Id | name | url | alexa | country |
 + ---- + -------------- + --------------------------- + - ------ + --------- +
 | 1 | Google | https://www.google.cm/ | 1 | USA |
 | 2 | Taobao | https://www.taobao.com/ | 13 | CN |
 | 3 | rookie tutorial | http://www.runoob.com/ | 4689 | CN |
 | 4 | microblogging | http://weibo.com/ | 20 | CN |
 5 | Facebook | https://www.facebook.com/ | 3 | USA |
 + ---- + -------------- + --------------------------- + - ------ + --------- +

ORDER BY instance

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

Examples

SELECT * FROM Websites ORDER BY Alexa
Execute the output:


ORDER BY DESC instance

The following SQL statement selects all sites from the "Websites" table and sorts them in descending order of "alexa"

Examples

SELECT * FROM Websites ORDER BY Alexa DESC ;
Execute the output:


ORDER BY multiple columns

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

Examples

SELECT * FROM Websites ORDER BY Country , alexa
Execute the output:
SQL AND & OR operator

SQL AND & OR operator

The AND & OR operator is used to filter records based on more than one condition.

SQL AND & OR operator

If both the first condition and the second condition are true, the AND operator displays a record.
If only one of the first condition and the second condition holds, the OR operator displays a record.

Demo database

In this tutorial, we will use the RUNOOB sample database.
The following is the data selected from the "Websites" table:
+ ---- + -------------- + --------------------------- + - ------ + --------- + | id | name          | url                        | alexa | country | + ---- + -------------- + - -------------------------- + ------- + --------- + | 1 | Google | Https : //www.google.cm/ | 1 | USA | | 2 | Taobao | https : //www.taobao.com/ | 13 | CN | | 3 | Rookie Tutorial | http : //www.runoob.COM / | 4689 | CN | | 4 | Twitter | HTTP : //weibo.com/ | 20 | CN | | 5 | Facebook | HTTPS : //www.facebook.com/ | 3 | USA | + --- - + -------------- + --------------------------- + ----- - + --------- +


           
              
          
              
         

AND operator instance

The following SQL statement selects all sites from the "Websites" table for "CN" and alexa rank greater than "50":

Examples

SELECT * FROM Websites WHERE country = ' CN ' AND alexa > 50 ;
Execute the output:


OR operator instance

The following SQL statement selects all customers whose country is "USA" or "CN" from the "Websites" table:

Examples

SELECT * FROM Websites WHERE country = ' USA ' OR country = ' CN ' ;
Execute the output:


Combined with AND & OR

You can also combine AND and OR (using parentheses to form complex expressions).
The following SQL statement selects all sites with alexa rank greater than "15" and "CN" or "USA" from the "Websites" table:

Examples

SELECT * FROM Websites WHERE alexa > 15 AND ( country = ' CN ' OR country = ' USA ' ) ;
Execute the output:
SQL where clause tutorial

SQL where clause tutorial

The WHERE clause is used to filter records.

SQL WHERE clause

The WHERE clause is used to extract records that meet the specified criteria.

SQL WHERE syntax

SELECT column_name,column_name
FROM table_name
WHERE column_name operator value;


Demo database

In this tutorial, we will use the sample database.
The following is the data selected from the "Websites" table:
+ ---- + -------------- + --------------------------- + - ------ + --------- + | id | name          | url                        | alexa | country | + ---- + -------------- + - -------------------------- + ------- + --------- + | 1 | Google | Https : //www.google.cm/ | 1 | USA | | 2 | Taobao | https : //www.taobao.com/ | 13 | CN | | 3 | Rookie Tutorial | http : //www.runoob.COM / | 4689 | CN | | 4 | Twitter | HTTP : //weibo.com/ | 20 | CN | | 5 | Facebook | HTTPS : //www.facebook.com/ | 3 | USA | + --- - + -------------- + --------------------------- + ----- - + --------- +


           
              
          
              
         


WHERE clause instance

The following SQL statement selects all sites from the "Websites" table for the "CN":

Examples

SELECT * FROM Websites WHERE country = ' CN ' ;
Execute the output:


Text field vs. numeric field

SQL uses single quotation marks to wrap text values ​​(most database systems also accept double quotes).
In the previous example, the 'CN' text field uses single quotation marks.
If it is a numeric field, do not use quotation marks.

Examples

SELECT * FROM Websites WHERE id = 1 ;
Execute the output:


The operators in the WHERE clause

The following operators can be used in the WHERE clause:
Operatordescription
=equal
<>not equal to. Note: In some versions of SQL, the operator can be written as! =
>more than the
<Less than
> =greater or equal to
<=Less than or equal to
BETWEENIn a certain range
LIKESearch for a pattern
INSpecifies multiple possible values ​​for a column
SQL the SELECT DISTINCT statement

SQL the SELECT DISTINCT statement

The SELECT DISTINCT statement is used to return a unique different value.

SQL SELECT DISTINCT statement

In the table, a column may contain multiple duplicate values, and sometimes you may want to list only the distinct values.
The DISTINCT keyword is used to return a unique different value.

SQL SELECT DISTINCT syntax

SELECT DISTINCT column_name,column_name
FROM table_name;


In this tutorial, we will use the RUNOOB sample database.
The following is the data selected from the "Websites" table:
+ ---- + -------------- + --------------------------- + - ------ + --------- + | id | name          | url                        | alexa | country | + ---- + -------------- + - -------------------------- + ------- + --------- + | 1 | Google | Https : //www.google.cm/ | 1 | USA | | 2 | Taobao | https : //www.taobao.com/ | 13 | CN | | 3 | Rookie Tutorial | http : //www.runoob.COM / | 4689 | CN | | 4 | Twitter | HTTP : //weibo.com/ | 20 | CN | | 5 | Facebook | HTTPS : //www.facebook.com/ | 3 | USA | + --- - + -------------- + --------------------------- + ----- - + --------- +


           
              
          
              
         


SELECT DISTINCT instance

The following SQL statement only from the "Websites" table "country" column to select a unique different value, that is, remove the "country" column repeat value:

Examples

SELECT DISTINCT country FROM Websites ;
Output results:

Popular Posts