Snowflake SQL Tutorial 99 - Schemas, and Dropping the PUBLIC Schema - Same Query, Different Rows

Snowflake SQL · Episode 99

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

A schema is the box a table lives in. A database holds schemas, a schema holds tables, and when you write a table name without saying which schema it is in, Snowflake decides for you. It uses whichever schema your session happens to be pointing at.

That last sentence is the whole post. We will build two schemas with a table of the same name in each, run one query against them twice without changing a character of it, and get different rows back both times. Then we will rename a schema, swap two schemas over in one statement, undrop a schema, and finish with a question worth knowing the answer to: can you drop the PUBLIC schema?

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

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

Let's see which schemas we already have

Before we make anything, let's ask the database what is in it. INFORMATION_SCHEMA.SCHEMATA is a view that lists the schemas of the database you are in.

Query
SELECT SCHEMA_NAME, SCHEMA_OWNER, IS_TRANSIENT, RETENTION_TIME, COMMENT
FROM INFORMATION_SCHEMA.SCHEMATA
ORDER BY SCHEMA_NAME;
Result2 rows · 5 columns
SCHEMA_NAMESCHEMA_OWNERIS_TRANSIENTRETENTION_TIMECOMMENT
INFORMATION_SCHEMANULLNO1Views describing the contents of schemas in this database
PUBLICACCOUNTADMINNO1NULL

Two schemas, and we did not create either of them. PUBLIC is the one we have been working in all course. INFORMATION_SCHEMA is the catalog itself, which is how we are able to run this query at all.

Have a look at the owner column. PUBLIC is owned by ACCOUNTADMIN. INFORMATION_SCHEMA has no owner at all, and the grid prints that as a real NULL. That is a hint about what these two are. One of them is ours and one of them is Snowflake's.

Neither is transient and both keep one day of Time Travel. Now let's check where the session is pointing.

Query
SELECT CURRENT_DATABASE() AS THE_DATABASE,
       CURRENT_SCHEMA()   AS THE_SCHEMA;
Result1 row · 2 columns
THE_DATABASETHE_SCHEMA
DEMO_DBPUBLIC

DEMO_DB and PUBLIC. Remember those two words, because everything that follows is about what they do to an unqualified table name.

Let's create some sample data

We need two schemas, and a table with the same name in both of them. Here is the whole script in one piece. Copy it into a worksheet and run it, and then we will watch Snowflake answer it one statement at a time.

Sample data
-- Sample data for this post.
-- Two schemas, and a table with the SAME NAME in both of them.
USE DATABASE DEMO_DB;
USE SCHEMA PUBLIC;

CREATE OR REPLACE SCHEMA STAGING_T99   COMMENT = 'episode 99 probe';
CREATE OR REPLACE SCHEMA REPORTING_T99 COMMENT = 'episode 99 probe';

CREATE OR REPLACE TABLE STAGING_T99.ORDERS_T99   (ID INT, STATUS VARCHAR);
CREATE OR REPLACE TABLE REPORTING_T99.ORDERS_T99 (ID INT, STATUS VARCHAR);

INSERT INTO STAGING_T99.ORDERS_T99 (ID, STATUS)
SELECT 1, 'RAW' UNION ALL SELECT 2, 'RAW' UNION ALL SELECT 3, 'RAW';

INSERT INTO REPORTING_T99.ORDERS_T99 (ID, STATUS)
SELECT 99, 'PUBLISHED';

Let's start with the two schemas.

Query
CREATE OR REPLACE SCHEMA STAGING_T99 COMMENT = 'episode 99 probe';
Result1 row · 1 column
status
Schema STAGING_T99 successfully created.
Query
CREATE OR REPLACE SCHEMA REPORTING_T99 COMMENT = 'episode 99 probe';
Result1 row · 1 column
status
Schema REPORTING_T99 successfully created.

Both created, and both carry a comment so we can find them again later. Now, what is inside a schema we just made?

Query
SELECT COUNT(*) AS TABLES_IN_A_NEW_SCHEMA
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'STAGING_T99';
Result1 row · 1 column
TABLES_IN_A_NEW_SCHEMA
0

Nothing. A new schema is an empty box with a name. That is all a schema ever is.

Now let's put a table called ORDERS_T99 in both of them

Here is the part that matters. We are going to create a table called ORDERS_T99 twice, in two different schemas, with different data in each.

