Sub topic 9
Making queries reusable with parameters
This chapter assumes you can run a query in the SQL editor and that you know the catalog.schema.table naming from the previous chapters. It covers named parameters, which let one saved query run with different values and without editing the SQL text.
Why use parameters
Suppose you often check long, expensive taxi trips. You could edit the number in the WHERE clause each time. That is error-prone, and anyone you share the query with must edit the text too. A parameter replaces the literal value with a marker. The editor then shows an input field for it.
Named parameter markers
A named parameter marker is a name with a colon in front of it, such as :min_distance. When the editor sees a marker, it adds a widget for that name below the query text.
SELECT
pickup_zip,
trip_distance,
fare_amount
FROM samples.nyctaxi.trips
WHERE trip_distance > :min_distance
ORDER BY fare_amount DESC
LIMIT 50;Type a value such as 10 into the min_distance field and run the query. Change the value and run it again. The SQL text does not change.
How a run uses a parameter
The value is passed to the warehouse as a typed value. It is not pasted into the text as a string. This is why a parameter is safer than building SQL by hand with string concatenation.
Setting the type of a parameter
By default a parameter is treated as text. Comparing a number column to text can give surprising results or errors. Open the parameter's settings in the editor and choose a type such as number, date, or a list of allowed values. Choose the type that matches the column you compare against.
| Type | Use it for | Example value |
|---|---|---|
| Text | Names, codes, ZIP values stored as strings | `10001` |
| Number | Thresholds and limits | `10` |
| Date | Time ranges | `2016-01-15` |
| Dropdown list | A fixed set of allowed values | `Manhattan` |
Common errors
- Missing value. A run fails if a marker has an empty field. Enter a value before you run.
- Wrong type.
:min_distanceset to text against a numeric column may fail or filter incorrectly. Set the type to number. - Quotes around the marker. Write
:min_distance, not':min_distance'. Quoting turns the marker into a plain string. - Using a marker for an identifier. A marker stands for a value. It cannot replace a table or column name.
Parameters and other features
A query with parameters can still be saved and shared. It is also a good source for a visualization, because you can change a value and rerun to see the chart update. The query profile for each run shows the values that you used, so you can compare a fast run with a slow one.
Exercise
You wrote WHERE fare_amount > ':min_fare' and the query returns no rows or an error, even with a valid number in the field. What is wrong, and how do you fix it?
Show answer
The quotes make ':min_fare' a literal string instead of a parameter marker, so no input value is used. Remove the quotes and write WHERE fare_amount > :min_fare. Then set the parameter type to number.
Exercise
Why is a typed parameter safer than editing the number in the SQL text, when you share the query with others?
Show answer
The value is passed to the warehouse separately from the SQL text, so it cannot change the structure of the query. Others also do not need to edit the SQL, which avoids accidental changes.