ModelingCubes
SQL
Define the SQL table or subquery that powers a cube.
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
- cubes
- cubes.data-source
- syntax.references