Query
CREATE OR REPLACE TABLE STAGING_T99.ORDERS_T99 (ID INT, STATUS VARCHAR);
Result1 row · 1 column
status
Table ORDERS_T99 successfully created.
Query
INSERT INTO STAGING_T99.ORDERS_T99 (ID, STATUS)
SELECT 1, 'RAW' UNION ALL SELECT 2, 'RAW' UNION ALL SELECT 3, 'RAW';
Result1 row · 1 column
number of rows inserted
3

Three rows in the staging copy, and every one of them has a status of RAW. Now the other one.

Query
CREATE OR REPLACE TABLE REPORTING_T99.ORDERS_T99 (ID INT, STATUS VARCHAR);
Result1 row · 1 column
status
Table ORDERS_T99 successfully created.
Query
INSERT INTO REPORTING_T99.ORDERS_T99 (ID, STATUS) SELECT 99, 'PUBLISHED';
Result1 row · 1 column
number of rows inserted
1

One row in the reporting copy, with a status of PUBLISHED.

Nothing warned us that a table of that name already existed. Why would it? They are in different schemas, so they are different tables. Both statements were completely legal.

Now let's run the same query twice

First we point the session at the staging schema.

Query
USE SCHEMA STAGING_T99;
Result1 row · 1 column
status
Statement executed successfully.

Now one query. It asks which schema it resolved against, how many rows it found, and one of the statuses. The table name in it is ORDERS_T99, with no schema in front of it.

Query
SELECT CURRENT_SCHEMA() AS RESOLVED_AGAINST,
       COUNT(*)         AS ROWS_RETURNED,
       MIN(STATUS)      AS A_STATUS
FROM ORDERS_T99;
Result1 row · 3 columns
RESOLVED_AGAINSTROWS_RETURNEDA_STATUS
STAGING_T993RAW

Three rows, status RAW, resolved against the staging schema. Exactly what we put there.

Now one statement, and nothing else changes.

Query
USE SCHEMA REPORTING_T99;
Result1 row · 1 column
status
Statement executed successfully.

And now the identical query. Same text, character for character. We are not retyping it, we are running it again.

Query
SELECT CURRENT_SCHEMA() AS RESOLVED_AGAINST,
       COUNT(*)         AS ROWS_RETURNED,
       MIN(STATUS)      AS A_STATUS
FROM ORDERS_T99;
Result1 row · 3 columns
RESOLVED_AGAINSTROWS_RETURNEDA_STATUS
REPORTING_T991PUBLISHED

One row, status PUBLISHED, resolved against the reporting schema.

Same query text. Same session. Same warehouse. Different answer. The only thing that changed between those two grids is a USE SCHEMA statement, and in real life that statement was typed some minutes earlier and has been forgotten about.

This is the dangerous part

There was no warning either time. Both queries were correct SQL and both returned a correct answer to the question they were actually asked. The question you think you are asking and the question Snowflake receives differ by a word you did not type.

Now let's spell the name out in full

The fix is to say which schema we mean. A fully qualified name is DATABASE.SCHEMA.TABLE, and it does not care where the session is pointing.

Let's ask for both counts in one statement, and have the query tell us where we are standing while it does it.

Query
SELECT (SELECT COUNT(*) FROM DEMO_DB.STAGING_T99.ORDERS_T99)   AS STAGING_ROWS,
       (SELECT COUNT(*) FROM DEMO_DB.REPORTING_T99.ORDERS_T99) AS REPORTING_ROWS,
       CURRENT_SCHEMA()                                        AS AND_WE_ARE_STILL_IN;
Result1 row · 3 columns
STAGING_ROWSREPORTING_ROWSAND_WE_ARE_STILL_IN
31REPORTING_T99

Three from staging and one from reporting, out of a single query, and we are still sitting in the reporting schema while it runs. Nothing about this answer depends on where the session happens to be.

The rule to take away

Unqualified names are fine in a worksheet, where you can see the database and schema in the dropdown next to the query. They are not fine in anything you save — a script, a view, a task, a stored procedure, anything that runs when you are not watching. A file does not carry your session with it.

So why did nothing warn us?

Because there was nothing to warn us about. Let's ask the catalog for every table called ORDERS_T99, wherever it is.

Query
SELECT TABLE_SCHEMA, TABLE_NAME, ROW_COUNT
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'ORDERS_T99'
ORDER BY TABLE_SCHEMA;
Result2 rows · 3 columns
TABLE_SCHEMATABLE_NAMEROW_COUNT
REPORTING_T99ORDERS_T991
STAGING_T99ORDERS_T993

Two rows, one per schema, with their row counts beside them. Two real tables, two different sets of rows, one name. The catalog is not confused and it is not hiding anything. It simply does not have an opinion about which one you meant.

