Exercises in t-SQL

  • I'm in the process of setting up a training environment here, it's a mechanism for me to bring high flyers in from the business and give them somewhere to learn SQL.

    I've got training content and t-SQL CBTs etc, but I was wondering whether there were any standard t-SQL exercises that I can make use of that are based on Adventureworks? Or if not, something that I could tailor for my environment?

    Anyone know of any decent content? This would be from complete beginner up...

  • Not truly knowing your groups background and how deep in depth you wish to go with your training, may I suggest reviewing the "SPACKLE" articles here on SSC.

    Just enter the term "SPACKLE" in the search window in the upper right hand corner of this page and give it a quick look over.

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

  • bitbucket-25253 (11/13/2012)


    Just enter the term "SPACKLE" in the search window in the upper right hand corner of this page and give it a quick look over.

    That is actually quite useful, but not what I'm looking for (this will be of value though!). What I'm looking for is exercises for the guys to do (who could be literally learning how to write a SELECT statement for the first time). I.e. can you pick out all the staff called bob from adventureworks who are right handed (random!). That kind of thing. I can write a load up myself, but I'm hoping someone else has done it for me ๐Ÿ˜€

  • I just wrote up a similar course and could not find any sample data to my liking. I started with Adventureworks but due to the multiple schema's decided against it for my class. went with a much simpler setup using only the default schema. i also looked at it as a good exercise for generating random test data. Really helped me in that area and i now am much more confident at creating the random data for testing queries if i dont have nice production data.

    EDIT: I was able to find the CREATE TABLE and data inserts for the DB i used. It is based off of the script from Essential SQL on SQL Server 2008 by Sikha Bagui with some rather heavy modifications by me to better suit the 5 day course i wrote.


    For faster help in answering any problems Please read How to post data/code on a forum to get the best help - Jeff Moden[/url] for the best way to ask your question.

    For performance Issues see how we like them posted here: How to Post Performance Problems - Gail Shaw[/url]

    Need to Split some strings? Jeff Moden's DelimitedSplit8K[/url]
    Jeff Moden's Cross tab and Pivots Part 1[/url]
    Jeff Moden's Cross tab and Pivots Part 2[/url]

  • T4FF (11/14/2012)


    bitbucket-25253 (11/13/2012)


    Just enter the term "SPACKLE" in the search window in the upper right hand corner of this page and give it a quick look over.

    That is actually quite useful, but not what I'm looking for (this will be of value though!). What I'm looking for is exercises for the guys to do (who could be literally learning how to write a SELECT statement for the first time). I.e. can you pick out all the staff called bob from adventureworks who are right handed (random!). That kind of thing. I can write a load up myself, but I'm hoping someone else has done it for me ๐Ÿ˜€

    Create a million row FizzBuzz table. FizzBuzz isn't a comment. It's the name of a very common problem based on a child's game, is frequently given on interviews, and is actually a wonderful teaching tool to show people the difference between RBAR, Hidden RBAR, and Set Based methods that use "Pseudo Cursors". Have them do it with explicit loops, with a recursive CTE, without either of those at least 3 different ways, and then measure and discuss the resource usage and performance differences. So far as I'm concerned, it's one of the more important concepts in SQL Server.

    The other exercise would be to teach people how to use Books Online and Google.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Try this:

  • Well, that didn't work very well, did it. You get the picture.

    http://www.sqlcourse.com/index.html

  • Jeff Moden (11/14/2012)

    Create a million row FizzBuzz table.

    You saw this right??

    T4FF (11/14/2012)who could be literally learning how to write a SELECT statement for the first time

    ๐Ÿ˜€ I'd scare them back to their departments with that ๐Ÿ˜‰

    Jeff Moden (11/14/2012)The other exercise would be to teach people how to use Books Online and Google.

    True!

  • I'm not sure if I've explained myself very well, as none of the suggestions are quite what I'm after. We've got training material, books, CBTs and coaching. What I'm after is exercises to give them to go away and figure out, scenarios....like can you tell me how many employees are over 50, can you tell me how many employees have red cars, can you tell me how many employees surnames begin with a etc etc

  • T4FF (11/15/2012)


    Jeff Moden (11/14/2012)

    Create a million row FizzBuzz table.

    You saw this right??

    T4FF (11/14/2012)who could be literally learning how to write a SELECT statement for the first time

    ๐Ÿ˜€ I'd scare them back to their departments with that ๐Ÿ˜‰

    Jeff Moden (11/14/2012)The other exercise would be to teach people how to use Books Online and Google.

    True!

    Yep... saw that. That's why I suggested the FizzBuzz thing first. Too many people jump into queries to find out things like how many people have Red cars and the like before they learn the one thing that's common and absolutely essential to all computer languages... how to count. Done correctly, it teaches by revelation (This is your brain on cursors and loops. BAD cursors and loops. This is your brain on recursive CTE's that count. BAD recursive CTEs that count. This is your brain on set based code. See? It likes you!) and once the students realize that they just created a million row table in a matter of seconds, they get a sense of accomplishment and a real sense of what performance actually is. It all leads to them taking on a bit of pride and excitement that will lead them to the Red car things.

    For what you're actually asking for, the best book ever for teaching the basics (in my humble opinion) is a very old book call "SQL Server 6.5 Design & Implimentation". It's absolutely loaded with some wonderful exercises along some review questions. It's out of print but you can usually find a used copy on Amazon.com. The ISBN is 1-56205-830-4. You should be able to float one for just a couple of bucks and it'll be worth every penny you spend on the shipping.

    Another good source of non-intimidating exercises is W3Schools.com. It even includes "try me" windows where folks can work from home even if they don't have SQL Server installed.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Ok, point taken, thanks for your additional info too, sounds interesting!

  • For the crowd you describe I think the SQL tutorial at W3Schools is a good start (my 2ยข). Like Jeff mentioned they have a "Try it yourself" section and they even have a little, 20-question test.

    "I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code."

    -- Itzik Ben-Gan 2001

  • CELKO (11/27/2012)


    I would also spend more time on the DDL than most people do. Good DDL with lots of constraints, DRI actions, etc. can do 80-90% of the hard work. And have at least one exercise in Normalization.

    Unfortunately, Adventureworks sucks as a schema. Its purpose was to show off as many T-SQL features as possible without much regard to Normalization, ISO standards, or basic data modeling.

    Hear HERE! I strongly second those recommendations! A bit on formatting, readability, and consistancy would go a long way as well. I just got done trying to read some code written by a 3rd party and the inconsistancies in case, indentation, and style (all written by the same person) were just awful. Teach them to take pride in their code. Seeing the word "SET" spelled as "set", "Set", "SET", and "SeT" within 4 adjacenct lines of code just doesn't make sense to me especially with all of the tools available nowadays.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • CELKO (11/27/2012)


    Create a million row FizzBuzz table. FizzBuzz isn't a comment. It's the name of a very common problem based on a child's game, is frequently given on interviews, ..

    :laugh: I had to do a FizzBuzz on an interview two years ago! I was appalled to be told that a lot of people failed it. The trick that even the experienced guys miss is MOD(n, 15) rather than just MOD(n, 3) and MOD(n, 5) in the CASE expression.

    The other exercise would be to teach people how to use Books Online and Google.

    Amen.

    Heh... too funny. I've given it as an interview question a couple of times just to see what would happen. DISMAL failure! I'd be happy if someone could actually get it right even using a WHILE loop never mind something like a cteTALLY or a cross join with ROW_NUMBER. Even the ones that think of the MOD(n,15) (n%15 in SQL Server) always put it last in the CASE statement simply because it comes last in the problem description.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 15 posts - 1 through 15 (of 73 total)

You must be logged in to reply to this topic. Login to reply