开发者

Difference between a statement and a query in SQL

I still live in this ambiguity: conceptually what's the difference between a statement and a query in SQL? Can anybody give a definition for each of them? It would be useful, for example when choosing variables names inside programs in a way that will be clear for everybody. Tha开发者_StackOverflow中文版nks!

ADDITIONALLY: How can I call a chunk of SQL code made by more than one statement where statements are separated by a semicolon (;)? Who already replied can edit his answer. Many thanks!


A statement is any text that the database engine recognizes as a valid command. As of SQL-92:

An SQL-statement is a string of characters that conforms to the format and syntax rules specified in this international standard.

A query is a statement that returns a recordset (possibly empty).

How can I call a chunk of SQL code made by more than one statement where statements are separated by a semicolon (;)? Who already replied can edit his answer. Many thanks!

A series of SQL statements sent to the server at once is called a batch.

Not all SQL engines required the statements in a batch to be semicolon delimited. SQL Server, for instance, generally does not and breaks the statements based on context. CTE statements starting with WITH are a notable exception.


A statement is any SQL command such as SELECT, INSERT, UPDATE, DELETE.

A query is a synonym for a SELECT statement.


From Wikipedia - SQL Language Elements

The SQL language is sub-divided into several language elements, including:

  • Clauses, which are constituent components of statements and queries. (In some cases, these are optional.)[9]
  • Expressions, which can produce either scalar values or tables consisting of columns and rows of data.
  • Predicates, which specify conditions that can be evaluated to SQL three-valued logic (3VL) or Boolean (true/false/unknown) truth values and which are used to limit the effects of statements and queries, or to change program flow.
  • Queries, which retrieve data based on specific criteria.
  • Statements, which may have a persistent effect on schemas and data, or which may control transactions, program flow, connections, sessions, or diagnostics.
    • SQL statements also include the semicolon (";") statement terminator. Though not required on every platform, it is defined as a standard part of the SQL grammar.
  • Insignificant whitespace is generally ignored in SQL statements and queries, making it easier to format SQL code for readability.


A statement is the general term for a piece of complete, correct SQL that you can send to a DBMS. A query is a statement that will return data, thus a query is a special kind of statement.

A SELECT ... would be a query, a DELETE... just a statement.


They're used interchangeably by most, but some often use the word "query" to mean, specifically, SELECT statements, because when you query something or someone, you want information. And SELECT queries return result sets, so that'd fit the description nicely. This also is evident in the fact that SELECT statements are formally called DQL (Data Query Language) statements.


Queries is used for retrieve data based on specific criteria, but statement may have a persistent effect on schemas and data, or which may control transactions, program flow, connections, sessions, or diagnostics. See also Wikipedia.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