Now let's rename a schema

ALTER SCHEMA ... RENAME TO changes a schema's name. Let's turn STAGING_T99 into LANDING_T99.

Query
ALTER SCHEMA STAGING_T99 RENAME TO LANDING_T99;
Result1 row · 1 column
status
Statement executed successfully.

The obvious question is what happened to the table that was inside it.

Query
SELECT TABLE_SCHEMA, TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'ORDERS_T99'
ORDER BY TABLE_SCHEMA;
Result2 rows · 2 columns
TABLE_SCHEMATABLE_NAME
LANDING_T99ORDERS_T99
REPORTING_T99ORDERS_T99

It went with the schema, and it is still called ORDERS_T99. We renamed the box, not the thing in it.

Now let's ask for the old name on purpose.

Query
SELECT COUNT(*) FROM DEMO_DB.STAGING_T99.ORDERS_T99;
Query failed — Snowflake returned an error
002003 (02000): SQL compilation error:
Schema 'DEMO_DB.STAGING_T99' does not exist or not authorized.

A clean compilation error. Schema DEMO_DB.STAGING_T99 does not exist or not authorized. So a rename is loud. Anything that referenced the old schema name breaks immediately and tells you why, which is the best kind of breakage there is.

While we are altering it, let's also change the comment.

Query
ALTER SCHEMA LANDING_T99 SET COMMENT = 'renamed and re-commented';
Result1 row · 1 column
status
Statement executed successfully.
Query
SELECT SCHEMA_NAME, COMMENT, RETENTION_TIME
FROM INFORMATION_SCHEMA.SCHEMATA
WHERE SCHEMA_NAME IN ('LANDING_T99', 'REPORTING_T99')
ORDER BY SCHEMA_NAME;
Result2 rows · 3 columns
SCHEMA_NAMECOMMENTRETENTION_TIME
LANDING_T99renamed and re-commented1
REPORTING_T99episode 99 probe1

The renamed schema carries the new comment and the other one still carries the comment we created it with. Both keep one day of Time Travel.

Now the interesting one: SWAP WITH

Right now LANDING_T99 holds the three RAW rows and REPORTING_T99 holds the one PUBLISHED row. Let's exchange them in a single statement.

Query
ALTER SCHEMA LANDING_T99 SWAP WITH REPORTING_T99;
Result1 row · 1 column
status
Statement executed successfully.

Now let's look at what is in each name.

Query
SELECT (SELECT COUNT(*) FROM DEMO_DB.LANDING_T99.ORDERS_T99)   AS LANDING_ROWS,
       (SELECT COUNT(*) FROM DEMO_DB.REPORTING_T99.ORDERS_T99) AS REPORTING_ROWS,
       (SELECT MIN(STATUS) FROM DEMO_DB.LANDING_T99.ORDERS_T99)   AS LANDING_STATUS,
       (SELECT MIN(STATUS) FROM DEMO_DB.REPORTING_T99.ORDERS_T99) AS REPORTING_STATUS;
Result1 row · 4 columns
LANDING_ROWSREPORTING_ROWSLANDING_STATUSREPORTING_STATUS
13PUBLISHEDRAW

They have traded places. LANDING_T99 now holds the single PUBLISHED row and REPORTING_T99 holds the three RAW ones.

The names stayed exactly where they were and the contents moved. That is what makes this statement useful. You build a new version of a schema under a working name, swap it into place, and there is never a moment where the name people are querying does not exist.

Can we get a dropped schema back?

Let's drop one and find out.

Query
DROP SCHEMA LANDING_T99;
Result1 row · 1 column
status
LANDING_T99 successfully dropped.
Query
SELECT COUNT(*) FROM DEMO_DB.LANDING_T99.ORDERS_T99;
Query failed — Snowflake returned an error
002003 (02000): SQL compilation error:
Schema 'DEMO_DB.LANDING_T99' does not exist or not authorized.

Gone, and the same clean error we got from the old name after the rename. Now, we know UNDROP TABLE exists and we saw UNDROP DATABASE in Episode 98. Is there an UNDROP SCHEMA?

Query
UNDROP SCHEMA LANDING_T99;
Result1 row · 1 column
status
Schema LANDING_T99 successfully restored.

There is. Schema LANDING_T99 successfully restored. So the UNDROP family covers all three: table, schema and database.

And the table inside it?

Query
SELECT COUNT(*) AS ROWS_RECOVERED FROM DEMO_DB.LANDING_T99.ORDERS_T99;
Result1 row · 1 column
ROWS_RECOVERED
1

