Query a model with SQL
Use Snowflake SQL to query the tables, views, or dynamic tables created when you deploy a model. This guide explains how concepts, properties, and relationships are represented as rows and columns in those tables and views. You’ll also see how to select data from these tables and views and join them together.
- Prerequisites
- Your model is already deployed to a target schema. See Deploy a model to deploy it or set its target schema.
- You can query the model’s target schema in Snowflake.
- You know the database and schema that contain the model’s outputs.
What you can query in a deployed model
Section titled “What you can query in a deployed model”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
Personconcept identified byPerson.iddeploys as an output namedPERSON. Each row represents one person, and theIDcolumn 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.nameappears in theNAMEcolumn of thePERSONoutput. 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_atconnectsPersontoCompany. ThePERSON_WORKS_AToutput has one row per person-company association, with the person’s ID inPERSONand the company’s ID inCOMPANY. Deployment settings determine whether an output is a table, view, or dynamic table.
See how it works with an example model
Section titled “See how it works with an example model”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:
PERSONhas one row per person, with their ID inIDand their name inNAME.COMPANYhas one row per company, with its ID inIDand its name inNAME.PERSON_WORKS_ATconnectsPERSONandCOMPANY. ItsPERSONcolumn contains a person’s ID, and itsCOMPANYcolumn contains a company’s ID.
These names reflect the example model’s default naming. Your model and deployment settings may use different names.
Query concept instances
Section titled “Query concept instances”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 IDFROM PERSON;Each result row contains a Person instance’s ID.
Select property values
Section titled “Select property values”To return each company’s ID and name, select both columns from COMPANY:
USE <model_db>.<model_schema>;
SELECT ID, NAMEFROM COMPANY;Each result row contains a company’s ID and name.
Join concept and relationship data
Section titled “Join concept and relationship data”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_NAMEFROM PERSON_WORKS_AT AS works_atJOIN PERSON AS person ON works_at.PERSON = person.IDJOIN 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.
Where to go next
Section titled “Where to go next”For Snowflake SQL syntax, see Snowflake’s SELECT reference.