sql初学者指南

SQLSELECT statements are used to retrieve data from the database and also, they populate the result of the query into the result-sets. The SQL examples of this article discourse and explain the fundamental usage of the SELECT statement in the queries.

SQLSELECT语句用于从数据库检索数据,并且还将查询结果填充到结果集中。 本文SQL示例论述并解释了SELECT语句在查询中的基本用法。

SQL (Structured Query Language) queries can be used to select, update and delete data from the database. If anyone desires to learn SQL, to learn the SELECT statements can be the best starting point. On the other hand, we can use T-SQL query language particularly for SQL Server databases and it is a proprietary extension form of the SQL.

SQL(结构化查询语言)查询可用于从数据库中选择,更新和删除数据。 如果有人希望学习SQL,那么学习SELECT语句可能是最好的起点。 另一方面,我们可以将T-SQL查询语言特别用于SQL Server数据库,它是SQL的专有扩展形式。

SELECT语句概述 (SELECT statement overview)

The most basic form of the SQL SELECT statement must be include SELECT, FROM clauses. In addition, if we want to filter the result set of the query, we should use the WHERE clause.

SQL SELECT语句的最基本形式必须包括SELECTFROM子句。 另外,如果要过滤查询的结果集,则应使用WHERE子句。

SELECT column1, column2 FROM table

The above query template specifies a very basic SQL SELECT statement. As you can see, column names are placed after the SELECT clause and these columns are separated with a comma sign with (,). After the FROM clause, we add the table name in which we want to populate the data into the result set. In addition, the following query template illustrates the usage of the WHERE clause in the SELECT query.

上面的查询模板指定了一个非常基本SQL SELECT语句。 如您所见,列名放在SELECT子句之后,并且这些列用(,)逗号分隔。 在FROM子句之后,我们将要在其中添加数据的表名添加到结果集中。 此外,以下查询模板说明了SELECT查询中WHERE子句的用法。

SELECT column1, column2 FROM table WHERE column1='value'

With the WHERE clause, we can filter the result set of the select statement. Filtering patterns are used after the WHERE clause. Now, we will make some SQL examples of the SQL SELECT statement and reinforce these theoretical notions.

使用WHERE子句,我们可以过滤select语句的结果集。 在WHERE子句之后使用过滤模式。 现在,我们将为SQL SELECT语句提供一些SQL示例,并强化这些理论概念。

基本SQL示例:进入SELECT语句的第一步 (Basic SQL examples: Your first step into a SELECT statement )

Assume that, we have a fruits table which likes the below and includes the following rows;

假设,我们有一个水果表,该表类似于下面并包括以下行;

ID

Fruit_Name

Fruit_Color

1

Banana

Yellow

2

Apple

Red

3

Lemon

Yellow

4

Strawberry

Red

5

Watermelon

Green

6

Lime

Green

ID

水果名称

水果色

1个

香蕉

黄色

2

苹果

3

柠檬

黄色

4

草莓

5

西瓜

绿色

6

酸橙

绿色

We want to get all data of the Fruit_Name from the Fruits table. In this case, we must write a SQL SELECT statement which looks like the below.SQL Server database engine processes this query and then returns the result-set of the query.

我们想从Fruits表中获取Fruit_Name的所有数据。 在这种情况下,我们必须编写如下所示SQL SELECT语句.SQL Server数据库引擎将处理此查询,然后返回查询的结果集。

SELECT Fruit_Name FROM Fruits

As you can see, the query returns only Fruit_Name column data.

如您所见,查询仅返回Fruit_Name列数据。

Now, we will practice other SQL examples which are related to the SELECT statement. In this example first example, we will retrieve all columns of the table. If we want to return all columns of the table, we can use a (*) asterisk sign instead of writing whole columns of the table. Through the following query, we can return all columns of the table.

现在,我们将练习与SELECT语句相关的其他SQL示例。 在本示例的第一个示例中,我们将检索表的所有列。 如果要返回表的所有列,可以使用(*)星号而不是写表的整个列。 通过以下查询,我们可以返回表的所有列。

SELECT * FROM Fruits

At the same time, to retrieve all columns, we can do this by writing them all separately. However, this will be a very cumbersome operation.

同时,要检索所有列,我们可以通过分别编写它们来完成。 但是,这将是非常麻烦的操作。

