Difference between a statement and a query in SQL

47,564

Solution 1

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.

Solution 2

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

A query is a synonym for a SELECT statement.

Solution 3

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.

Solution 4

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.

Solution 5

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.

Share:
47,564
bluish
Author by

bluish

I'm here to learn and to improve help retrieval about IT. #SOreadytohelp You can contact me at rumoreblu snail gmail dot com ;)

Updated on July 09, 2022

Comments

  • bluish
    bluish almost 2 years

    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. Thanks!

    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!

  • onedaywhen
    onedaywhen over 13 years
    Is a SELECT..INTO (doesn't return a resultset) a query? Is (VALUES (1)) AS T (col); (does return a resultset) a query?
  • onedaywhen
    onedaywhen over 13 years
    "SELECT statements are formally called DQL" -- I think the use of DML. DDL, DCL, etc are informal. Who is the trusted source for such matters? I've seen various definitions of DCL. SELECT is normally associated with DML, IMO. I don't think DQL is commonly used, other than to mean Doctrine Query Language or a typo for SQL (consider that the D key is next to the S key).
  • ARK
    ARK about 9 years
    "A statement is any text that the database engine recognizes as a valid command". Please tell difference between command and query.
  • NoName
    NoName over 2 years
    So queries are a subset of statements that do not alter the database?