The table came back with the schema, holding the row it had when we dropped it.

Why one and not three

If you were expecting three rows there, look back at the swap. LANDING_T99 gave its three RAW rows away and took the single PUBLISHED row in exchange. The restored schema came back exactly as it was at the moment we dropped it, which is the whole point. Nothing was lost in the recovery.

Can we drop the PUBLIC schema?

This is the question this episode exists for, and it is not a question to try out on a database you care about. So let's make a throwaway one.

Query
CREATE OR REPLACE DATABASE PUBLIC_PROBE_T99;
Result1 row · 1 column
status
Database PUBLIC_PROBE_T99 successfully created.

A brand new database. Does it have a PUBLIC schema?

Query
SELECT COUNT(*) AS PUBLIC_EXISTS
FROM PUBLIC_PROBE_T99.INFORMATION_SCHEMA.SCHEMATA
WHERE SCHEMA_NAME = 'PUBLIC';
Result1 row · 1 column
PUBLIC_EXISTS
1

It does, and we never asked for one. Now let's drop it.

Query
DROP SCHEMA PUBLIC_PROBE_T99.PUBLIC;
Result1 row · 1 column
status
PUBLIC successfully dropped.

PUBLIC successfully dropped. No warning, no confirmation, no special treatment. Let's see what is left.

Query
SELECT ARRAY_AGG(SCHEMA_NAME) AS SCHEMAS_LEFT
FROM PUBLIC_PROBE_T99.INFORMATION_SCHEMA.SCHEMATA;
Result1 row · 1 column
SCHEMAS_LEFT
[ "INFORMATION_SCHEMA" ]

One schema left, and it is the catalog. The database now has no PUBLIC schema at all.

So this is worth changing your mind about

“Every database has a PUBLIC schema” is a default, not a guarantee. It is there when the database is created and it can be dropped afterwards like any other schema. Any code that writes a bare DATABASE.PUBLIC.TABLE and assumes the middle part is always available is relying on something nobody promised it.

And can we drop INFORMATION_SCHEMA?

The other schema in that database is the catalog. Let's try the same statement on it.

Query
DROP SCHEMA PUBLIC_PROBE_T99.INFORMATION_SCHEMA;
Query failed — Snowflake returned an error
003001 (42501): SQL access control error:
Insufficient privileges to operate on schema 'INFORMATION_SCHEMA'.

Refused. Read the message slowly though, because it is not the message you would expect: Insufficient privileges to operate on schema INFORMATION_SCHEMA.

That is an access control error, not a rule about what DROP SCHEMA can be pointed at. Snowflake is not saying this cannot be done. It is saying we are not the one who gets to do it — and every schema we have created in this post came back owned by ACCOUNTADMIN, so this is not a small role being told no.

Can we create a database without a PUBLIC schema?

If PUBLIC can be dropped afterwards, the next question is whether we can avoid getting one in the first place. Let's make another database and give it a schema of our own.

Query
CREATE OR REPLACE DATABASE NO_PUBLIC_T99;
Result1 row · 1 column
status
Database NO_PUBLIC_T99 successfully created.

And a schema with WITH MANAGED ACCESS, which is a real option on CREATE SCHEMA and means grants inside it are controlled by the schema owner rather than by whoever owns each object.

Query
CREATE OR REPLACE SCHEMA NO_PUBLIC_T99.LOCKED_T99 WITH MANAGED ACCESS;
Result1 row · 1 column
status
Schema LOCKED_T99 successfully created.

Now let's list what that database actually ended up with. SHOW SCHEMAS gives us a much wider grid than the catalog view did at the top of this post.

SHOW SCHEMAS
SHOW SCHEMAS IN DATABASE NO_PUBLIC_T99;
Result3 rows · 15 columns · scroll →
created_onnameis_defaultis_currentdatabase_nameownercommentoptionsretention_timeowner_role_typeclassification_profile_databaseclassification_profile_schemaclassification_profileobject_visibilityis_nested
2026-09-28 13:01:24.179000-07:00INFORMATION_SCHEMANNNO_PUBLIC_T99Views describing the contents of schemas in this database1NULLNULLNULLNULLfalse
2026-09-28 13:01:23.989000-07:00LOCKED_T99NYNO_PUBLIC_T99ACCOUNTADMINMANAGED ACCESS1ROLENULLNULLNULLNULLfalse
2026-09-28 13:01:23.737000-07:00PUBLICNNNO_PUBLIC_T99ACCOUNTADMIN1ROLENULLNULLNULLNULLfalse

