From c86c48203edf67a696a6e64c677a4792ff26079e Mon Sep 17 00:00:00 2001 From: Victoria Lim Date: Mon, 18 Apr 2022 09:28:32 -0700 Subject: [PATCH] recommendation for comparing strings and numbers (#12442) --- docs/querying/sql-syntax.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/docs/querying/sql-syntax.md b/docs/querying/sql-syntax.md index c6a6ffb1edd..5ad78065c5d 100644 --- a/docs/querying/sql-syntax.md +++ b/docs/querying/sql-syntax.md @@ -67,6 +67,21 @@ The WHERE clause refers to columns in the FROM table, and will be translated to WHERE clause can also reference a subquery, like `WHERE col1 IN (SELECT foo FROM ...)`. Queries like this are executed as a join on the subquery, described in the [Query translation](sql-translation.md#subqueries) section. +Strings and numbers can be compared in the WHERE clause of a SQL query through implicit type conversion. +For example, you can evaluate `WHERE stringDim = 1` for a string-typed dimension named `stringDim`. +However, for optimal performance, you should explicitly cast the reference number as a string when comparing against a string dimension: +``` +WHERE stringDim = '1' +``` + +Similarly, if you compare a string-typed dimension with reference to an array of numbers, cast the numbers to strings: +``` +WHERE stringDim IN ('1', '2', '3') +``` + +Note that explicit type casting does not lead to significant performance improvement when comparing strings and numbers involving numeric dimensions since numeric dimensions are not indexed. + + ## GROUP BY The GROUP BY clause refers to columns in the FROM table. Using GROUP BY, DISTINCT, or any aggregation functions will