SQL zero-based introductory learning (5)

SQL zero-based introductory learning (5)

SQL zero-based introductory learning (4)

SQL SELECT TOP, LIMIT, ROWNUM child clauses

####SQL SELECT TOP clause
The SELECT TOP clause is used to specify the number of records to be returned.

The SELECT TOP clause is very useful for large tables with thousands of records.

Note: Not all database systems support the SELECT TOP statement. MySQL supports the LIMIT statement to select the specified number of data, and Oracle can use ROWNUM to select.

####SQL Server / MS Access Syntax

SELECT TOP number|percent column_name(s)
FROM table_name;

MySQL syntax

SELECT column_name(s)
FROM table_name
LIMIT number;

example

SELECT *
FROM Persons
LIMIT 5;

Oracle syntax

SELECT column_name(s)
FROM table_name
WHERE ROWNUM <= number;

example

SELECT *
FROM Persons
WHERE ROWNUM <=5;

demo database

In this tutorial, we will use the RUNOOB sample database.

Here is the data from the "Websites" table:

mysql> SELECT * FROM Websites;
+----+---------------+---------------------------+-------+---------+
| id | name          | url                       | alexa | country |
+----+---------------+---------------------------+-------+---------+
|  1 | Google        | https://www.google.cm/    |     1 | USA     |
|  2 | 淘宝          | https://www.taobao.com/   |    13 | CN      |
|  3 | 菜鸟教程       | http://www.runoob.com/    |  5000 | USA     |
|  4 | 微博           | http://weibo.com/         |    20 | CN      |
|  5 | Facebook      | https://www.facebook.com/ |     3 | USA     |
|  7 | stackoverflow | http://stackoverflow.com/ |     0 | IND     |
+----+---------------+---------------------------+-------+---------+

MySQL SELECT LIMIT example

The following SQL statement selects the first two records from the "Websites" table:

SELECT * FROM Websites LIMIT 2;

Execute the above SQL, the data is as follows:
insert image description here

SQL SELECT TOP PERCENT instance

Percentages can also be used as parameters in Microsoft SQL Server.

The following SQL statement selects the top 50 percent of records from the websites table:

Example
The following operations can be performed on a Microsoft SQL Server database.

SELECT TOP 50 PERCENT * FROM Websites;

SQL LIKE operator

The LIKE operator is used to search for a specified pattern in a column in the WHERE clause.

SQL LIKE operator

The LIKE operator is used to search for a specified pattern in a column in the WHERE clause.

SQL LIKE syntax

SELECT column1, column2, ...
FROM table_name
WHERE column LIKE pattern;

Parameter description:
column1, column2, …: the name of the field to be selected, which can be multiple fields. If no field names are specified, all fields will be selected.
table_name: The name of the table to be queried.
column: The field name to search.
pattern: search pattern.

demo database

In this tutorial, we will use the RUNOOB sample database.

Here is the data from the "Websites" table:

mysql> SELECT * FROM Websites;
+----+---------------+---------------------------+-------+---------+
| id | name          | url                       | alexa | country |
+----+---------------+---------------------------+-------+---------+
|  1 | Google        | https://www.google.cm/    |     1 | USA     |
|  2 | 淘宝          | https://www.taobao.com/   |    13 | CN      |
|  3 | 菜鸟教程       | http://www.runoob.com/    |  5000 | USA     |
|  4 | 微博           | http://weibo.com/         |    20 | CN      |
|  5 | Facebook      | https://www.facebook.com/ |     3 | USA     |
|  7 | stackoverflow | http://stackoverflow.com/ |     0 | IND     |
+----+---------------+---------------------------+-------+---------+

SQL LIKE operator instance

The following SQL statement selects all customers whose name begins with the letter "G":

SELECT * FROM Websites
WHERE name LIKE 'G%';

Execution output:
insert image description here
Tip: The "%" symbol is used to define wildcards (default letters) before and after the pattern. You'll learn more about wildcards in the next chapter.

The following SQL statement selects all customers whose name ends with the letter "k":

example

SELECT * FROM Websites
WHERE name LIKE '%k';

Execution output:
insert image description here
The following SQL statement selects all customers whose name contains the pattern "oo":

example

SELECT * FROM Websites
WHERE name LIKE '%oo%';

Execution output:
insert image description here
By using the NOT keyword, you can select records that do not match the pattern.

The following SQL statement selects all customers whose name does not contain the pattern "oo":

example

SELECT * FROM Websites
WHERE name NOT LIKE '%oo%';

Execution output:
insert image description here

Guess you like

Origin blog.csdn.net/weixin_44006731/article/details/129157669