How to Use Variables in a Query

Use variables in your SQL queries by wrapping them in a double curly braces, like {{id}}. This feature is only available to Premium users.

select *
from users
where id = {{id}}

Screenshot of an ID query variable

If your variable type is a string, you'll need to wrap it in quotes:

select *
from users
where email = '{{email}}'

If your variable name ends with _date or _at, PopSQL will show you a datepicker where you can easily select a date.

select *
from users
where created_at between '{{created_at}}' and '{{end_date}}' -- both of these will show a datepicker in the UI


PopSQL uses Liquid as its template language, so there's a lot of useful stuff you can do.

Default values

You can assign a default value to a variable by using the default filter:

select *
from movies
where category = '{{ category | default: 'horror' }}'

Control flow

You can use an if and unless statements in your query:

select *
from users
{% unless skip_join %}
  inner join orders on orders.user_id =
{% endunless %}
{% if email %}
  where = '{{email}}'
  limit 10
{% endif %}

Ready for a modern SQL editor?