+ 9

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

Are they the same?

17th Oct 2020, 10:57 PM
Sonic
Sonic - avatar
18 Réponses
+ 18
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.
18th Oct 2020, 2:54 AM
David Carroll
David Carroll - avatar
+ 7
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.
18th Oct 2020, 2:28 AM
Brian
Brian - avatar
+ 7
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
18th Oct 2020, 2:44 AM
chaithra Gowda
chaithra Gowda - avatar
+ 7
David Carroll it's more informative then we explained actually!! For sure it's not redundant its worthy(:
18th Oct 2020, 4:05 AM
chaithra Gowda
chaithra Gowda - avatar
+ 4
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?
18th Oct 2020, 4:43 AM
Sonic
Sonic - avatar
+ 3
SELECT * FROM users; Statement, query or both?
17th Oct 2020, 11:10 PM
Sonic
Sonic - avatar
+ 3
yes, these terms are synonyms
17th Oct 2020, 11:37 PM
Евгений
Евгений - avatar
+ 3
DOH... I started writing my response before Brian and Chaithra Gowda👑 had posted their replies. So... my response might be redundant.
18th Oct 2020, 2:57 AM
David Carroll
David Carroll - avatar
+ 3
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.
19th Oct 2020, 9:53 PM
Sonic
Sonic - avatar
+ 3
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. 🤓
19th Oct 2020, 10:28 PM
David Carroll
David Carroll - avatar
+ 2
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.
18th Oct 2020, 5:07 AM
David Carroll
David Carroll - avatar
+ 2
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.
19th Oct 2020, 8:41 PM
ViceKnightTA
ViceKnightTA - avatar
+ 2
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. 😉👌
19th Oct 2020, 9:47 PM
David Carroll
David Carroll - avatar
+ 2
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. 🙂
19th Oct 2020, 10:06 PM
ViceKnightTA
ViceKnightTA - avatar
+ 2
I have now added "SQL" before "query" in the question to avoid some confusion.
19th Oct 2020, 11:50 PM
Sonic
Sonic - avatar
+ 2
You mean "SQL statement" is the set.
22nd Oct 2020, 6:37 AM
Sonic
Sonic - avatar
+ 1
SQL is set and SQL query is subset of SQL
22nd Oct 2020, 2:08 AM
RAHUL YADAV
RAHUL YADAV - avatar