Skip to main content

Creating Tables and Views with SQL

Two SQL statements create Magpie tables from a query: CREATE TABLE … AS SELECT saves the query's result as a table, and CREATE VIEW saves the query itself. Each one does the same job as a Magpie command, save result as table and create table … from sql(...), so you can use whichever fits where you're working.

Run the SQL statements in a SQL paragraph, from a SQL client connected over JDBC, or in a Magpie paragraph wrapped in sql(...), where they run immediately. The examples assume an existing table of orders, sales.orders.

Saving a query's result as a table​

In SQL, Create Table As Select runs the query and saves its result in one statement:

CREATE TABLE analytics.orders_2026 AS
SELECT * FROM sales.orders WHERE year(order_date) = 2026

The Magpie equivalent takes two steps: run the query, then save the last result with Save Result:

sql(select * from sales.orders where year(order_date) = 2026);

save result as table orders_2026 in schema analytics;

Both store the table's data on the repository's default data source. Their options correspond:

CREATE TABLE … AS SELECTsave result as table
OR REPLACEwith replace
WITHOUT GRANTSwithout grants
IF NOT EXISTSNo equivalent
No equivalenttemporary, with data source, partitioning, with replace with delete, a JSON table spec

Use CREATE TABLE … AS SELECT when you're working in SQL and the default storage is enough. With IF NOT EXISTS, it's safe to rerun: if the table already exists, the query doesn't run at all. Its OR REPLACE refuses to replace a view, where with replace replaces whatever table has the name.

Use save result when you need to control how the table is stored: a JSON table spec with a persistence mapping (for example, a Delta table, which UPDATE, DELETE, and MERGE need), a particular data source, partitioning, a temporary table, or deleting the replaced table's data. Because running the query and saving it are separate steps, you can also check the result with show 10 from result before you save it.

Saving a query as a view​

In SQL, Create View saves the query:

CREATE VIEW sales.open_orders AS
SELECT order_id, customer_id, amount FROM sales.orders WHERE status = 'open'

The Magpie equivalent is Create Table From SQL:

create table open_orders in schema sales
from sql(select order_id, customer_id, amount from sales.orders where status = 'open');

Magpie carries out CREATE VIEW with the same code as create table … from sql(...), so both create the same kind of table: one that stores no data and runs its query each time it's read, so it always reflects the current contents of sales.orders. TEMPORARY and OR REPLACE work the same way in both, and $variables in the query are resolved when a session first reads the view, not when it's created. The value then holds for the rest of that session.

Use CREATE VIEW when you're working in SQL. To replace a view, CREATE OR REPLACE VIEW is the safer choice in either kind of paragraph: it refuses if the name belongs to a table that holds data. The Magpie create or replace table … from sql(...) doesn't check, so it would replace that table and leave its data files with no table pointing at them.

Use create table … from sql(...) when you want to pass a JSON table spec, or to replace a view without copying its grants (without grants).

Choosing a form​

You want toUse
Create a table from a query, with default storageCREATE TABLE … AS SELECT
Create a table only if it doesn't exist yetCREATE TABLE IF NOT EXISTS … AS SELECT
Choose the table's format, data source, or partitioningsave result as table
Look at a result before saving itsql(...), then show, then save result as table
Create or replace a viewCREATE [OR REPLACE] VIEW
Pass a JSON table spec for a viewcreate table … from sql(...)