SELECT player_name, season = exploded_season AS is_active FROM nba_player_seasons CROSS JOIN UNNEST (sequence(1996, 2002)) AS t(exploded_season) WHERE player_name = 'Michael Jordan';
Please help me understand the 'season = exploded_season' in the above SQL? As I understand, such expression can not appear in SELECT.
ChatGPT said:
Great question! You're right to pause here — season = exploded_season AS is_active in the SELECT clause might look confusing at first glance.
Let’s break down this line:
✅ What it actually means:
This is a boolean expression with an alias.
-
season = exploded_seasonis a comparison — it checks whether theseasoncolumn is equal to the valueexploded_season(which comes from theUNNESTed sequence). -
AS is_activeis just giving this boolean result a column alias.
So this line is saying:
“Return a column named
is_active, which isTRUEifseason = exploded_season, otherwiseFALSE.”
🔎 So what is exploded_season?
Let’s look at the FROM clause:
This means:
-
You're generating a list of numbers from
1996to2002usingsequence(). -
You're unnesting that array into rows, with each value aliased as
t(exploded_season).
So exploded_season is each season from 1996 to 2002.
✅ Full query explained
SELECT player_name, season = exploded_season AS is_active FROM nba_player_seasons CROSS JOIN UNNEST (sequence(1996, 2002)) AS t(exploded_season) WHERE player_name = 'Michael Jordan';
This query:
-
Filters the table for rows where
player_name = 'Michael Jordan'. -
Joins each such row to every season from 1996 to 2002 (using
CROSS JOIN UNNEST). -
Outputs two columns:
-
player_name -
A boolean
is_active, which isTRUEonly whenseason = exploded_season.
-
📝 Example output
| player_name | is_active |
|---|---|
| Michael Jordan | FALSE |
| Michael Jordan | TRUE |
| Michael Jordan | FALSE |
| ... | ... |
Let me know if you'd like to adjust this to show the seasons explicitly or pivot the result to one row per player with columns per season.
You said:
ChatGPT said:
Excellent follow-up. The expression:
is not valid in all standard SQL — it's a feature supported only in some SQL dialects, especially those that allow boolean expressions directly in the SELECT clause.
🔍 Let’s break it down:
✅ Valid in these databases:
| Database | Supports boolean_expr AS alias directly? | Notes |
|---|---|---|
| PostgreSQL | ✅ Yes | Fully supports boolean expressions in SELECT, WHERE, GROUP BY, etc. |
| Trino/Presto | ✅ Yes | Also supports UNNEST and inline booleans |
| BigQuery | ✅ Yes | Allows SELECT a = b AS is_equal |
| SQLite | ✅ Yes | Evaluates to 0 (false) or 1 (true) |
| MySQL | ⚠️ Yes, but returns 0/1 | Booleans are treated as integers |
| SQL Server | ❌ No | You must use CASE WHEN instead |
| Oracle | ❌ No | Booleans not directly selectable; must use CASE |
🧱 In standard SQL (ISO SQL):
Standard SQL does not allow using a boolean expression like a = b directly in the SELECT list unless:
-
It is wrapped in a
CASEexpression or -
The DBMS extends the standard to support it.
✅ Standard-compatible version:
SELECT CASE WHEN season = exploded_season THEN TRUE ELSE FALSE END AS is_active
This works everywhere.
✅ Recommendation:
If you're working in:
-
PostgreSQL / Trino / BigQuery:
season = exploded_season AS is_activeis fine. -
SQL Server / Oracle: Use
CASEinstead. -
Portable code (multi-platform): Always use
CASE WHEN.

浙公网安备 33010602011771号