SQL Syntax

SQL Syntax

From LongJump Support Wiki

SQL Syntax
Jump to: navigation, search

Here is the syntax for the SQL SELECT statement that the SQL parser recognizes.


About SQL Syntax

  • SQL syntax is case insensitive.
  • Field and table names are case sensitive
  • [ x ] - Optional (one or none)
  • [, x ... ] - Optional additional values, in a comma-separated list
  • x | y - Choose one. Curly braces are added when needed: { x | y } ...
  • CAPITALIZED - SQL Keyword (case insensitive)
  • italicized - Value you supply. (Table names and column names are case-sensitive)


When you make a SQL query, you are not querying the platform database directly. Rather, you are querying a view of the database that is restricted by your role permissions, so the records and fields you can see using SQL are the same as those you see when using the GUI. In addition, the platform implements various safeguards to prevent SQL Injection attacks.

  • SQL syntax is not case-sensitive, except for table names and field names.
  • Backticks are (`) are needed around any table or field name that is one of the SQL Reserved Words.
For example: SELECT * FROM `Order`
Without the backticks, you get an error like this:
Encountered "Order" at line column 15. Was expecting one of (...
That error means the parser found a word it recognized, but didn't find other things it expected before it got to that word.
  • When either a table name or a field name is reserved word, and both are used together, then backticks are needed around the combination.
For example: `Order.order_number`
  • If you do a query on different tables that have fields with the same name, you should provide column aliases for those fields. (Otherwise, a query for A.phone and B.phone identifies both sets of values as "phone". A column alias like A.phone AS A-phone makes it possible to tell which table a set of values came from.)
Learn more: column_expr
Learn more:

SELECT Statement

In a select statement, you designate one or more columns separated by commas (or "*" for all columns), plus a table or join to get the data from, and additional options:

SELECT * FROM Customers
SELECT * FROM Customers LIMIT 80
SELECT * FROM Customers ORDER BY zip_code, customer_name
[(] column_expr [, column_expr ...] [)]
FROM [(] table [, table ...] [)]
[WHERE where_clause]
[GROUP BY col_name [ASC|DESC]  [, col_name [ASC|DESC] ...]
[HAVING where_clause]
[ORDER BY col_name [ASC|DESC]  [, col_name [ASC|DESC] ...]
[LIMIT {maximum_rows | offset, maximum_rows} ]


Eliminates duplicate rows from the result set.
For example: DISTINCT(customer_name, address)
(Parentheses are optional, but must be matched if present.)
Adds summary rows that aggregate values for each group.
Is only allowed as part of GROUP BY, for performance reasons. (WHERE does the same thing outside of a GROUP BY clause, only more efficiently.)
Ascending (the default).
Maximum number of rows to return.
The row to start from. Offset for the first row is zero (0).
Learn more:


*  |  table_alias.*  |  col_name [ [AS] col_alias ]  |  expr [AS] col_alias


Used for the column name in the result set in the SQL Browser, or for the tag name in the REST execSQL resource.
Required when an expression is specified.
Can be used as a field name in a group_by_clause or order_by_clause.
For example:
SELECT CONCAT(last_name,', ',first_name) AS full_name
FROM Customer_Contacts ORDER BY full_name;


SELECT cust.customer_name, orders.order_amount
   FROM `Order` AS orders, Customer AS cust
   GROUP BY cust.customer_name WITH ROLLUP


Note: The "Order" table must be enclosed in backticks, because that name is a reserved word in SQL. (If used to qualify a field name, the syntax would be: `Order.fieldName`.)
  table_name [ [AS] table_alias ]
| join_table


table [INNER | LEFT | RIGHT] JOIN [(] table [, table ...] [)]
[ON on_clause]
Learn more: About Table Joins


on_condition [ {AND|OR} on_condition ...]


  table_alias.col_name IS [NOT] NULL
| table_alias.col_name = table_alias.col_name
| table_alias.col_name != table_alias.col_name
| ( on_clause )
  • If multiple tables are specified, a JOIN is implied
  • An INNER join (aka a STRAIGHT join) is the default.
  • table_alias is required when specifying columns in a JOIN.


An expression that evaluates to true or false for a row. When true, the row is selected.

SELECT orders.order_number, Item.amount
   FROM `Order` AS orders, OrderItems AS Item
   WHERE Item.related_to_Orders = orders.id
   ORDER BY orders.order_number


condition [ {AND|OR} condition ...]


  expr IS [NOT] NULL
| expr [NOT] LIKE wildcard_string
| expr [NOT] REGEXP pattern_string
| expr [NOT] IN (expr [, expr ...])
| expr = expr
| expr < expr
| expr > expr
| expr != expr
| expr <= expr
| expr >= expr


| number
| boolean
| date/time
| column_name
| SQL Function
| + expr
| - expr
| (expr [, expr ...] )


A sequence of alphanumeric characters and/or symbols, enclosed in single quotes:

'a string'

Strings can also "escape sequences" for characters that do not appear on the keyboard:

\0 ASCII NUL (0x00).
\' Single quote (“'”).
\" Double quote (“"”).
\b Backspace character.
\n Newline (linefeed).
\r Carriage return.
\t Tab character.
\\ Backslash (“\”).
\% The “%” character. Used in a [NOT] LIKE expression, so the character isn't interpreted as a wildcard.
\_ The “_” character. Used in a [NOT] LIKE expression, so the character isn't interpreted as a wildcard.
\Z ASCII 26 (Control+Z). To prevent the character from being interpreted as End-of-File in Windows.


A string containing one or more wildcards, used in a [NOT] LIKE expression:

xx yy
% Matches any number of characters, or zero characters.
_ Matches exactly one character.


A string containing a regular expression, used in a [NOT] REGEXP expression:

Character(s) Meaning
. Any character, including newlines.
* Zero or more of previous character. As in: '.*'
+ One or more of previous character. As in: 't+'
? Zero or one of previous character. As in: 'T?'
| XYZ'
(...) XYZ) Company', '(ABC|XYZ)( Company)?'
(The latter pattern matches "ABC", "XYC", "ABC Company", and "XYZ Company".)
[...] Character set:
[abcdef] - match any of the characters in the set
[a-f] - match any of the characters in the range
[a-fA-FzZ] - Combination, Match any of the specified characters,or any of the characters in either range.
[^...] - Negation. Match anything other than the characters specified in the set.
^ Beginning of a string when it is in a pattern.

"Not", when it is the first character in a character set.

$ End of string.
Since strings use backslash escape syntax (for example, “\n” to represent the newline character), you must double any backslash characters you use in your REGEXP strings. For example, to include a question mark (?) in a string, the pattern matcher needs to see \?. (So it looks for a question mark character, instead of interpreting the question mark as "zero or one".) To get that string to the pattern matcher, you specify \\?. String processing reduces the \\ to \, so the regular expression processor sees \?, as intended. For example, this expression evaluates as true:
SELECT 'Question?' REGEXP 'Q.*\\?'


Numbers can be specified as integers:


Or as floating point/decimal numbers:



Boolean true/false values can be specified as integers:

1 (true)
0 (false)


Learn more: SQL Date and Time Functions
Date "As Entered" Date/Time "Convert to Local" Date/Time
SQL Browser 'YYYY-MM-DD'
(UTC Format)
'YYYY-MM-DD hh:mm:ss' 'YYYY-MM-DDThh:mm:ssZ'
(UTC Format)
(UTC Format)
'YYYY-MM-DD hh:mm:ss' 'YYYY-MM-DDThh:mm:ssZ'
(UTC Format)
Java User format, specified
in Company Information
User format, in user timezone User format, in user timezone
Personal tools