---
title: SQL
description: Supernova が受け付ける DuckDB ベースの SQL 方言、レイクの名前、有用な書き方、拒否される構文を学びます。
species: reference
---
# SQL

Supernova は DuckDB 1.4-LTS を基にした読み取り専用の SQL 方言を受け付けます。レイクのテーブルは `titan.<schema>.<table>` または `<schema>.<table>` で参照します。どちらも組織のレイク内で解決されます。

```sql
with ranked as (
  select
    customer,
    amount,
    row_number() over (partition by customer order by created_at desc) as recency
  from titan.stripe.charges
  where status = 'succeeded' and not _deleted
)
select * exclude (recency)
from ranked
qualify recency = 1
```

## 名前とスコープ

`titan` はカタログの別名であり、物理的なレイク ID ではありません。物理 ID をクエリに記述することはできません。修飾のないリレーション名は、そのスコープ内の CTE と別名だけに解決されます。

空白を含む識別子やキーワードと重なる識別子には二重引用符を使います。文字列リテラルには一重引用符を使います。

## 利用可能なクエリ構造

基本機能には CTE、内部結合と外部結合、`using`、ウィンドウ関数、`qualify`、`union all`、`union all by name`、`distinct`、`order by`、`limit`、`offset` があります。アスタリスクの修飾子を使うと、列数の多いソーステーブルを扱いやすくなります。

```sql
select * exclude (metadata, _synced_at)
replace (lower(email) as email)
from titan.hubspot.contacts
where not _deleted
```

リストのインデックスは 1 始まりです。JSON はキー、インデックス、JSON パスを指定した `->` と `->>` に対応します。高階のリスト関数は `x -> expression` と `lambda x: expression` の形式を受け付けます。

便利な組み込み関数として `date_trunc`、`strftime`、`strptime`、`date_diff`、`epoch_ms`、`split_part`、`regexp_extract`、`regexp_replace`、`string_agg`、`json_extract`、`unnest`、`generate_series`、`list_aggregate` があります。

## キャストと null

`cast(value as type)` は変換できないと失敗します。`try_cast(value as type)` はその行に null を返します。`is not distinct from` は null を安全に扱う等価比較を提供します。

```sql
select try_cast(property_annual_revenue as decimal(18, 2)) as annual_revenue
from titan.hubspot.companies
```

## 拒否される文

クエリからレイクの状態を変更することはできません。`insert`、`update`、`delete`、`merge`、`drop`、`alter`、`truncate`、`attach`、`copy`、`install`、`load`、トランザクション文、マクロ以外の `create` 文は、拒否した構文の名前を示す機能エラーを返します。

この方言は、ストアドプロシージャ、空間型、外部カタログ、ファイルの自動検出も拒否します。データの取り込みにはソース、永続的な派生テーブルにはモデル、送信先への書き込みには送信を使ってください。
