SQL Cheatsheet - best practices for different flavors of SQL syntax

Learn about some best practices for working with SQL! This post is back by popular demand -- we all use SQL in our Retool apps fairly often. Example SQL snippets to use for the various SQL resource types can be super useful to have on hand.

Use arrays in queries

Every SQL database has a slightly different way of handling arrays. Because SQL resources in Retool are set to convert queries to prepared statements by default this can have more challenging interactions with multiple values inside a single parameter. Here are best practices for some of our most common databases without having to disable prepared statements.

Postgres
SELECT
  *
FROM
  users
WHERE
  id = ANY({{ [1, 2, 3] }})
MS SQL Server (2016+)
SELECT
  *
FROM
  users
WHERE
  id IN ( SELECT convert(int, value) FROM string_split({{ [1, 2, 3].toString() }}, ',') )
MS SQL Server (pre-2016)
SELECT
  *
FROM
  users
WHERE
  id IN ( SELECT Split.a.value('.', 'NVARCHAR(MAX)') DATA FROM ( SELECT CAST( '<X>' + REPLACE({{ [1,2,3,4,5,5,6].JOIN(',') }}, ',', '</X><X>') + '</X>' AS XML ) AS String ) AS A CROSS APPLY String.nodes('/X') AS Split(a) )
MySQL
SELECT
  *
FROM
  users
WHERE
  id IN ({{ [1, 2, 3] }})
BigQuery
SELECT
  *
FROM
  users
WHERE
  id
IN UNNEST({{ [1, 2, 3] }})
Cosmos DB
SELECT
  *
FROM
  c --c is the container ID input in the query editor UI WHERE array_contains( {{ [1,2,3] }}, c.id )
Databricks
SELECT
 * 
FROM
 users
WHERE
 contains ( {{ multiselect1.value.join() }}, user_name );
-- replace multiSelect1 with the source of the array you are using
-- replace user_name with the column name you want to reference

-- or another option here is:
WHERE user_name in (select explode(split({{ "'" + multiselect1.value.join(',') + "'" }} , ',')))
Apache Hive (JDBC)
SELECT *
FROM users
WHERE array_contains(split({{ multiselect.value.join() }}, ','), user_name)
SQLite (JDBC)
SELECT *
FROM users
-- Use Javascript to create object from array for use in query
WHERE json({{ JSON.stringify(multiselect.value.reduce((o,k)=>(o[k]=1,o),{})) }}) -> user_name

Databases with unique array structures

Snowflake
SELECT
  *
FROM
  PUBLIC.USERS
WHERE
  ARRAY_CONTAINS(ID::variant, SPLIT( {{[123,224].join()}}, ',') )
Redshift
SELECT
  *
FROM
  users
WHERE
  id IN ({{ [1, 2, 3].join() }})
OtherSQL
SELECT
  *
FROM
  users
WHERE CONTAINS({{','+'george,fred,chris'+',' }}, ',' || users.name || ',')

As a final fallback, another clever way of getting this to work in SQL databases that support substring matching is to convert your array into a comma separated string beginning and ending with a comma. If your column to matches with a comma added before and after (using the || operator), that would be a unique match.

Show all data when a filter is not in use

A common use case is to have a dropdown that allows a user to filter the users by status. However, if you want to show all statuses when no status is selected, you will need to use the following pattern to achieve your goal.

SQL
SELECT
  *
FROM
  users
WHERE ( {{ !select1.value }} OR users.status = {{ select1.value }} )
MS SQL Server
SELECT
  *
FROM
  users
WHERE ( {{!select1.value ? 1 : 0}} = 1 OR users.status = {{ select1.value }} )
Show data when filter is not in use for multiselect fields
SELECT * FROM "location"
JOIN leads ON leads_test."Location ID" = "location"."location_id"
WHERE
  ({{source_select.value == 0}}) -- This worked for me, not .length, but .value
  OR (leads_test."Source" = ANY ({{ source_select.value }})) -- Here you replace it for your multiselect

Thanks to @Member_Machine_Tech_Support for this one!^

Organize WHERE clauses

There are three approaches to keep in mind whenever you write queries with complex or specific conditional logic.

Combine logic often

Avoid writing redundant logic inside a WHERE clause and combine conditions to make queries quicker and easier to understand.

SQL
SELECT
  item, category
FROM
  food
WHERE (category = 'Fruit') AND (item = 'Orange' OR item = 'Apple')

This WHERE clause contains a redundant AND condition since both Orange and Apple are already part of the Fruit category. Removing unnecessary conditions speeds up queries and reduces complexity.

Organize logic

Use parentheses () to organize conditions. This helps your queries perform as expected and are easier to understand.

SQL
SELECT
  *
FROM
  users
WHERE (status IN ('Active', 'Trial') AND last_active = '01-01-2022') OR (owner = '{{current_user.fullName}}' )

Filter data with transformers

Use transformers to filter queries of smaller data sets. Transformers further reduce complexity and allow you to refine query data using JavaScript.

Transformers run client-side in the browser. For larger data sets, keep conditional logic within your queries so that your apps remain performant.

24 Likes
Use a list of values from a table in a SQL IN statement
Show all data when a filter is not in use + Use arrays in queries
Use variable in query without doble quote
Query database based on date range
Why can't I update my database?
Query multiple selected row from different tables
Can't filter SQL result by array - Snowflake
Query postgres with filter multiselect input
Show events on Calendar based on multiselect value
Multi-select value passthrough to SQL
Add if statement from Retool in SQL query
Matching multiselect with varchar array
Select multiple rows with a string of id seperated by commas
Using SelectedRows in a where clause
How to remove quotes from return value of a js
Batch Processing in SQL mode
Why is this query having syntax issue?
PSQL Query Not Running Properly in Workflow
Snowflake IN operator: issues with more than one IN operator with bound variables
Editing checkbox field and clicking save does nothing
Workflow - String value referenced from a block not working
Data Filters in Apps - dropdown/date filter in an App
Problems with a multiselect returning a NULL or empty array
Snowflake - trying to build a dashboard
Passing selected table values to a SQL IN statement
I am trying to show all data when filter is not in use
Need help with the following issue related to loops in sql?
Note column/field in Table
Help! Need bulk-information edit help
JS code to produce SQL WHERE clause from filterStack
Issue with Dynamic Variables in SQL Queries After Recent Update
MySQL Delete using IN Clause not deleting all rows
Issue with populating tables with multi select form element arrays
Passing Parameters via additionalScope for SQL Queries
Strange behavior on MSSQL IN Clause