Contents

Chapter 1

Reusable Query Library

A collection of SQL query patterns that work across all Retool templates. Copy these into your Retool queries and adjust table/column names for your schema.

Pagination

sql
-- Offset-based pagination (works with Retool's table component)
SELECT *
FROM your_table
ORDER BY created_at DESC
LIMIT {{table1.pageSize}}
OFFSET {{table1.paginationOffset}}
sql
-- Case-insensitive search across multiple columns
SELECT *
FROM your_table
WHERE (
  {{searchInput.value}} = ''
  OR name ILIKE '%' || {{searchInput.value}} || '%'
  OR email ILIKE '%' || {{searchInput.value}} || '%'
  OR id::text = {{searchInput.value}}
)

Date Range Filtering

sql
-- Filter by date range picker component
SELECT *
FROM your_table
WHERE created_at BETWEEN {{dateRange.start}} AND {{dateRange.end}}

Aggregation with Filters

sql
-- Dashboard statistics with conditional aggregation
SELECT
  COUNT(*) as total,
  COUNT(*) FILTER (WHERE status = 'active') as active_count,
  COUNT(*) FILTER (WHERE created_at >= CURRENT_DATE) as today_count,
  ROUND(AVG(some_metric), 2) as avg_metric
FROM your_table
WHERE created_at >= CURRENT_DATE - INTERVAL '30 days'

Upsert Pattern

sql
-- Insert or update based on a unique key
INSERT INTO your_table (unique_key, name, value, updated_at)
VALUES ({{form.data.key}}, {{form.data.name}}, {{form.data.value}}, NOW())
ON CONFLICT (unique_key) DO UPDATE SET
  name = EXCLUDED.name,
  value = EXCLUDED.value,
  updated_at = NOW()

Soft Delete

sql
-- Soft delete with audit trail
UPDATE your_table
SET
  status = 'deleted',
  deleted_at = NOW(),
  deleted_by = {{current_user.id}}
WHERE id = {{table1.selectedRow.id}}

Audit Log Insert

sql
-- Log an action to an audit trail
INSERT INTO audit_logs (user_id, action, resource_type, resource_id, metadata, ip_address, created_at)
VALUES (
  {{current_user.id}},
  'update',
  'order',
  {{table1.selectedRow.id}},
  jsonb_build_object('old_status', {{table1.selectedRow.status}}, 'new_status', {{statusSelect.value}}),
  {{current_user.ip}},
  NOW()
)

Bulk Operations

sql
-- Bulk update using selected rows from a table
UPDATE your_table
SET status = {{statusSelect.value}}, updated_at = NOW()
WHERE id = ANY(ARRAY[{{table1.selectedRows.map(r => r.id).join(',')}}]::int[])

Time-Series for Charts

sql
-- Generate time-series data for chart components
SELECT
  DATE(created_at) as date,
  COUNT(*) as count,
  SUM(amount) as total_amount
FROM your_table
WHERE created_at >= CURRENT_DATE - INTERVAL '30 days'
GROUP BY DATE(created_at)
ORDER BY date
Chapter 2

Resource Configuration Guide

Connecting Data Sources in Retool

Before importing templates, you need to configure at least one data source (called a "Resource" in Retool).

PostgreSQL

1. Go to Resources in the top navigation

2. Click "Create new"PostgreSQL

3. Fill in:

  • Host: Your database hostname
  • Port: 5432 (default)
  • Database name: Your database name
  • Username: A database user (use read-only for dashboards)
  • Password: The user's password
  • SSL: Enable for production databases

4. Click "Test connection" then "Create resource"

REST API

1. Go to Resources"Create new"REST API

2. Configure:

  • Base URL: https://api.example.com/v1
  • Headers: Add Authorization: Bearer YOUR_TOKEN

3. These templates reference REST APIs for some integrations

MySQL

Same as PostgreSQL, but select MySQL as the resource type. Port default is 3306.

Environment Variables

Set up environment variables in Retool for values that differ between staging and production:

1. Go to SettingsEnvironment Variables

2. Add variables like:

  • DATABASE_HOST
  • API_BASE_URL
  • SLACK_WEBHOOK_URL

3. Reference them in queries as {{env.DATABASE_HOST}}

Security Best Practices

  • Read-only users: Create a database user with SELECT-only permissions for dashboard queries
  • Write users: Use a separate user with INSERT/UPDATE permissions only for the specific tables each template modifies
  • Connection pooling: Retool manages connections, but configure your database to accept enough connections
  • IP allowlisting: Retool Cloud connects from specific IP ranges — add them to your database firewall
  • Audit logging: Enable query logging on your database for compliance
Retool Dashboard Templates v1.0.0 — Free Preview