Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12345»»»

Exercises in t-SQL Expand / Collapse
Author
Message
Posted Thursday, November 15, 2012 11:51 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 9:07 PM
Points: 38,381, Visits: 35,308
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1385485
Posted Tuesday, November 27, 2012 5:02 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, July 22, 2015 2:22 AM
Points: 12, Visits: 181
Ok, point taken, thanks for your additional info too, sounds interesting!
Post #1389105
Posted Tuesday, November 27, 2012 3:05 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 12:34 PM
Points: 1,195, Visits: 4,489
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.



-- Alan Burstein



Read this article for best practices on asking questions.
Need to split a string? Try this (Jeff Moden)
Need a pattern-based string spitter? Try this (Dwain Camps)

"I can't 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. " -- Itzek Ben-Gan 2001

My blog
Post #1389430
Posted Tuesday, November 27, 2012 5:58 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 9:07 PM
Points: 38,381, Visits: 35,308
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1389475
Posted Tuesday, November 27, 2012 6:02 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 9:07 PM
Points: 38,381, Visits: 35,308
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, ..


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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1389476
Posted Wednesday, November 28, 2012 1:40 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, August 24, 2015 12:20 PM
Points: 1,064, Visits: 2,582
Jeff Moden (11/27/2012)
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, ..


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.


I'm not sure I'd think less of a coder who didn't know the "n mod 15" trick - that's a nugget of math trivia, not a fundamental programming construct.

If the coder attempting to solve the "fizz-buzz" puzzle (a) knows how to generate a set of sequential integers in T-SQL without a loop construct, (b) understands that the "fizz-buzz-fizzbuzz" requirement is most easily and efficiently satisfied in T-SQL with a CASE expression, (c) recognizes that the "fizzbuzz" condition must come first in the CASE expression, and (d) codes the "fizzbuzz" condition to produce the correct result (either "WHEN n % 3 = 0 AND n % 5 = 0" or "WHEN n % 15 = 0"), he or she has passed with flying colors, in my opinion!

In fact, I wouldn't fault a coder for getting the order of conditions in the CASE expression wrong at first as long as he or she identifies the cause of the incorrect result for multiples of 15 and corrects the error on the second try.


Jason Wolfkill
Blog: SQLSouth
Twitter: @SQLSouth
Post #1390105
Posted Wednesday, November 28, 2012 5:33 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 9:07 PM
Points: 38,381, Visits: 35,308
wolfkillj (11/28/2012)
Jeff Moden (11/27/2012)
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, ..


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.


I'm not sure I'd think less of a coder who didn't know the "n mod 15" trick - that's a nugget of math trivia, not a fundamental programming construct.

If the coder attempting to solve the "fizz-buzz" puzzle (a) knows how to generate a set of sequential integers in T-SQL without a loop construct, (b) understands that the "fizz-buzz-fizzbuzz" requirement is most easily and efficiently satisfied in T-SQL with a CASE expression, (c) recognizes that the "fizzbuzz" condition must come first in the CASE expression, and (d) codes the "fizzbuzz" condition to produce the correct result (either "WHEN n % 3 = 0 AND n % 5 = 0" or "WHEN n % 15 = 0"), he or she has passed with flying colors, in my opinion!

In fact, I wouldn't fault a coder for getting the order of conditions in the CASE expression wrong at first as long as he or she identifies the cause of the incorrect result for multiples of 15 and corrects the error on the second try.


I consider knowing modulus fuctions to be required basic mathematical knowledge for all programmers but especially for anyone working with a database. It goes hand-in-hand with such things as knowing how to do simple integer math for grouping and I certainly don't consider it to be a trivial math function or a trick question. I agree that all of what you've stated is equally important but the knowledge of basic math functions in any programmig language is not a trivial requirement.

Still, I agree... missing one part of one question in the interview process does't guarantee a "No" by any means especially since most of the people I've asked do, in fact, get the modulus portion right and all of the rest of it wrong.


--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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1390212
Posted Wednesday, November 28, 2012 5:46 PM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Wednesday, August 12, 2015 2:18 PM
Points: 887, Visits: 1,782
Jeff Moden (11/27/2012)
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, ..


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.


What position were the people interviewing for? was it entry level developer or some higher position?



For faster help in answering any problems Please read How to post data/code on a forum to get the best help - Jeff Moden 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

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

Jeremy Oursler
Post #1390216
Posted Wednesday, November 28, 2012 9:41 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, August 24, 2015 12:20 PM
Points: 1,064, Visits: 2,582
Jeff Moden (11/28/2012)
wolfkillj (11/28/2012)
Jeff Moden (11/27/2012)
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, ..


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.


I'm not sure I'd think less of a coder who didn't know the "n mod 15" trick - that's a nugget of math trivia, not a fundamental programming construct.

