Snowflake SQL Tutorial 108 - A SELECT Star View Does Not Go Stale, It Breaks

Snowflake SQL · Episode 108

In this post we are going to cover views in Snowflake.

A view is not a table that updates itself. It is a query with a name. You give Snowflake a SELECT, Snowflake saves it, and every time somebody reads the view that SELECT runs again.

Almost everything surprising about a view follows from that one sentence, so we will start with the simplest one and keep asking what it is really holding. The part that catches most people out is what SELECT * does inside a view, and it is not going stale.

Every query below was run on a real Snowflake account, and every result you see is the output it gave back.

Episode 108 of the Snowflake SQL: Zero to Hero series on TechBrothersIT.

Let's create some sample data

One ordinary table. Six sales across three regions.

Sample data
-- Sample data for this post.
USE DATABASE DEMO_DB;
USE SCHEMA PUBLIC;

CREATE OR REPLACE TABLE SALES_T108 (
    SALE_ID INT,
    REGION  VARCHAR(20),
    AMOUNT  NUMBER(10,2)
);

INSERT INTO SALES_T108 (SALE_ID, REGION, AMOUNT)
SELECT 1, 'north', 100.00
UNION ALL SELECT 2, 'north', 250.00
UNION ALL SELECT 3, 'south',  75.50
UNION ALL SELECT 4, 'south', 310.00
UNION ALL SELECT 5, 'east',   40.00
UNION ALL SELECT 6, 'east',   60.00;
Query
SELECT SALE_ID, REGION, AMOUNT FROM SALES_T108 ORDER BY SALE_ID;
Result6 rows · 3 columns
SALE_IDREGIONAMOUNT
1north100.00
2north250.00
3south75.50
4south310.00
5east40.00
6east60.00

Nothing clever here. This is the table everything else in the post reads from.

Now let's put a view over it

A view is the word VIEW, a name, and a query. Let's save the totals per region.

Query
CREATE OR REPLACE VIEW REGION_TOTALS_T108 AS
SELECT REGION,
       COUNT(*)    AS SALES,
       SUM(AMOUNT) AS TOTAL
FROM SALES_T108
GROUP BY REGION;
Result1 row · 1 column
status
View REGION_TOTALS_T108 successfully created.

Now let's read it exactly as though it were a table.

Query
SELECT REGION, SALES, TOTAL FROM REGION_TOTALS_T108 ORDER BY REGION;
Result3 rows · 3 columns
REGIONSALESTOTAL
east2100.00
north2350.00
south2385.50

Three rows, one per region, which is exactly what the query inside the view says. Reading a view looks like reading a table, and that is the point of one.

Let's ask the catalog what each of those two objects is

Both of them appear in INFORMATION_SCHEMA.TABLES, so we can ask about them side by side.

Query
SELECT TABLE_NAME, TABLE_TYPE, ROW_COUNT, BYTES
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'PUBLIC'
  AND TABLE_NAME IN ('SALES_T108', 'REGION_TOTALS_T108')
ORDER BY TABLE_NAME;
Result2 rows · 4 columns
TABLE_NAMETABLE_TYPEROW_COUNTBYTES
REGION_TOTALS_T108VIEWNULLNULL
SALES_T108BASE TABLE61024

The table is a BASE TABLE with a row count and a size in bytes. The view is a VIEW, and its row count and its bytes are both NULL.

Those are real NULLs, which the grid above shows differently from an empty cell. They are not zero either. There is no answer to give, because a view does not hold rows and does not occupy bytes.

That is the whole idea, written down in the catalog.

So what happens when the base table changes?

Let's add one more sale to the table. A big one, in the north.

Query
INSERT INTO SALES_T108 (SALE_ID, REGION, AMOUNT) SELECT 7, 'north', 1000.00;
Result1 row · 1 column
number of rows inserted
1

Now let's ask the view again. We are not going to refresh anything, or rebuild anything, or wait.

Query
SELECT REGION, SALES, TOTAL FROM REGION_TOTALS_T108 ORDER BY REGION;
Result3 rows · 3 columns
REGIONSALESTOTAL
east2100.00
north31350.00
south2385.50

North has a third sale now and a much bigger total. The other two regions are untouched.

There is nothing to refresh, because there is nothing held. The query ran again and the table it reads had changed.

Can we write through a view?

Let's try inserting a row into the view.

Query
INSERT INTO REGION_TOTALS_T108 (REGION, SALES, TOTAL) SELECT 'west', 1, 10.00;
Query failed — Snowflake returned an error
001759 (42601): SQL compilation error:
INSERT statement's target must be a table

