patternModerate
What actually is a SQL clause?
Viewed 0 times
sqlclauseactuallywhat
Problem
This may sound like an overly simple question but I'm not finding it easy to locate a proper answer.
To the question "What are sql clauses?" most of the resources on internet simply provide a list of clauses and explain what they do.
But I'm trying to understand in abstract terms how it is defined. Like a generic notation that captures all kinds of queries that can be written and how clauses fit in there.
Are there properties common to all clauses? Why is UNION called an operator whereas HAVING is a clause?
To the question "What are sql clauses?" most of the resources on internet simply provide a list of clauses and explain what they do.
But I'm trying to understand in abstract terms how it is defined. Like a generic notation that captures all kinds of queries that can be written and how clauses fit in there.
Are there properties common to all clauses? Why is UNION called an operator whereas HAVING is a clause?
Solution
In general terms, a clause is just a logical chunk of a SQL statement - and, usually, is a chunk that is (theoretically) optional.
I'm a SQL Server guy, so I'll be thinking in those terms.
If I add a
I'll get back n rows (where n is the number of rows in the
You can add a
You'll note I said "
An operator, on the other hand is used to combine or compare two complete items. Just like the
I'm probably oversimplifying, but thinking in these terms should at least head you in the right direction.
I'm a SQL Server guy, so I'll be thinking in those terms.
SELECT GETDATE() is a valid SQL Server SELECT statement. It'll return the current date and time.If I add a
FROM clause:SELECT GETDATE()
FROM sys.objectsI'll get back n rows (where n is the number of rows in the
sys.objects table), each of which will have the same (unnamed) column, showing the current date and time.You can add a
WHERE clause to limit the number of rows you bring back; a GROUP BY clause to either deduplicate your data, or to allow you to put aggregate values in the SELECT list; a HAVING clause (usually to eliminate rows based on aggregate data); an ORDER BY clause to sort the data - but, none of those have to be there.You'll note I said "
SELECT list" when referring to the list of columns returned. I've never heard this referred to as a clause - presumably, because it does have to be there.An operator, on the other hand is used to combine or compare two complete items. Just like the
+ operator lets you combine numbers (1 + 2) or strings ([firstname] + ' ' + [lastname]), the UNION, EXCEPT, DIFFERENCE, and INTERSECT operators let you combine two SQL statements.I'm probably oversimplifying, but thinking in these terms should at least head you in the right direction.
Code Snippets
SELECT GETDATE()
FROM sys.objectsContext
StackExchange Database Administrators Q#171979, answer score: 10
Revisions (0)
No revisions yet.