If the coder attempting to solve the "fizz-buzz" puzzle (a) knows how to generate a set of sequential integers in T-SQL without a loop construct, (b) understands that the "fizz-buzz-fizzbuzz" requirement is most easily and efficiently satisfied in T-SQL with a CASE expression, (c) recognizes that the "fizzbuzz" condition must come first in the CASE expression, and (d) codes the "fizzbuzz" condition to produce the correct result (either "WHEN n % 3 = 0 AND n % 5 = 0" or "WHEN n % 15 = 0"), he or she has passed with flying colors, in my opinion!

In fact, I wouldn't fault a coder for getting the order of conditions in the CASE expression wrong at first as long as he or she identifies the cause of the incorrect result for multiples of 15 and corrects the error on the second try.


I consider knowing modulus fuctions to be required basic mathematical knowledge for all programmers but especially for anyone working with a database. It goes hand-in-hand with such things as knowing how to do simple integer math for grouping and I certainly don't consider it to be a trivial math function or a trick question. I agree that all of what you've stated is equally important but the knowledge of basic math functions in any programmig language is not a trivial requirement.

Still, I agree... missing one part of one question in the interview process does't guarantee a "No" by any means especially since most of the people I've asked do, in fact, get the modulus portion right and all of the rest of it wrong.


I didn't mean that I wouldn't expect a coder to know the modulo function, but that I wouldn't expect a coder to know that all numbers that are divisible by both 3 and 5 are also divisible by 15. That's the "math trivia", and ignorance of that fact doesn't reflect negatively on one's ability as a programmer. On the other hand, a fizz-buzz solution that doesn't use the modulo function would raise some serious doubts about the coder's ability.


Jason Wolfkill
Blog: SQLSouth
Twitter: @SQLSouth
Post #1390268
Posted Thursday, November 29, 2012 3:53 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, August 24, 2015 9:31 AM
Points: 2,926, Visits: 5,410
Sorry for my a bit unorthodox view on " FizzBuzz" problem in T-SQL, but couldn't keep myself quiet
I don't think it's a good T-SQL question at all..
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

Agree on this, but I wouldn't ask specifically for FizzBuzz. Probably question like "Could you show few ways to produce a list of N [sequential] numbers in T-SQL" would do good enough for finding if the man can think in "SETs" and aware of T-SQL features such as CTE's, windowed functions and even IDENTITY function which can produce it together with creating a table while used in SELECT INTO.
If I want to check knowledge of CASE in T-SQL, I would prefer to ask the question where it's need to be used in ORDER BY...


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.

Actually, in my experience, there are more people who miss data conversion on number to varchar required in the ELSE part of this CASE...


If the coder attempting to solve the "fizz-buzz" puzzle (a) knows how to generate a set of sequential integers in T-SQL without a loop construct, (b) understands that the "fizz-buzz-fizzbuzz" requirement is most easily and efficiently satisfied in T-SQL with a CASE expression, (c) recognizes that the "fizzbuzz" condition must come first in the CASE expression, and (d) codes the "fizzbuzz" condition to produce the correct result (either "WHEN n % 3 = 0 AND n % 5 = 0" or "WHEN n % 15 = 0"), he or she has passed with flying colors, in my opinion!

I cannot say that knowing MOD defines you as person who knows the basic Math functions in T-SQL. What makes MOD more basic than ABS or POWER for example?
I do agree that Fizz-Buzz question is a good one for other programming languages, but T_SQL...

Now, the final one: If you really want to make an impression (good or bad) on the interview, when asked such question, then ask interviewer if he want to see common solution using MOD or kind of "native" T-SQL which doesn't need one!
When I've done it myself once, I caused a bit of surprised. Technical interviewer (I think at least 10 years younger than me), in front of his manager, instead of simply asking me to show how, replied back: "It's impossible, how will you check the number can be divided by 3 or 5?" And when I said, that it's not really required in T-SQL, I've been asked to explain myself by the manager. In reality it can be done by few ways. Here I will post just one of them:
  ;WITH 
S1 AS (SELECT 1 N FROM (VALUES (1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) A(N)
,(VALUES (1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) B(N))
,Set100
AS
( SELECT ROW_NUMBER() OVER (ORDER BY N) N, 'Fizz' F,'Buzz' B
FROM S1
)
SELECT COALESCE(S15.F + S15.B, S5.B, S3.F, CAST(S1.N AS VARCHAR))
FROM Set100 S1
LEFT JOIN Set100 S3 ON S3.N * 3 = S1.N
LEFT JOIN Set100 S5 ON S5.N * 5 = S1.N
LEFT JOIN Set100 S15 ON S15.N * 15 = S1.N
ORDER BY S1.N

No MOD, No CASE WHEN, just very basic T-SQL joins + COALESCE from ANSI beloved by J.C. ...
I'm no gonna do discuss if it's slower than CASE WHEN with MOD, that was done just to show, if you really can think in "SET's"


_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1390407
« Prev Topic | Next Topic »

Add to briefcase ««12345»»»

Permissions Expand / Collapse