001759, and the message is blunt: an INSERT statement's target must be a table.

That view aggregates though, so it would be fair to say it is the GROUP BY being refused rather than the view. Let's take that excuse away and build the simplest view there is. One table, a WHERE clause, no grouping at all.

Query
CREATE OR REPLACE VIEW BIG_SALES_T108 AS
SELECT SALE_ID, REGION, AMOUNT
FROM SALES_T108
WHERE AMOUNT >= 100;
Result1 row · 1 column
status
View BIG_SALES_T108 successfully created.
Query
SELECT SALE_ID, REGION, AMOUNT FROM BIG_SALES_T108 ORDER BY SALE_ID;
Result4 rows · 3 columns
SALE_IDREGIONAMOUNT
1north100.00
2north250.00
4south310.00
7north1000.00

Four rows, and every one of them is a row of the base table. There is nothing in this view for an insert to be confused by.

Query
INSERT INTO BIG_SALES_T108 (SALE_ID, REGION, AMOUNT) SELECT 8, 'west', 500.00;
Query failed — Snowflake returned an error
001759 (42601): SQL compilation error:
INSERT statement's target must be a table

The same refusal and the same error code. And an update?

Query
UPDATE BIG_SALES_T108 SET AMOUNT = 0 WHERE SALE_ID = 1;
Query failed — Snowflake returned an error
001759 (42601): SQL compilation error:
UPDATE statement's target must be a table

Refused as well. Views in Snowflake are read only. All of them, not just the complicated ones.

Now let's write one with SELECT * and ask what Snowflake stored

This is the part of the post that matters, and it starts with something that looks harmless.

Query
CREATE OR REPLACE VIEW EVERYTHING_T108 AS
SELECT * FROM SALES_T108;
Result1 row · 1 column
status
View EVERYTHING_T108 successfully created.
Query
SELECT * FROM EVERYTHING_T108 ORDER BY SALE_ID;
Result7 rows · 3 columns
SALE_IDREGIONAMOUNT
1north100.00
2north250.00
3south75.50
4south310.00
5east40.00
6east60.00
7north1000.00

Every row and every column of the table. Exactly what you would expect.

Now let's ask Snowflake what it actually saved when we created that view.

Query
SELECT GET_DDL('VIEW', 'EVERYTHING_T108') AS THE_STORED_DEFINITION;
Result1 row · 1 column
THE_STORED_DEFINITION
create or replace view EVERYTHING_T108( SALE_ID, REGION, AMOUNT ) as SELECT * FROM SALES_T108;

Read that carefully. There is no star in it. The stored definition names the three columns, and the SELECT * is still in the body underneath them.

Snowflake expanded the star the moment the view was saved, and wrote the column list into the object. It is frozen there.

Query
SELECT ORDINAL_POSITION, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'EVERYTHING_T108'
ORDER BY ORDINAL_POSITION;
Result3 rows · 2 columns
ORDINAL_POSITIONCOLUMN_NAME
1SALE_ID
2REGION
3AMOUNT

And the catalog agrees. The view declares three columns, by name, in order.

Now let's add a column to the table underneath

So the view declares three columns. Let's give the table a fourth.

Query
ALTER TABLE SALES_T108 ADD COLUMN CHANNEL VARCHAR(10) DEFAULT 'retail';
Result1 row · 1 column
status
Statement executed successfully.
Query
SELECT SALE_ID, REGION, AMOUNT, CHANNEL FROM SALES_T108 ORDER BY SALE_ID;
Result7 rows · 4 columns
SALE_IDREGIONAMOUNTCHANNEL
1north100.00retail
2north250.00retail
3south75.50retail
4south310.00retail
5east40.00retail
6east60.00retail
7north1000.00retail

The table has its new column and every existing row has the default in it. That part went fine.

Query
SELECT (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
        WHERE TABLE_NAME = 'SALES_T108')      AS COLUMNS_IN_THE_TABLE,
       (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
        WHERE TABLE_NAME = 'EVERYTHING_T108') AS COLUMNS_IN_THE_VIEW;
Result1 row · 2 columns
COLUMNS_IN_THE_TABLECOLUMNS_IN_THE_VIEW
43

Four columns in the table, three in the view. Now let's ask the view for the new column.

Query
SELECT SALE_ID, CHANNEL FROM EVERYTHING_T108 ORDER BY SALE_ID;
Query failed — Snowflake returned an error
002057 (42601): SQL compilation error:
View definition for 'DEMO_DB.PUBLIC.EVERYTHING_T108' declared 3 column(s), but view query produces 4 column(s).

