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

This Table Creation puzzle asks for an explanation before you run its code. I suggest examining the batch separators carefully.

A fitted drawer belongs to one cabinet while a neighboring cabinet has an empty recess.

USE tempdb;
GO
CREATE SCHEMA MyScheMA
CREATE TABLE MyTable1 (ID int);
GO
SELECT * FROM MyTable1;

Creating the schema and table succeeds in the original scenario. The final SELECT reports error 208: Invalid object name MyTable1. Why does that happen? State the default-schema and pre-existing-object assumptions.

Use a disposable database without those names if you later test an answer. The code remains the challenge. Readers and comments can supply the explanation.

The original promotion offered three workshop entries with 30-day access and newsletter-subscription eligibility. Its deadline was October 25, 2019. That promotion has ended. The related puzzles provide further challenges.

Related reading

A puzzle result is not independent of connection context, it is behavior to explain under explicit assumptions.

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.


Discover more from SQL Authority with Pinal Dave

Subscribe to get the latest posts sent to your email.

Schema, SQL Scripts, SQL Server, SQL Table Operation
Previous Post
Delete the Wrong Row: A Cheap Answer That Passes the Test
Next Post
SQL Puzzle – IN and IS NOT NULL – Strange Results

Related Posts

319 Comments. Leave new

  • Hi,
    I knew time is over. But I saw it today. first thought of mind error due to schema. Then i execute it also, and find yes my first thought is correct.

    correct statement will be for select query : SELECT * FROM MyScheMA.MyTable1

    it is just issue of scope of “MyTable1”.

    Reply
  • Battepati Anantha Krishna
    November 2, 2019 10:16 am

    Thank you ?, I hope you can see me my answer as well. Because it’s showing me the message, “your comment is awaiting moderation” message on top of the comment.

    Reply
  • Kuldeep Kumawat
    November 3, 2019 12:11 pm

    Because MyTable1 is created under MyScheMA.
    So correct select statement would be select * from MyScheMA.MyTable1

    Reply
  • Hi there, I can’t see my answer in the comments.

    Reply
  • This was one such contest, where pretty much everyone got the correct answer and it was impossible to mention everyone’s name. There are over 300 correct answer. The judges has selected 3 different winners and each will get personalized email from the team.

    Reply
  • Dear Sir
    I have conclusion about this question sir.
    1. As per SQL Server Standards we cannot possible to execute the Create & Select Statement at time.
    2. By Defect SQL Server behavior is, If we creating Or selecting any table we need to mention below
    following four parts in Statement
    a. Server Name
    b. Database Name
    c. Schema (Table Owner)
    d. Table Name
    3. By Default SQL Server it will assigned Based on Login below Following Parts
    a. Server Name
    b. Database Name
    c. Schema Name
    Above is SQL Server Standards may I correct sir.

    Now my question is.

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

    This above syntax we have not mentioned any Schema Name while creating table, why it was taken automatically previous created schema. This conclusion we need solve.
    Because any select statement we need to mention four parts if any missing it will give error this commonly understood,
    Why created new table recently created new Schema because we have not mention any schema while creating table this is my question.
    Sir this purely my clarification purpose sir.

    Reply
  • Whoo Hoo! Good luck to everyone.
    Keeping my fnkrs crsddtes (hard to type with crossed fingers :)

    Reply
  • Since we did not used “GO” after creation of schema, the table is created under MySchema scope level.
    so we have to write MySchema.MyTable1 in “SELECT” query.

    if we try in different way we can eliminate above error from your question
    CREATE SCHEMA MyScheMA
    go
    CREATE TABLE MyTable1 (ID INT)
    go
    SELECT * FROM MyTable1

    Reply
  • tempdb works differently. when sql server is restarted, tempdb gets created using the template which is the model. Once recreated, we end up losing our work done before restarting the tempdb.

    Reply
  • Allen Shepard
    April 13, 2020 7:51 pm

    Anshika, Yes. This is why putting TempDb on RAM drive. A drive that lives in memory only.
    Helps SSD drives live longer. They seem to be faster but I do not have any numbers.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.