Skip to content

Query a model with SQL

Deployment creates Snowflake tables, views, or dynamic tables in the model’s target schema. Source tables remain in their original schemas. The separate meta schema holds deployment metadata, such as refresh state. Monitor a deployed model shows how to list the tables, views, and dynamic tables in the model’s target schema.

How concepts, properties, and relationships map to SQL

Section titled “How concepts, properties, and relationships map to SQL”

Here’s how concepts, properties, and relationships map to the tables, views, and dynamic tables created during model deployment:

  • Concept: A Person concept identified by Person.id deploys as an output named PERSON. Each row represents one person, and the ID column contains that person’s identity key.
  • Property: A property attached to a concept appears as a column in that concept’s output. For example, Person.name appears in the NAME column of the PERSON output. A standalone property has its own output, with columns for the property’s fields.
  • Relationship: Each relationship has its own output. For example, Person.works_at connects Person to Company. The PERSON_WORKS_AT output has one row per person-company association, with the person’s ID in PERSON and the company’s ID in COMPANY. Deployment settings determine whether an output is a table, view, or dynamic table.

The queries below use a model of people and the companies they work at. They query three deployed outputs, each of which is a Snowflake table, view, or dynamic table:

  • PERSON has one row per person, with their ID in ID and their name in NAME.
  • COMPANY has one row per company, with its ID in ID and its name in NAME.
  • PERSON_WORKS_AT connects PERSON and COMPANY. Its PERSON column contains a person’s ID, and its COMPANY column contains a company’s ID.

These names reflect the example model’s default naming. Your model and deployment settings may use different names.

Replace the placeholders with the database and schema that contain your deployed model outputs. Set your Snowflake session to that schema, then select the ID column from PERSON:

USE <model_db>.<model_schema>;
SELECT
ID
FROM
PERSON;

Each result row contains a Person instance’s ID.

To return each company’s ID and name, select both columns from COMPANY:

USE <model_db>.<model_schema>;
SELECT
ID,
NAME
FROM
COMPANY;

Each result row contains a company’s ID and name.

To see each person’s name alongside the name of the company they work at, join PERSON_WORKS_AT to PERSON and COMPANY. Match its PERSON column to PERSON.ID and its COMPANY column to COMPANY.ID:

USE <model_db>.<model_schema>;
SELECT
person.ID AS PERSON_ID,
person.NAME AS PERSON_NAME,
company.ID AS COMPANY_ID,
company.NAME AS COMPANY_NAME
FROM
PERSON_WORKS_AT AS works_at
JOIN
PERSON AS person
ON works_at.PERSON = person.ID
JOIN
COMPANY AS company
ON works_at.COMPANY = company.ID;

The result includes the ID and name of the person and company referenced by each relationship row.

For Snowflake SQL syntax, see Snowflake’s SELECT reference.