002057. Read the message, because it is not the one I expected: “declared 3 column(s), but view query produces 4 column(s)”.

It is not telling us the column is unknown. It is telling us the view no longer agrees with itself. The declaration says one thing and the body says another, and Snowflake will not guess which one we meant.

Not stale. Refused.

Let's fix it the only way there is

There is no repair command for this. The fix is to save the view again, which expands the star again, over the table as it is now.

Query
CREATE OR REPLACE VIEW EVERYTHING_T108 AS
SELECT * FROM SALES_T108;
Result1 row · 1 column
status
View EVERYTHING_T108 successfully created.
Query
SELECT * FROM EVERYTHING_T108 ORDER BY SALE_ID;
Result7 rows · 4 columns
SALE_IDREGIONAMOUNTCHANNEL
1north100.00retail
2north250.00retail
3south75.50retail
4south310.00retail
5east40.00retail
6east60.00retail
7north1000.00retail

Four columns, and it works. Exactly the same statement we wrote the first time, with a different result frozen into it.

Which is worth sitting with for a second. The text of your view definition does not tell you what the view declares. The day you last ran it does.

Now let's do it again with a named-column view beside it

The refusal above is the finding of this episode, and the first run of the demo nearly lost it. I expected the view to go quietly stale rather than refuse, so the query went in unmarked, and the capture harness of that evening stored nothing for an unmarked failure. The harness was fixed the same night, and a follow-up file asked the question again with the right marker and added the comparison the first file never made. That is why the objects below carry a B on the end of their names.

Two views over one table. The first written SELECT *, the lazy one.

Setup for the follow-up run
-- The follow-up run, on names of its own.
CREATE OR REPLACE TABLE SALES_T108B (
    SALE_ID INT,
    REGION  VARCHAR(20),
    AMOUNT  NUMBER(10,2)
);

CREATE OR REPLACE VIEW STAR_VIEW_T108B AS
SELECT * FROM SALES_T108B;
Query
INSERT INTO SALES_T108B (SALE_ID, REGION, AMOUNT) SELECT 1, 'north', 100.00
UNION ALL SELECT 2, 'north', 250.00
UNION ALL SELECT 3, 'south',  75.50
UNION ALL SELECT 4, 'east',   40.00;
Result1 row · 1 column
number of rows inserted
4

And the second naming its three columns, which is the only difference between them.

Query
CREATE OR REPLACE VIEW NAMED_VIEW_T108B AS
SELECT SALE_ID, REGION, AMOUNT FROM SALES_T108B;
Result1 row · 1 column
status
View NAMED_VIEW_T108B successfully created.
Query
SELECT * FROM STAR_VIEW_T108B ORDER BY SALE_ID;
Result4 rows · 3 columns
SALE_IDREGIONAMOUNT
1north100.00
2north250.00
3south75.50
4east40.00
Query
SELECT SALE_ID, REGION, AMOUNT FROM NAMED_VIEW_T108B ORDER BY SALE_ID;
Result4 rows · 3 columns
SALE_IDREGIONAMOUNT
1north100.00
2north250.00
3south75.50
4east40.00

Identical results. You could not tell them apart from the output.

Query
SELECT (SELECT COUNT(*) FROM STAR_VIEW_T108B)  AS ROWS_FROM_THE_STAR_VIEW,
       (SELECT COUNT(*) FROM NAMED_VIEW_T108B) AS ROWS_FROM_THE_NAMED_VIEW;
Result1 row · 2 columns
ROWS_FROM_THE_STAR_VIEWROWS_FROM_THE_NAMED_VIEW
44

Let's ask what Snowflake stored for the star one.

Query
SELECT GET_DDL('VIEW', 'STAR_VIEW_T108B') AS THE_STORED_DEFINITION;
Result1 row · 1 column
THE_STORED_DEFINITION
create or replace view STAR_VIEW_T108B( SALE_ID, REGION, AMOUNT ) as SELECT * FROM SALES_T108B;
Result3 rows · 2 columns
ORDINAL_POSITIONCOLUMN_NAME
1SALE_ID
2REGION
3AMOUNT

The same thing as before. The star is gone and three column names are written into the object.

Now let's add the fourth column to this table too.

Query
ALTER TABLE SALES_T108B ADD COLUMN CHANNEL VARCHAR(10) DEFAULT 'retail';
Query
SELECT (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
        WHERE TABLE_NAME = 'SALES_T108B')     AS COLUMNS_IN_THE_TABLE,
       (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
        WHERE TABLE_NAME = 'STAR_VIEW_T108B') AS COLUMNS_THE_VIEW_DECLARES;
