Skip to main content

Raw SQL

Raw SQL fields execute custom PostgreSQL queries to produce computed values directly from the database.

Go to StudioCollections → [Collection Name] → Fields tab → Add Field, then select Raw SQL. To edit an existing field, click the edit action next to it.

View all settings
SettingDescription
Field NameText input, required. Unique identifier for the field.
Input TypeDropdown, required. Choose Single, Array, Object, or Object Array.
Display LabelText input. Human-readable label shown in forms.
IconIcon picker. Visual identifier.
Raw SQLCode editor, required. PostgreSQL query to execute.
StatusToggle, defaults to true. Enable or disable the field.
RequiredCheckbox. Make the field mandatory.
DescriptionText area. Documents the field's purpose.

Input Types

  • Single — Returns a single scalar value
  • Array — Returns an array of values
  • Object — Returns a JSON object (single row)
  • Object Array — Returns an array of JSON objects (multiple rows)

When to Use

Choose Raw SQL when:

  • You need database-specific PostgreSQL functions
  • Complex calculations not possible with Compute fields
  • Window functions or advanced aggregations
  • Subqueries and cross-collection joins

Settings

See Common Settings for shared options.

Not available: Default Value, Primary Key, Unique, Faker Type, Form Visibility, DB Field Name

Raw SQL Editor (Basic Settings)

Code editor, required. Write PostgreSQL queries to compute the field value. The editor provides syntax highlighting for SQL.

SELECT * FROM users;

Advanced Settings

Only Description is available in Advanced Settings for this field type.

Example Expressions

-- Concatenate with formatting
CONCAT(first_name, ' ', last_name)

-- Date calculations
NOW() - created_at

-- Conditional logic
CASE WHEN status = 'active' THEN 1 ELSE 0 END
note

Raw SQL fields are read-only — no database column is created.

warning

Invalid SQL causes query errors. Test queries in a SQL editor before saving.