SELECT ID,Fruit_Name ,Fruit_Color FROM Fruits

SQL示例:如何过滤SELECT语句 (SQL examples: How to filter a SELECT statement )

In this section, we will take a glance at simple clause usage of the WHERE clause. If we want to filter the result set of the SQL SELECT statement, we have to use the WHERE clause. For example, we want to filter the fruits whose colors are red. In order to filter results of the query, at first we add the column name which we want to filter and then specify the filtering condition. In the below SQL example, we will filter the red fruits of the Fruits table.

在本节中,我们将看一下WHERE子句的简单子句用法。 如果要过滤SQL SELECT语句的结果集,则必须使用WHERE子句。 例如,我们要过滤红色的水果。 为了过滤查询的结果,首先我们添加要过滤的列名,然后指定过滤条件。 在下面SQL示例中,我们将过滤Fruits表的红色水果。

SELECT * FROM Fruits WHERE Fruit_Color='Red'

As you can see that, the result set only includes the red fruits data. However, in this example, we filter the exact values of the columns with (=) equal operator. In some circumstances, we want to compare the similarity of the filtered condition. LIKE clause and (%) percent sign operator combination helps us to overcome these type of issues. For example, we can filter the fruits who start with the letter “L” character. The following query will apply a filter to Fruit_Name and this filter enables to retrieve fruits who start with “L” chracter.

如您所见,结果集仅包含红色水果数据。 但是,在此示例中,我们使用( = )等于运算符过滤了列的确切值。 在某些情况下,我们想比较过滤条件的相似性。 LIKE子句和(%)百分号运算符的组合可帮助我们克服此类问题。 例如,我们可以过滤以字母“ L”开头的水果。 下面的查询将应用过滤器Fruit_Name和该过滤器能够检索水果谁与“L” chracter开始。

SELECT * FROM Fruits WHERE Fruit_Name LIKE 'L%'

At the same time, we can apply (%) percentage operator at any place or multiple times to thw filter pattern. In the following example, we will filter the fruits name which includes ‘n’ chracter.

同时,我们可以应用(%)百分比 操作员可以在任何地方或多次进行过滤。 在以下示例中,我们将过滤包含“ n”字样的水果名称。

SELECT * FROM Fruits WHERE Fruit_Name LIKE '%n%'

Another commonly used operator is (_) the underscore operator. This operator represents any character in the filter pattern. Assume that, we want to apply a filter to the fruit names which meet the following criterias:

另一个常用的运算符是( _ ) 下划线运算符。 该运算符表示过滤器模式中的任何字符。 假设,我们要对符合以下条件的水果名称应用过滤器:

  • The first character of the fruit name could be any character 水果名称的第一个字符可以是任何字符
  • The second character of the fruit name must be ‘a’ 水果名称的第二个字符必须为“ a”
  • The remaining part of the fruit name can contain any character 水果名称的其余部分可以包含任何字符

The following SQL example will meet all criteria.

以下SQL示例将满足所有条件。

SELECT * FROM Fruits WHERE Fruit_Name LIKE '_a%'

SQL示例:SELECT TOP语句 (SQL examples: SELECT TOP statement )

The SELECT TOP statement is used to limit the number of rows which returns the result of the query. For example, if want to retrieve only two rows from the table we can use the following query. Therefore, we can limit the result set of the query. In the following SQL examples, we will limit the result set of the query. Normally, the result of the query without TOP operator can returns much more rows but we force to limit returning row numbers of the query with TOP clause.

SELECT TOP语句用于限制返回查询结果的行数。 例如,如果只想从表中检索两行,则可以使用以下查询。 因此,我们可以限制查询的结果集。 在以下SQL示例中,我们将限制查询的结果集。 通常,没有TOP运算符的查询结果可以返回更多行,但是我们强制使用TOP子句限制查询的返回行号。

SELECT TOP (2) * FROM Fruits

At the same time, we can limit the result set of the SQL SELECT statement with a percent value. Such as, the following query returns only %60 percentage of result set.

同时,我们可以用百分比值限制SQL SELECT语句的结果集。 例如,以下查询仅返回结果集的%60%。

SELECT TOP (60) PERCENT * FROM Fruits

As you can see we added PERCENT expression to TOP operator and limit the result set of the query.

如您所见,我们将PERCENT表达式添加到TOP运算符,并限制了查询的结果集。