Result1 row · 2 columns
COLUMNS_IN_THE_TABLECOLUMNS_THE_VIEW_DECLARES
43

Four in the table, three declared by the view. Now let's ask the star view for everything.

Query
SELECT * FROM STAR_VIEW_T108B ORDER BY SALE_ID;
Query failed — Snowflake returned an error
002057 (42601): SQL compilation error:
View definition for 'DEMO_DB.PUBLIC.STAR_VIEW_T108B' declared 3 column(s), but view query produces 4 column(s).

Refused, same code, same sentence. And it is worse than it looks. Let's ask it for only the three columns it does declare.

Query
SELECT SALE_ID, REGION, AMOUNT FROM STAR_VIEW_T108B ORDER BY SALE_ID;
Query failed — Snowflake returned an error
002057 (42601): SQL compilation error:
View definition for 'DEMO_DB.PUBLIC.STAR_VIEW_T108B' declared 3 column(s), but view query produces 4 column(s).

The same error. So let's not ask for any columns at all and just count the rows.

Query
SELECT COUNT(*) AS ROWS_PRESENT FROM STAR_VIEW_T108B;
Query failed — Snowflake returned an error
002057 (42601): SQL compilation error:
View definition for 'DEMO_DB.PUBLIC.STAR_VIEW_T108B' declared 3 column(s), but view query produces 4 column(s).

The same error again. The view is unusable entirely, for every query you can put to it, because a column was added to a table it does not even mention by name.

And the one that spelled its columns out?

Query
SELECT SALE_ID, REGION, AMOUNT FROM NAMED_VIEW_T108B ORDER BY SALE_ID;
Result4 rows · 3 columns
SALE_IDREGIONAMOUNT
1north100.00
2north250.00
3south75.50
4east40.00

Four rows, completely fine. The new column is not in its definition, it was never in its definition, and nothing about it changed.

Query
SELECT (SELECT COUNT(*) FROM NAMED_VIEW_T108B) AS ROWS_FROM_THE_NAMED_VIEW,
       (SELECT COUNT(*) FROM INFORMATION_SCHEMA.VIEWS
        WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'STAR_VIEW_T108B')
                                               AS STAR_VIEW_STILL_IN_CATALOG,
       (SELECT COUNT(*) FROM INFORMATION_SCHEMA.VIEWS
        WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'NAMED_VIEW_T108B')
                                               AS NAMED_VIEW_STILL_IN_CATALOG;
Result1 row · 3 columns
ROWS_FROM_THE_NAMED_VIEWSTAR_VIEW_STILL_IN_CATALOGNAMED_VIEW_STILL_IN_CATALOG
411

One keyword was the whole difference. And both views are still listed in the catalog.

Can the catalog tell us which one is broken?

That last grid counted both views as present, which is already the uncomfortable part. Let's try to ask the catalog the question directly.

Query
SELECT TABLE_NAME, TABLE_TYPE, IS_SECURE
FROM INFORMATION_SCHEMA.VIEWS
WHERE TABLE_SCHEMA = 'PUBLIC' AND RIGHT(TABLE_NAME, 6) = '_T108B'
ORDER BY TABLE_NAME;
Query failed — Snowflake returned an error
000904 (42000): SQL compilation error: error line 1 at position 19
invalid identifier 'TABLE_TYPE'

That one is my mistake rather than Snowflake's. INFORMATION_SCHEMA.VIEWS has no TABLE_TYPE column, so the query never ran. I am leaving it on the page because the error is useful: if you go looking for TABLE_TYPE, it lives in INFORMATION_SCHEMA.TABLES, not in the views view.

The question still has an answer, and the grid above the refusal is where it is. Both views counted as present, one of them answers every query and the other answers none, and there is no column anywhere in the catalog saying which is which.

The dangerous part

A broken view looks exactly like a working one until somebody reads it. Nothing lists it as invalid, nothing warns you when you change the table, and the ALTER TABLE that broke it succeeded quietly.

Does it break the other way round too?

Let's save the star view again, so it declares four columns, and then take a column away instead of adding one.

Query
CREATE OR REPLACE VIEW STAR_VIEW_T108B AS
SELECT * FROM SALES_T108B;
Result1 row · 1 column
status
View STAR_VIEW_T108B successfully created.
Query
SELECT * FROM STAR_VIEW_T108B ORDER BY SALE_ID;
Result4 rows · 4 columns
SALE_IDREGIONAMOUNTCHANNEL
1north100.00retail
2north250.00retail
3south75.50retail
4east40.00retail

