Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 831
bluish Avatar asked Jan 19 '11 13:01

bluish


People also ask

What is a query statement in SQL?

An SQL SELECT statement retrieves records from a database table according to clauses (for example, FROM and WHERE ) that specify criteria. The syntax is: SELECT column1, column2 FROM table1, table2 WHERE column2='value';

Is a select statement similar to a query?

SELECT is the most common operation in SQL, called "the query". SELECT retrieves data from one or more tables, or expressions. Standard SELECT statements have no persistent effects on the database.

What is the difference between query and command?

Let's start by defining what commands and queries are. In its simplest form, a command is an operation that changes the state of the application. And, a query is an operation that reads the state of the application. In an application, data is represented using models.


1 Answers

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.

like image 116
Quassnoi Avatar answered Sep 18 '22 14:09

Quassnoi