T-SQL Tuesday - T-SQL Puzzle with HAVING

Adam’s hosting another T-SQL Tuesday, for which this post is jumping in. He’s themed it around T-SQL Puzzles, which I found quite interesting, because the world is full of them.

Most of the questions that I answer on forums, help sites, and so on, are puzzles. I guess there’s the difference between “Problem” and “Puzzle”, but I prefer to think of thing as puzzles.

For Adam’s meme though, I thought I’d share a Puzzle that I ask students who take my Advanced T-SQL course. The idea is to have them start thinking about what each component of T-SQL is actually doing, so that they can better address problems they face. If you have a rifle, it’s nice to actually know what the various components of it are for, so that you can use it more effectively.

I actually ask them a large number of things, but the one that I thought I’d pose for you all today is about the results of this. The answer will be in the next blog post, which hopefully you haven’t read yet. I will have them both published on Tuesday 12th, this one at the start of the day, and the answer towards the end of the day.

The question is simply a query. Can you predict the output, and explain why? Feel free to comment to your heart’s content, as I will moderate them and only publish them afterwards. In fact, I’ll probably take a few days to get to them (being holiday period), so I apologise if you’re wanting to read what other people thought too.

Naturally, you can check your answer by actually running the query, but please provide your thoughts before you do. The query is below. There is no FROM clause. There is no GROUP BY clause. Does it error, do you get an empty resultset, do you get a single row containing NULL, do you get a single row with data, do you get multiple rows, or something else I haven’t suggested? Enjoy.

SELECT 'No rows'
WHERE 1=2
HAVING 1=1;

Published Tue, Jan 12 2010 10:01 by Rob Farley
Filed under: , ,

Comments

Monday, January 11, 2010 9:49 PM by Michael

# re: T-SQL Tuesday - T-SQL Puzzle with HAVING

SELECT 1 + 1 will return 2, I have to assume that a column title 'no column name' will contain a single value 'No rows'... but I dont think it will return anything as the where condition is never going to be satisfied.

Tuesday, January 12, 2010 8:30 AM by Michael J Swart

# re: T-SQL Tuesday - T-SQL Puzzle with HAVING

... that was unexpected. And strangely enough this is related to Kalen Delaney's T-SQL Tuesday entry here:

sqlblog.com/.../non-aggregated-columns-in-a-group-by-query.aspx

Tuesday, January 12, 2010 3:54 PM by Rob Farley

# re: T-SQL Tuesday - T-SQL Puzzle with HAVING

Michael,

I think Kalen's is unexpected, but that mine just needs a good understanding of how HAVING works.

Make sure you read my explanation for it at msmvps.com/.../t-sql-tuesday-having-puzzle-answer.aspx

Rob

Monday, February 08, 2010 1:15 PM by Adam Machanic

# T-SQL Tuesday #002: The Roundup

According to T-SQL Tuesday rules as ratified by me in the first and second T-SQL Tuesday posts, the T-SQL