Working again, with four columns declared. Now let's drop that column from the table.

Query
ALTER TABLE SALES_T108B DROP COLUMN CHANNEL;
Result1 row · 1 column
status
Statement executed successfully.
Query
SELECT * FROM STAR_VIEW_T108B ORDER BY SALE_ID;
Query failed — Snowflake returned an error
002057 (42601): SQL compilation error:
View definition for 'DEMO_DB.PUBLIC.STAR_VIEW_T108B' declared 4 column(s), but view query produces 3 column(s).

Refused, and read the numbers in the message: declared four, produces three. The same complaint with the two numbers the other way round.

So either direction breaks it. Adding a column breaks it and removing one breaks it, and in both cases the view is the thing that stops working while the table is perfectly happy.

Query
SELECT SALE_ID, REGION, AMOUNT FROM NAMED_VIEW_T108B ORDER BY SALE_ID;
Result4 rows · 3 columns
SALE_IDREGIONAMOUNT
1north100.00
2north250.00
3south75.50
4east40.00

The named view carried on without a murmur, again. It never mentioned that column, so dropping it was none of its business.

What to actually do about this

SELECT * in a worksheet is fine, and we said so back in Episode 1. SELECT * inside anything you save is a different thing, because the star is resolved once and kept.
Name your columns in a view. It is more typing on the day you write it and it is the difference between a view that ignores a schema change and a view that refuses every query until somebody rebuilds it.

Now let's comment and rename a view

A view takes a comment and a rename the same way a table does. Let's comment the totals view.

Query
ALTER VIEW REGION_TOTALS_T108 SET COMMENT = 'totals per region - episode 108';
Result1 row · 1 column
status
Statement executed successfully.
Query
SELECT TABLE_NAME, COMMENT, IS_SECURE
FROM INFORMATION_SCHEMA.VIEWS
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'REGION_TOTALS_T108';
Result1 row · 3 columns
TABLE_NAMECOMMENTIS_SECURE
REGION_TOTALS_T108totals per region - episode 108NO

The comment is recorded, and there is an IS_SECURE column next to it reading NO. Hold on to that column, we are coming back to it.

Now let's rename the view.

Query
ALTER VIEW REGION_TOTALS_T108 RENAME TO TOTALS_BY_REGION_T108;
Result1 row · 1 column
status
Statement executed successfully.
Query
SELECT REGION, TOTAL FROM REGION_TOTALS_T108 ORDER BY REGION;
Query failed — Snowflake returned an error
002003 (42S02): SQL compilation error:
Object 'REGION_TOTALS_T108' does not exist or not authorized. Your primary role ACCOUNTADMIN or one of your secondary roles must have at least one privilege granted on TABLE REGION_TOTALS_T108.

The old name is gone, with the same 002003 a dropped table gives. A rename is not a copy.

Query
SELECT REGION, SALES, TOTAL FROM TOTALS_BY_REGION_T108 ORDER BY REGION;
Result3 rows · 3 columns
REGIONSALESTOTAL
east2100.00
north31350.00
south2385.50

And the new name gives the same three rows.

Query
SELECT TABLE_NAME, COMMENT
FROM INFORMATION_SCHEMA.VIEWS
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'TOTALS_BY_REGION_T108';
Result1 row · 2 columns
TABLE_NAMECOMMENT
TOTALS_BY_REGION_T108totals per region - episode 108

The comment came with it. Comments follow the object, not the name.

What does SET SECURE actually hide?

This is the part I did not know the answer to. First, let's read the definition of an ordinary view out of the catalog.

Query
SELECT TABLE_NAME, VIEW_DEFINITION
FROM INFORMATION_SCHEMA.VIEWS
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'BIG_SALES_T108';
Result1 row · 2 columns
TABLE_NAMEVIEW_DEFINITION
BIG_SALES_T108CREATE OR REPLACE VIEW BIG_SALES_T108 AS SELECT SALE_ID, REGION, AMOUNT FROM SALES_T108 WHERE AMOUNT >= 100

There it is, the whole SELECT statement in plain text. Anybody who can see the catalog can read it.

Now let's make it secure.

Query
ALTER VIEW BIG_SALES_T108 SET SECURE;
Result1 row · 1 column
status
Statement executed successfully.
Query
SELECT TABLE_NAME, IS_SECURE
FROM INFORMATION_SCHEMA.VIEWS
WHERE TABLE_SCHEMA = 'PUBLIC'
  AND TABLE_NAME IN ('BIG_SALES_T108', 'TOTALS_BY_REGION_T108')
