SQL
Define the SQL table or subquery that powers a cube. Use the sql property to point to a physical table, or write a SELECT statement for derived datasets and transformations.
Overview
The sql property defines the base table or SQL subquery that a cube is built on. This is the foundation for all measures and dimensions in the cube.
Example
cubes:
- name: orders
sql: SELECT * FROM public.orders
- name: orders_with_users
sql: >
SELECT o.*, u.name as user_name
FROM public.orders o
LEFT JOIN public.users u ON o.user_id = u.idOptions
sql (required)
The SQL SELECT statement or table reference.
# Simple table reference
sql: SELECT * FROM orders
# Subquery with joins
sql: >
SELECT o.*, p.name as product_name
FROM orders o
JOIN products p ON o.product_id = p.id
# Using references to other cubes
sql: SELECT * FROM {users.sql()} WHERE active = truesql_table
Alternative to sql — directly reference a table without SELECT.
cubes:
- name: orders
sql_table: public.ordersBest Practices
- Prefer sql_table when selecting all columns from a single table
- Use subqueries for complex joins or filtering at the cube level
- Avoid aggregations in the base SQL — let measures handle that
See Also
Segments
Segments define reusable row-level filters that can be applied to any query on a cube. Use them for common filters like "active users" or "paid orders" that multiple consumers need.
Dimensions
Dimensions define the attributes used for grouping and filtering data in your semantic layer. They map to table columns and provide the axes for slicing measures in queries.