Snowflake Cortex Analyst lets business users ask questions in natural language and have the service automatically generate and run SQL queries against structured data. It uses a semantic model (defined in YAML) to understand the underlying table structure and business logic, producing accurate SQL. This article walks through how to design the semantic model, how the conversion flow works, and how to set up permissions.
Cortex Analyst is a managed Snowflake service that takes a natural-language question and generates a SQL query based on a semantic model. Traditional BI tools require a dashboard designer to predefine queries up front; with Cortex Analyst, end users simply ask free-form questions and get answers back.
The generated SQL is restricted to SELECT statements, so there is no risk of data modification. Snowflake's RBAC is also enforced, so data the user's role cannot access never appears in the results.
[ビジネスユーザー]
│ 「先月の商品カテゴリ別売上は?」
▼
[Cortex Analyst API]
│
├─ 1. セマンティックモデル(YAML)を読み込み
│ └─ テーブル構造・カラムの意味・計算式を把握
│
├─ 2. LLMが自然言語を解析
│ └─ 意図を理解し、該当するテーブル・カラムをマッピング
│
├─ 3. SQLクエリを生成(SELECT文のみ)
│ └─ セマンティックモデルの定義に従ったJOIN・集計を構築
│
└─ 4. SQLを返却(自動実行 or ユーザー確認後実行)
└─ 結果はJSON形式でAPIレスポンスに含まれるSemantic models are written in YAML and placed on a Snowflake stage. The model defines logical tables, dimensions, measures, time dimensions, and relationships.
# semantic_model.yaml
name: sales_analytics
description: "EC売上分析用セマンティックモデル"
tables:
- name: orders
base_table:
database: ANALYTICS_DB
schema: PUBLIC
table: FACT_ORDERS
description: "注文トランザクションテーブル"
dimensions:
- name: order_id
expr: ORDER_ID
description: "注文の一意識別子"
data_type: VARCHAR
- name: product_category
expr: PRODUCT_CATEGORY
description: "商品カテゴリ(家電、食品、衣料品など)"
data_type: VARCHAR
time_dimensions:
- name: order_date
expr: ORDER_DATE
description: "注文日"
data_type: DATE
measures:
- name: total_revenue
expr: SUM(ORDER_AMOUNT)
description: "合計売上金額"
data_type: NUMBER
- name: order_count
expr: COUNT(DISTINCT ORDER_ID)
description: "注文件数"
data_type: NUMBER
- name: avg_order_value
expr: AVG(ORDER_AMOUNT)
description: "平均注文単価"
data_type: NUMBER
- name: customers
base_table:
database: ANALYTICS_DB
schema: PUBLIC
table: DIM_CUSTOMERS
description: "顧客マスタ"
dimensions:
- name: customer_id
expr: CUSTOMER_ID
description: "顧客ID"
data_type: VARCHAR
- name: region
expr: REGION
description: "顧客の所在地域"
data_type: VARCHAR
relationships:
- name: order_customer
left_table: orders
right_table: customers
join_type: LEFT
relationship_columns:
- left_column: CUSTOMER_ID
right_column: CUSTOMER_ID
verified_queries:
- name: "月次カテゴリ別売上"
question: "商品カテゴリ別の月次売上を教えて"
sql: |
SELECT
DATE_TRUNC('MONTH', o.ORDER_DATE) AS month,
o.PRODUCT_CATEGORY,
SUM(o.ORDER_AMOUNT) AS total_revenue
FROM ANALYTICS_DB.PUBLIC.FACT_ORDERS o
GROUP BY 1, 2
ORDER BY 1, 3 DESC| Element | Role | Required |
|---|---|---|
| tables | Defines the tables available for queries | Yes |
| dimensions | Columns used for grouping and filtering | Yes |
| time_dimensions | Time-based columns (dates and timestamps) | Recommended |
| measures | Aggregation expressions (SUM, COUNT, AVG, etc.) | Yes |
| relationships | JOIN definitions between tables | Required for multi-table models |
| verified_queries | Verified question-SQL pairs; few-shot examples that improve accuracy | Recommended |
| description | Natural-language descriptions of tables and columns | Critical for accuracy |
Cortex Analyst can be invoked via the REST API or the Python SDK. You can also embed it in a Streamlit app inside Snowsight.
# Python SDKでの呼び出し例
import json
from snowflake.core import Root
root = Root(session)
analyst = root.databases["ANALYTICS_DB"].schemas["PUBLIC"].cortex_analyst
response = analyst.send_message(
semantic_model_file="@my_stage/semantic_model.yaml",
messages=[
{"role": "user", "content": "先月の地域別売上トップ5を教えて"}
]
)
# レスポンスからSQLと結果を取得
for item in response.message.content:
if item.type == "sql":
print("Generated SQL:", item.statement)
elif item.type == "text":
print("Explanation:", item.text)Cortex Analyst permissions follow Snowflake's standard RBAC model.
| Operation | Required Privilege |
|---|---|
| Calling the Cortex Analyst API | SNOWFLAKE.CORTEX_USER database role |
| Reading the semantic model YAML | READ on the stage |
| Target tables for the generated SQL | SELECT on the tables |
| Query execution | USAGE on the warehouse |
-- Cortex Analyst利用ユーザーへの権限付与例
GRANT DATABASE ROLE SNOWFLAKE.CORTEX_USER TO ROLE analyst_role;
GRANT USAGE ON DATABASE analytics_db TO ROLE analyst_role;
GRANT USAGE ON SCHEMA analytics_db.public TO ROLE analyst_role;
GRANT SELECT ON ALL TABLES IN SCHEMA analytics_db.public TO ROLE analyst_role;
GRANT READ ON STAGE analytics_db.public.my_stage TO ROLE analyst_role;
GRANT USAGE ON WAREHOUSE analyst_wh TO ROLE analyst_role;Registering question-SQL pairs in the verified_queries section of the semantic model improves SQL generation accuracy when similar questions come in. This is a few-shot prompting approach and is particularly effective when you have business-specific terminology or logic.
Cortex Analyst
問題 1
Which statement about the Cortex Analyst semantic model is correct?
正解: B
Cortex Analyst semantic models are placed on a stage as YAML files and define tables, dimensions, measures, and relationships. A is wrong because the format is YAML on a stage, not JSON in a table. C is wrong because Cortex Analyst cannot be used without a semantic model. D is wrong because verified_queries is recommended but not required.
Where is the Cortex Analyst semantic model stored?
Semantic models are stored as YAML files on a Snowflake stage. You reference them via a stage path such as @my_stage/semantic_model.yaml, and Cortex Analyst uses the file to understand table structure, column semantics, and calculation logic before generating SQL. Git repository integration is also available for version control.
Is the SQL generated by Cortex Analyst safe?
Cortex Analyst only generates SELECT statements — it never produces INSERT/UPDATE/DELETE/DDL. The user's role permissions are also enforced at execution time, so it cannot touch tables or columns the user lacks access to. You can further restrict the query surface by limiting which tables are exposed in the semantic model.
When should I use Cortex Analyst vs. Cortex Search?
Cortex Analyst specializes in natural-language-to-SQL translation over structured data. It fits use cases like business users asking analytical questions such as "What are the top 10 products by sales last month?" Cortex Search, by contrast, specializes in hybrid search over unstructured text and is used for document search or as a RAG retriever.
Practice with certification-focused question sets
無料で問題を解いてみるNicheeLab Editorial Team
NicheeLab editorial team focused on data engineering and cloud certification learning. Content is structured around practical study needs and official exam domains.
Snowflake Certifications: All 11 Exams Explained (2026)
Every SnowPro certification — Associate, Core, Specialty, Ad...
Snowflake Exam Difficulty Ranking: All 11 Certs Compared (2026)
All 11 SnowPro exams ranked by difficulty with study-time es...
Snowflake Study Guide: Fastest Pass Route by Exam (2026)
How to pass SnowPro certifications efficiently — official ma...
SnowPro Core (COF-C03): Complete Exam Guide (2026)
Pass the SnowPro Core exam — six domains, scope, sample ques...
SnowPro Associate Platform (SOL-C01): Complete Guide (2026)
The entry-level SnowPro Associate exam — scope, weighting, s...