ORDER BY TABLE_NAME;
Result2 rows · 2 columns
TABLE_NAMEIS_SECURE
BIG_SALES_T108YES
TOTALS_BY_REGION_T108NO

The flag flipped to YES for the one we changed and the other view is still NO. So the statement did something. The question is what.

Query
SELECT TABLE_NAME, VIEW_DEFINITION
FROM INFORMATION_SCHEMA.VIEWS
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'BIG_SALES_T108';
Result1 row · 2 columns
TABLE_NAMEVIEW_DEFINITION
BIG_SALES_T108CREATE OR REPLACE VIEW BIG_SALES_T108 AS SELECT SALE_ID, REGION, AMOUNT FROM SALES_T108 WHERE AMOUNT >= 100

Still completely readable. The whole definition, exactly as before.

Query
SELECT GET_DDL('VIEW', 'BIG_SALES_T108') AS THE_STORED_DEFINITION;
Result1 row · 1 column
THE_STORED_DEFINITION
create or replace secure view BIG_SALES_T108( SALE_ID, REGION, AMOUNT ) as SELECT SALE_ID, REGION, AMOUNT FROM SALES_T108 WHERE AMOUNT >= 100;

And GET_DDL prints the word secure in the statement, with the body right underneath it.

The reason is ownership. This session owns the view. SECURE is about what other roles can see, and the owner is not another role.

Who secure is for

This is worth knowing before you assume a secure view is a black box. Run these two queries as yourself and the definition is right there, which makes it very easy to conclude that SET SECURE did nothing. To see what it does you have to ask from a role that does not own the view.

Query
SELECT SALE_ID, REGION, AMOUNT FROM BIG_SALES_T108 ORDER BY SALE_ID;
Result4 rows · 3 columns
SALE_IDREGIONAMOUNT
1north100.00
2north250.00
4south310.00
7north1000.00

Reading it still works, of course, with the same four rows.

Query
ALTER VIEW BIG_SALES_T108 UNSET SECURE;
Result1 row · 1 column
status
Statement executed successfully.
Query
SELECT TABLE_NAME, IS_SECURE
FROM INFORMATION_SCHEMA.VIEWS
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'BIG_SALES_T108';
Result1 row · 2 columns
TABLE_NAMEIS_SECURE
BIG_SALES_T108NO

And the flag goes back. It is one property, on and off.

Now let's build a view on another view

Views stack. Let's take the totals per region and put a second view on top of it that keeps only the top region.

Query
CREATE OR REPLACE VIEW TOP_REGION_T108 AS
SELECT REGION, TOTAL
FROM TOTALS_BY_REGION_T108
QUALIFY ROW_NUMBER() OVER (ORDER BY TOTAL DESC) = 1;
Result1 row · 1 column
status
View TOP_REGION_T108 successfully created.
Query
SELECT REGION, TOTAL FROM TOTALS_BY_REGION_T108 ORDER BY TOTAL DESC;
Result3 rows · 2 columns
REGIONTOTAL
north1350.00
south385.50
east100.00

Those are the three regions in order, biggest total first.

Query
SELECT REGION, TOTAL FROM TOP_REGION_T108;
Result1 row · 2 columns
REGIONTOTAL
north1350.00

North, with the biggest total. Two levels of query and one answer.

A correction to the video

If you are reading this after watching the video, the narration calls the top region west at this point. That is wrong, and the grid above is why: there is no west row in the table yet. West turns up later in this post, when we recreate the table from scratch. The top region here is north.

Now let's drop the view in the middle.

Query
DROP VIEW TOTALS_BY_REGION_T108;
Result1 row · 1 column
status
TOTALS_BY_REGION_T108 successfully dropped.

And ask the one built on top of it.

Query
SELECT REGION, TOTAL FROM TOP_REGION_T108;
Query failed — Snowflake returned an error
002037 (42601): SQL compilation error:
Failure during expansion of view 'TOP_REGION_T108': SQL compilation error:
Object 'DEMO_DB.PUBLIC.TOTALS_BY_REGION_T108' does not exist or not authorized. Your primary role ACCOUNTADMIN or one of your secondary roles must have at least one privilege granted on TABLE DEMO_DB.PUBLIC.TOTALS_BY_REGION_T108.

002037, failure during expansion of view. The message quotes the object it could not resolve, which is more help than most errors give you.

