SQL Puzzle – Schema and Table Creation – Answer Without Running Code

It has been a while we have a puzzle on this blog. Let us have a very simple and interesting puzzle today about Schema and Table Creation. Please answer this puzzle without running the code. If you totally can’t figure out why this is happening, you may run the code as well.

SQL Puzzle - Schema and Table Creation - Answer Without Running Code table-creation-800x535

Puzzle – Schema and Table Creation

Run the following code in the SQL Server Management Studio.

USE TempDB
GO
CREATE SCHEMA MyScheMA
CREATE TABLE MyTable1 (ID INT)
GO
SELECT * FROM MyTable1

After creating a schema and table when you try to select the data from Table, you get the following error:

Msg 208, Level 16, State 1, Line 6
Invalid object name ‘MyTable1’.

Answer the Question: When we created the schema and table, we got no error. Now, why do we get an error when we select data from the table?

Winning Prize Worth USD 1000

We will be announcing 3 winners. Each winner will get free entry to the SQL Server Performance Tuning Practical Workshop – Recorded Classes. You will get free access to the class for 30 days.

Rules to Win the Puzzle

Post your valid answer on or before October 25th, 2019 (Anywhere in the world) in the comments section.

You must be a subscriber of the newsletter on or before October 25th, 2019.

If you enjoyed this puzzle, here are a few puzzles that I had published earlier. I am sure you may find them interesting as well.

Reference: Pinal Dave (https://blog.sqlauthority.com)

Schema, SQL Scripts, SQL Server, SQL Table Operation
Previous Post
Winners and Solution – DELETE Qualified Rows From Multiple Tables
Next Post
SQL Puzzle – IN and IS NOT NULL – Strange Results

Related Posts

319 Comments. Leave new

  • When you created the table you were still in the same batch as the Schema creation so the table was created in MyScheMA, but the GO statement takes you to a new batch so it looks for the table in the default schema

    Reply
  • Sunil Immadisetty
    October 17, 2019 8:50 pm

    Schema name was not used in the select statement

    Reply
  • You need to add USE MyScheMA first to switch to the correct database/schema because you are now in TempDB which doesn’t have the table.

    Reply
  • Table created in myschema…

    Reply
  • When create table is executed with create schema in one block, the table is owned by the schema created in preceding statement and not dbo. Select * from MyScheMA.MyTable1 will need to be executed to query the table. Alternatively if we separate the create schema and create table statements with a batch separator “Go”; the table will be created under default schema dbo and the select statement “Select * from MyTable1” will work as it will default to dbo schema where the table would have been created..

    Reply
  • Rolando Almaguer
    October 17, 2019 9:04 pm

    you need to query the right schema, as you created one schema and a table on the schema, you con not point to a table on the database schema, but on the new created one…. hope it is clear.
    Run instead “select * from myschema.mytable1”

    Reply
  • When you CREATE SCHEMA MyScheMA with additional object creation (the create table statements) in the same batch (before the GO), the extra objects will be contained in the newly created schema.

    When trying to select from the newly created table. The FROM clause in the SELECT statement did not specify the schema MyScheMA in the table_source reference. Since the currently connected user was not assigned a default schema of MyScheMA, SQL can not find the object name MyTable1 in the current users schema … The table MyTable1 exists in the MyScheMA schema.

    If the select statement was written like this it would have worked:
    SELECT * from MyScheMA.MyTable1

    Reply
  • Mustafa Soner Acar
    October 17, 2019 9:06 pm

    Because we created the table under schema “MyScheMA”. When we do a bare select, it is assumed we are using “dbo” schema by default.

    We should use
    SELECT * FROM MyScheMA.MyTable1
    to get results.

    Reply
  • The table was created by default in the dbo schema. In the select statement, the table must be qualified by “dbo.”.The creation of MyScheMA is a red herring.

    Reply
  • Table was created without ‘;’ so statement ran fine but table didn’t created.
    That’s why we are not able to run last select command

    Reply
  • The table is created in the new schema as they are in the same batch and SSMS is being “helpful”. Since you are not including the schema name in the select statement, it is giving you the error as the table does not exist in the default schema.

    Reply
  • Ferhat KarataÅŸ
    October 17, 2019 9:35 pm

    Wrong database selected

    Reply
  • Table belongs to schema
    So
    Select * from schemaname.tablename;

    Reply
  • where put puzzle answers?

    Reply
  • SELECT * FROM MyScheMa.MyTable1

    Reply
  • without “go” separator, it creates table under MyScheMA and then select statement divided by “go” should use that schema name…

    Reply
  • In between Go is missing

    Reply
  • Hi, This one is easy, it is because you need to specify the schema name like SELECT * FROM MyScheMA.MyTable1, otherwise SQL Server will try to use dbo schema instead…

    Reply
  • When locating an object, SQL Server will first check the user’s default schema. If the object is not found, SQL Server will then check the dbo schema. it is important to include schema names when referring to an object, in this case – MyScheMA.

    Reply
  • You need to also specify the schema in the select like so SELECT * FROM MyScheMA.MyTable1

    Reply

Leave a Reply