Difference between statement and query in SQL

I still live in this ambiguity: conceptually, what is the difference between an operator and query in SQL? Can someone give a definition for each of them? This would be useful, for example, when choosing variable names within programs so that it is clear to everyone. Thanks!

OPTIONAL:. How can I call a piece of SQL code made by more than one statement, where the statements are separated by a semicolon ( ; )? Who has already answered, can edit his answer. Many thanks!

+24
source share
6 answers

An operator is any text that the database engine recognizes as a valid command. Starting with 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 an operator that returns a set of records (possibly empty).

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

A series of SQL sent to the server immediately is called a package.

Not all SQL engines required statements in a batch to be separated by a semicolon. SQL Server , for example, typically does not execute or break contextual statements. The CTE expression starting with WITH is a notable exception.

+21
source

Operator - any SQL command, such as SELECT, INSERT, UPDATE, DELETE.

A query is synonymous with a SELECT statement.

+8
source

From Wikipedia - Elements of the SQL language

The SQL language is divided into several elements of the language, including:

  • Clauses that are components of statements and queries. (In some cases, this is optional.) [9]
  • Expressions that can produce either scalar values ​​or tables consisting of columns and rows of data.
  • Predicates that define conditions that can be evaluated for three-valued SQL (3VL) or Boolean (true / false / unknown) values ​​and which are used to limit the effects of statements and queries or to change the flow of a program.
  • Queries that retrieve data based on specific criteria.
  • Statements that can have a permanent impact on schemas and data or manage transactions, program flows, connections, sessions, or diagnostics.
    • SQL statements also include the terminator of the term semicolon (";"). Although not required on every platform, it is defined as a standard part of SQL grammar.
  • Minor spaces are usually ignored in SQL statements and queries, which simplifies the formatting of SQL code for readability.
+7
source

A statement is a general term for the part of complete, correct SQL that you can send to the DBMS. A query is an operator that will return data, so a query is a special expression.

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

+4
source

They are used interchangeably by most, but some often use the word β€œquery” to mean, in particular, SELECT , because when you ask for something or someone, you need information. And SELECT queries return result sets to match the description. This is also evident in the fact that SELECT formally called DQL (Data Query Language) SELECT .

+1
source

Queries are used to retrieve data based on certain criteria, but the operator can have a permanent impact on schemas and data, or manage transactions, program flow, connections, sessions, or diagnostics. See Also Wikipedia .

0
source

All Articles