Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is whitespace optional in SQL queries?

I have noticed that using either Oracle or SQLite, queries like this are perfectly valid

SELECT*FROM(SELECT a,MAX(b)i FROM c GROUP BY a)WHERE(a=1)OR(i=2);

Is that a “feature” of SQL that keywords or words of a query need not be surrounded with whitespace? If so, why was it designed this way? SQL has been designed to be readable, this seems to be a form of obfuscation (particularly the MAX(b)i thing where i is a token which serves as an alias).

like image 491
Benoit Avatar asked Feb 09 '11 16:02

Benoit


1 Answers

SQL-92 BNF Grammar here explicitly states that delimiters (bracket, whitespace, * etc) are valid to break up the tokens, which makes the white space optional in various cases where other delimiters already break up the tokens.

This is true not only for SQLite and Oracle, but MySQL and SQL Server at least (that I work with and have tested), since it is specified in the language definition.

like image 94
RichardTheKiwi Avatar answered Nov 01 '22 03:11

RichardTheKiwi