也可以看看 (See also)

For more articles on the SQL SELECT statement including SQL examples see

有关包含SQL示例SQL SELECT语句的更多文章,请参见

  • SQL SELECT INTO Statement SQL SELECT INTO语句
  • SQL INSERT INTO SELECT Statement Overview and Examples SQL INSERT INTO SELECT语句概述和示例

翻译自: https://www.sqlshack.com/sql-examples-for-beginners-sql-select-statement-usage/

sql初学者指南

sql初学者指南_初学者SQL示例:SQL SELECT语句的用法相关推荐

  1. sql初学者指南_使用tSQLt框架SQL单元测试面向初学者

    sql初学者指南 tSQLt is a powerful, open source framework for SQL Server unit testing. In this article, we ...

  2. sql查询初学者指南_适用于初学者SQL Server查询优化技巧与实际示例

    sql查询初学者指南 In this article, we will continue to learn essential techniques of the SQL Server query t ...

  3. sql查询初学者指南_面向初学者SQL Server查询执行计划–聚集索引运算符

    sql查询初学者指南 We have discussed how to created estimated execution plans and actual execution plans in ...

  4. sql查询初学者指南_面向初学者SQL Server查询执行计划–非聚集索引运算符

    sql查询初学者指南 Now that we understand what Clustered Index Scan and Clustered Index Seek are, how they o ...

  5. sql查询初学者指南_面向初学者SQL Server查询执行计划–类型和选项

    sql查询初学者指南 When a DBA is working with the SQL Server, he/she might sometimes say that the execution ...

  6. java初学者指南_企业Java中事务隔离级别的初学者指南

    java初学者指南 介绍 基于ACID事务属性的关系数据库强一致性模型. 在本文中,我们将阐明对资源本地事务和JTA事务使用不同的事务隔离级别和各种配置模式的背后原因. 隔离和一致性 在关系数据库系统 ...

  7. GBase 8s SQL 指南:教程———6编写高级SELECT语句

    6编写高级SELECT语句 本章中增大了使用SELECT语句可执行的操作的范围.并使您能够执行更复杂的数据库查 询和数据处理.编写SELECT语句着重于SELECT语句语法中的五个子句.本章添加了 G ...

  8. sql azure 语法_什么是Azure SQL Cosmos DB?

    sql azure 语法 介绍 (Introduction) In the Azure Portal, you will find the option to install Azure SQL Co ...

  9. selenium初学者指南_如何进化人工智能生活:初学者指南

    selenium初学者指南 'Evolving artificial intelligent life' might sound like a grandiose claim from an indi ...

最新文章

  1. R语言常用的统计函数
  2. MongoDB常用命令示例
  3. Nacos注册中心的部署与用法详细介绍
  4. linux 系统盘无法ls,系统故障排除
  5. 62 SD配置-交货凭证配置-定义交货类型
  6. 1091 N-自守数 (15分)
  7. Python的pyproject.toml文件中的tool.poetry.dev-dependencies选项
  8. 取消button的点击效果_(Vue动效)6.Vue中列表过渡效果
  9. 软件工程第一次作业(2)
  10. 一个比较完整的pytorch项目
  11. Linux学习(1)阿里云服务器及配置、使用xshell远程连接
  12. Excel曲线拟合的精度问题
  13. 乐优商城(14)–订单服务
  14. 智能DNS Smartdns 简介
  15. 蓝桥杯特殊回文数C语言简易版
  16. 动态规划——01背包——精卫填海
  17. 4位数字排列组合(所有组合与出现一次)
  18. omf多路径 oracle_ORACLE OMF
  19. 刺沙冰雪高爆版辅助多开方案
  20. 如何将页面换成黑白-滤镜

热门文章

  1. wpf绑定 dictionary 给定关键字不再字典中_为什么要在 JavaScript 中学习函数式编程?...
  2. Unity3D ARPG网络游戏编程实践
  3. PostgreSql之在group by查询下拼接列字符串
  4. 11.28 String类
  5. redis的linux安装
  6. IIS支持flv文件
  7. git由于网络原因导致 please make sure you have the correct access rights and the repository exists
  8. C++---STL中迭代器失效的总结
  9. 零基础带你学习MySQL—分页查询(十八)
  10. 【百度地图】——利用三级联动加载百度地图