Query
SELECT TABLE_NAME, TABLE_TYPE
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'TOP_REGION_T108';
Result1 row · 2 columns
TABLE_NAMETABLE_TYPE
TOP_REGION_T108VIEW

And it is still in the catalog, still listed as a view. Broken, listed, and looking exactly like the others.

What happens when we drop the base table?

Something harsher. Let's drop the table everything reads from.

Query
DROP TABLE SALES_T108;
Result1 row · 1 column
status
SALES_T108 successfully dropped.
Query
SELECT TABLE_NAME, TABLE_TYPE
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'PUBLIC'
  AND TABLE_NAME IN ('SALES_T108', 'EVERYTHING_T108', 'BIG_SALES_T108')
ORDER BY TABLE_NAME;
Result2 rows · 2 columns
TABLE_NAMETABLE_TYPE
BIG_SALES_T108VIEW
EVERYTHING_T108VIEW

The table is gone from that list and both views are still there. Neither of them noticed.

Query
SELECT SALE_ID, REGION FROM BIG_SALES_T108 ORDER BY SALE_ID;
Query failed — Snowflake returned an error
002037 (42601): SQL compilation error:
Failure during expansion of view 'BIG_SALES_T108': SQL compilation error:
Object 'DEMO_DB.PUBLIC.SALES_T108' does not exist or not authorized. Your primary role ACCOUNTADMIN or one of your secondary roles must have at least one privilege granted on TABLE DEMO_DB.PUBLIC.SALES_T108.

The same shape of error as the dropped view gave us: failure during expansion, naming the table it could not find.

Now let's put the table back. Same columns, two new rows, nothing else.

Query
CREATE OR REPLACE TABLE SALES_T108 (
    SALE_ID  INT,
    REGION   VARCHAR(20),
    AMOUNT   NUMBER(10,2),
    CHANNEL  VARCHAR(10) DEFAULT 'retail'
);
Result1 row · 1 column
status
Table SALES_T108 successfully created.
Query
INSERT INTO SALES_T108 (SALE_ID, REGION, AMOUNT) SELECT 10, 'west', 500.00
UNION ALL SELECT 11, 'west', 20.00;
Result1 row · 1 column
number of rows inserted
2
Query
SELECT SALE_ID, REGION, AMOUNT FROM BIG_SALES_T108 ORDER BY SALE_ID;
Result1 row · 3 columns
SALE_IDREGIONAMOUNT
10west500.00

The view works again, on its own, and nobody rebuilt it. Only one of the two new rows comes back, because that view filters on the amount and the smaller row does not qualify.

Query
SELECT * FROM EVERYTHING_T108 ORDER BY SALE_ID;
Result2 rows · 4 columns
SALE_IDREGIONAMOUNTCHANNEL
10west500.00retail
11west20.00retail

And the star view too, with four columns, because we saved it again earlier and the recreated table has the same four.

This is the sentence that explains the whole post. A view resolves its tables by name, every single time it runs. That is why it healed here, and it is why it broke in the first place.

One last thing: what the catalog shows at the end

Let's ask SHOW VIEWS what is in this schema before we tidy up.

Query
SHOW VIEWS IN SCHEMA PUBLIC;
Result3 rows · 12 columns · scroll →
Scroll sideways for the text and is_secure columns.
created_onnamereserveddatabase_nameschema_nameownercommenttextis_secureis_materializedowner_role_typechange_tracking
2026-09-28 18:24:42.426000-07:00BIG_SALES_T108DEMO_DBPUBLICACCOUNTADMINCREATE OR REPLACE VIEW BIG_SALES_T108 AS SELECT SALE_ID, REGION, AMOUNT FROM SALES_T108 WHERE AMOUNT >= 100falsefalseROLEOFF
2026-09-28 18:24:46.695000-07:00EVERYTHING_T108DEMO_DBPUBLICACCOUNTADMINCREATE OR REPLACE VIEW EVERYTHING_T108 AS SELECT * FROM SALES_T108falsefalseROLEOFF
2026-09-28 18:24:51.847000-07:00TOP_REGION_T108DEMO_DBPUBLICACCOUNTADMINCREATE OR REPLACE VIEW TOP_REGION_T108 AS SELECT REGION, TOTAL FROM TOTALS_BY_REGION_T108 QUALIFY ROW_NUMBER() OVER (ORDER BY TOTAL DESC) = 1falsefalseROLEOFF

The whole definition of each view is in the text column, which is the quickest way to see what a view really is. Have a look at the comment cells too: they are empty, which the grid marks differently from a NULL. The one view we commented was renamed and then dropped, so it is not in this list.