Three schemas, and we only created one of them. There is INFORMATION_SCHEMA, there is our LOCKED_T99 with MANAGED ACCESS sitting in the options column, and there is PUBLIC. It arrived anyway.

So there is no way to create a database that never has a PUBLIC schema. You either keep it or you drop it afterwards.

Empty is not NULL

The empty cells in that grid and the NULL cells are two different things, and the grid marks them differently. SHOW SCHEMAS writes the INFORMATION_SCHEMA owner as an empty string, so the cell is blank and marked empty. The classification columns on the same row hold a real SQL NULL and print the word. Compare that owner cell with the first grid in this post, where INFORMATION_SCHEMA.SCHEMATA reports the same missing owner as a genuine NULL. Two views, one fact, two different ways of saying nothing.

One last thing: let's put everything back

Both probe databases were made to be thrown away, so let's throw them away.

Cleanup
-- Put the database back the way we found it.
DROP DATABASE IF EXISTS PUBLIC_PROBE_T99;
DROP DATABASE IF EXISTS NO_PUBLIC_T99;

USE SCHEMA DEMO_DB.PUBLIC;

DROP SCHEMA IF EXISTS DEMO_DB.LANDING_T99;
DROP SCHEMA IF EXISTS DEMO_DB.REPORTING_T99;
Query
DROP DATABASE IF EXISTS PUBLIC_PROBE_T99;
Result1 row · 1 column
status
PUBLIC_PROBE_T99 successfully dropped.
Query
DROP DATABASE IF EXISTS NO_PUBLIC_T99;
Result1 row · 1 column
status
NO_PUBLIC_T99 successfully dropped.

Then point the session back at the schema we started in, fully qualified, because by now we have earned the habit.

Query
USE SCHEMA DEMO_DB.PUBLIC;
Result1 row · 1 column
status
Statement executed successfully.
Query
DROP SCHEMA IF EXISTS DEMO_DB.LANDING_T99;
Result1 row · 1 column
status
LANDING_T99 successfully dropped.
Query
DROP SCHEMA IF EXISTS DEMO_DB.REPORTING_T99;
Result1 row · 1 column
status
REPORTING_T99 successfully dropped.

And a last check that the database we actually use is exactly as we found it.

Query
SELECT CURRENT_DATABASE()                 AS THE_DATABASE,
       CURRENT_SCHEMA()                   AS THE_SCHEMA,
       (SELECT COUNT(*) FROM EVENTS_JSON) AS DEMO_DB_IS_READABLE,
       (SELECT COUNT(*) FROM INFORMATION_SCHEMA.SCHEMATA
        WHERE SCHEMA_NAME LIKE '%_T99')   AS PROBE_SCHEMAS_LEFT;
Result1 row · 4 columns
THE_DATABASETHE_SCHEMADEMO_DB_IS_READABLEPROBE_SCHEMAS_LEFT
DEMO_DBPUBLIC40

DEMO_DB and PUBLIC, the course data still readable, and no probe schemas left behind.

Key points

  • A table name with no schema in front of it is resolved against CURRENT_SCHEMA(), whatever that happens to be.
  • Two schemas can hold a table of the same name, and nothing warns you. They are different tables.
  • The identical query text returned three rows and then one row, with one USE SCHEMA statement between the two runs.
  • A fully qualified DATABASE.SCHEMA.TABLE name does not move when the session does. Qualify anything you save: a script, a view, a task, a stored procedure.
  • ALTER SCHEMA ... RENAME TO takes the tables with it, and turns the old schema name into a clean compilation error.
  • ALTER SCHEMA ... SWAP WITH exchanges the contents of two schemas in one statement. The names stay put.
  • UNDROP SCHEMA exists, and the tables inside come back with it. So UNDROP covers table, schema and database.
  • The PUBLIC schema can be dropped, so “every database has a PUBLIC schema” is a default and not a guarantee.
  • INFORMATION_SCHEMA refuses, and it refuses on insufficient privileges rather than on a rule.
  • A new database arrives with a PUBLIC schema whether you want one or not. Dropping it afterwards is the only route.
Next in this series

Episode 100 — CREATE TABLE. NOT NULL, DEFAULT and AUTOINCREMENT, and a default that does not do what almost everyone thinks it does.

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, including the three refusals. The _T99 schemas, tables and databases are throwaway objects created and dropped by the scripts above. 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. Schema behaviour follows the Snowflake CREATE SCHEMA documentation.

No comments:

Post a Comment

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