What's the difference between an SQL statement and an SQL query? | SoloLearn: Learn to code for FREE!


What's the difference between an SQL statement and an SQL query?

Are they the same?

10/17/2020 10:57:06 PM


18 Answers

New Answer


NOTE: Question was updated to specify _SQL_ query. ---- A SQL statement could be a query and a query could be a SQL statement... but, these are most definitely NOT the same or synonymous. A query can refer to any request for data based on various types of filtering criteria, projection, sorting, paging, size, etc instructions. Queries can apply to SQL and NOSQL databases, HTTP Requests for dynamically rendered web pages, REST APIs, SOAP Web Services, shell commands searching for files, SMS text messages, a verbal question, or any other interface that can request data. Queries could be a SELECT SQL statement in relational databases, the Find({}) or Aggregate([]) methods in MongoDB, MATCH / RETURN Cypher statements in Neo4j, etc. SQL statements refer to different commands in relational databases that apply to query (SELECT), as well as, update (INSERT, CREATE, UPDATE) and various other statements. So... as you can see, the meanings of a query and a SQL statement are not as narrow as you might have thought.


My own opinion is that if a SQL statement returns data then it is a statement that is a query, else it is merely a statement. So SELECT is both a statement and a query, whereas INSERT and UPDATE are only statements.


In simple while creating table the statement we use are called statements* we can't call them as queries:) Example:create tables, drop table, truncate tables ,delete ,update ,insert etc. queries are to retrieve the data(it just selects a part of data from database which are obeying specified condition) Eg:GROUP BY query, JOIN query, ORDER BY query , MAX ,MIN etc


David Carroll it's more informative then we explained actually!! For sure it's not redundant its worthy(:


David Carroll I should have rephrased the question to say SQL query and SQL statement but thanks for giving the bigger picture 😁. By including SMS text messages and verbal questions, are you referring to human queries eliciting human responses or are are you referring to natural language processing and speech to text based automated response systems?


SELECT * FROM users; Statement, query or both?


yes, these terms are synonyms


DOH... I started writing my response before Brian and Chaithra Gowda👑 had posted their replies. So... my response might be redundant.


Also found this which confirms that sql queries are a subset of sql statements. https://stackoverflow.com/questions/4735856/difference-between-a-statement-and-a-query-in-sql#:~:text=A%20statement%20is%20the%20general,a%20special%20kind%20of%20statement.&text=This%20also%20is%20evident%20in,(Data%20Query%20Language)%20statements.


ViceKnightTA LOL... Indeed... I can relate and completely get what you're saying when it comes to knowing how to water down the techno babble with a nontechnical audience. But, if a client was to explicitly ask me if there was a difference, I'd certainly be obliged to answer accordingly. 😉 Likewise, in a Q&A community targeting a technical audience of learners, it makes sense to provide answers that aren't so watered down. 🤓


Sonic SMS text could be either automated responses, similar to AI bot, or human responses. I went full blown human to human interaction with the verbal questions.


I think we should stop overcomplicating it for the OP's sanity. For all intents and purposes, in the SQL course and in the realm of databases, query and SQL statement are the same thing. Don't let this distract you from what really matters like the technical database concepts or the SQL syntax itself.


ViceKnightTA Let's assume the question was in the context of "the SQL course," to state that "query and SQL statement are the same thing" is like saying "questions and communication are the same thing." Even if we were to relax the manner in which we describe generalizations, we could say that "query" is a "SQL Statement", but we could not say the reverse that "SQL Statement" is a "query" which would imply all SQL Statements are queries. While this might seem to be an overcomplication, if people can't get these simple generalizations down about database terms, they're really going to struggle understanding cardinality / relationships and normalization concepts or how to write queries that involves joining multiple tables, as it were. 😉👌


David Carroll I don't know man...at the end of the day, some of my customers/clients call it a "query" and others call it an "sql statement", if I tried to be overly remedial and correct them by thoroughly explaining the difference just as you stated, instead of just performing the work based on their requests or requirements, pretty sure they'd get annoyed and/or laugh our firm out of their contract 😂. Agree with being thorough and specific about certain concepts when learning, but generalizing between a 'query' and 'sql statement' certainly hasn't hurt my understanding of the overall subject matter. 🙂


I have now added "SQL" before "query" in the question to avoid some confusion.


You mean "SQL statement" is the set.


SQL is set and SQL query is subset of SQL


A statement end with a semicolons And a set of statement is a query It's hard to understand in some why but remember it's not true always 😉