Query
SELECT COUNT(*) AS VIEWS_IN_THIS_SCHEMA_FROM_THIS_FILE
FROM INFORMATION_SCHEMA.VIEWS
WHERE TABLE_SCHEMA = 'PUBLIC' AND RIGHT(TABLE_NAME, 5) = '_T108';
Result1 row · 1 column
VIEWS_IN_THIS_SCHEMA_FROM_THIS_FILE
3

Counted from INFORMATION_SCHEMA.VIEWS, which agrees with SHOW VIEWS about how many there are.

Finally, let's clean up after ourselves.

Cleanup
-- Put the schema back the way we found it.
DROP VIEW  IF EXISTS TOP_REGION_T108;
DROP VIEW  IF EXISTS EVERYTHING_T108;
DROP VIEW  IF EXISTS BIG_SALES_T108;
DROP VIEW  IF EXISTS NAMED_VIEW_T108B;
DROP VIEW  IF EXISTS STAR_VIEW_T108B;
DROP TABLE IF EXISTS SALES_T108;
DROP TABLE IF EXISTS SALES_T108B;
Query
DROP VIEW IF EXISTS TOP_REGION_T108;
Result1 row · 1 column
status
TOP_REGION_T108 successfully dropped.
Result1 row · 1 column
status
EVERYTHING_T108 successfully dropped.
Result1 row · 1 column
status
BIG_SALES_T108 successfully dropped.
Result1 row · 1 column
status
NAMED_VIEW_T108B successfully dropped.
Query
DROP TABLE IF EXISTS SALES_T108;
Result1 row · 1 column
status
SALES_T108 successfully dropped.
Query
SELECT CURRENT_DATABASE()                 AS THE_DATABASE,
       CURRENT_SCHEMA()                   AS THE_SCHEMA,
       (SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES
        WHERE RIGHT(TABLE_NAME, 5) = '_T108') AS PROBE_OBJECTS_LEFT,
       (SELECT COUNT(*) FROM CUSTOMERS)   AS DEMO_DB_IS_READABLE;
Result1 row · 4 columns
THE_DATABASETHE_SCHEMAPROBE_OBJECTS_LEFTDEMO_DB_IS_READABLE
DEMO_DBPUBLIC010
Result1 row · 4 columns
THE_DATABASETHE_SCHEMAPROBE_OBJECTS_LEFTDEMO_DB_IS_READABLE
DEMO_DBPUBLIC010

Nothing left from either run, and the demo database still reads. Back where we started.

Key points

  • A view is a saved query. The catalog gives it a ROW_COUNT and a BYTES of NULL, because it holds nothing.
  • Change the base table and the view answers differently at once. There is nothing to refresh.
  • You cannot INSERT or UPDATE through a view, and that includes a single-table view with no aggregate. Both get 001759.
  • SELECT * inside a view is expanded when you save it. GET_DDL shows the column list and no star.
  • Add a column to the table and the star view does not go stale, it refuses — declared 3 column(s), but view query produces 4 column(s).
  • It refuses every query, including the three columns it does declare and a bare COUNT(*).
  • Drop a column instead and it breaks the same way with the numbers reversed: declared four, produces three.
  • A view that named its columns survived both changes untouched.
  • The broken view is still listed in the catalog and nothing says it is broken.
  • ALTER VIEW handles SET COMMENT and RENAME TO, and the comment follows the object through the rename.
  • SET SECURE flips IS_SECURE to YES and does not hide the definition from the owner. GET_DDL prints the body right under the word secure.
  • Drop the view underneath a view, or the table underneath both, and you get 002037 failure during expansion — while the views stay in the catalog looking healthy.
  • Put the table back and the views work again by themselves, because a view resolves its tables by name every time it runs.
Next in this series

Episode 109 — materialized views. The object that does store its results, and refuses most of the queries a view takes without complaint. The list of refusals is the specification.

Every query and every result grid on this page was captured from a live Snowflake account (version 10.34.101) running the demo script for this episode. The episode ran twice and the post says where it switches: the named-versus-star comparison is on SALES_T108B from a follow-up run, whose setup appears as plain code with no grid. One query on this page was written wrong and failed, and the text beside it says so rather than hiding it. The spoken narration names the wrong region in the stacked-view section, and the note there says which region the grid gives. Every count in the text is read off one of the grids on this page. Nothing is timed and no performance claims are made anywhere in this series. Behaviour follows the Snowflake CREATE VIEW documentation.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.