<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Rob Farley : sql, fun</title><link>http://msmvps.com/blogs/robfarley/archive/tags/sql/fun/default.aspx</link><description>Tags: sql, fun</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>T-SQL Tuesday - HAVING Puzzle answer</title><link>http://msmvps.com/blogs/robfarley/archive/2010/01/12/t-sql-tuesday-having-puzzle-answer.aspx</link><pubDate>Tue, 12 Jan 2010 13:59:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1749663</guid><dc:creator>Rob Farley</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/robfarley/rsscomments.aspx?PostID=1749663</wfw:commentRss><comments>http://msmvps.com/blogs/robfarley/archive/2010/01/12/t-sql-tuesday-having-puzzle-answer.aspx#comments</comments><description>&lt;p&gt;Earlier today you may have seen a &lt;a href="http://msmvps.com/blogs/robfarley/archive/2010/01/12/t-sql-tuesday-t-sql-puzzle-with-having.aspx" target="_blank"&gt;blog post of mine about a puzzle involving HAVING&lt;/a&gt;. You should read that post before this one. It was part of &lt;a href="http://sqlblog.com/blogs/adam_machanic/archive/2010/01/04/invitation-for-t-sql-tuesday-002-a-puzzling-situation.aspx" target="_blank"&gt;Adam Machanic’s T-SQL Tuesday&lt;/a&gt; meme.&lt;/p&gt;  &lt;p&gt;The question was about the query:&lt;/p&gt; SELECT &amp;#39;No Rows&amp;#39;   &lt;br /&gt;WHERE 1=2   &lt;br /&gt;HAVING 1=1;   &lt;p&gt;And here’s the explanation.&lt;/p&gt;  &lt;p&gt;Start by making yourself a &amp;quot;dual table&amp;quot;, like what you&amp;#39;d use in Oracle, and use this instead of having no FROM clause. Put a row in it. &lt;/p&gt;  &lt;p&gt;CREATE TABLE dual (dummy bit);    &lt;br /&gt;INSERT dual VALUES (1);&lt;/p&gt;  &lt;p&gt;--Now count the rows in it    &lt;br /&gt;SELECT COUNT(*)     &lt;br /&gt;FROM dual;&lt;/p&gt;  &lt;p&gt;--Now count how many rows don&amp;#39;t match 1=2 (of course, the answer is zero)    &lt;br /&gt;SELECT COUNT(*)     &lt;br /&gt;FROM dual     &lt;br /&gt;WHERE 1=2;&lt;/p&gt;  &lt;p&gt;--Naturally we&amp;#39;d get nothing back if we weren&amp;#39;t grouping    &lt;br /&gt;SELECT &amp;#39;Something&amp;#39;     &lt;br /&gt;FROM dual     &lt;br /&gt;WHERE 1=2;&lt;/p&gt;  &lt;p&gt;--But HAVING forces the grouping functionality as well (like using COUNT(*))    &lt;br /&gt;SELECT &amp;#39;Something&amp;#39;     &lt;br /&gt;FROM dual     &lt;br /&gt;WHERE 1=2     &lt;br /&gt;HAVING 1=1;&lt;/p&gt;  &lt;p&gt;--So in this query, we couldn&amp;#39;t put any of our real columns in, only aggregate functions and constants    &lt;br /&gt;SELECT *     &lt;br /&gt;--Errors     &lt;br /&gt;FROM dual     &lt;br /&gt;WHERE 1=2     &lt;br /&gt;HAVING 1=1;&lt;/p&gt;  &lt;p&gt;--And leaving out the FROM clause implies that we&amp;#39;re asking all this of a secret internal table with a single row. All these queries work just the same without the FROM clause at all. &lt;/p&gt;  &lt;p&gt;--Count the rows in our pretend table (one)    &lt;br /&gt;SELECT COUNT(*)&lt;/p&gt;  &lt;p&gt;--Now count how many rows don&amp;#39;t match 1=2 (zero)    &lt;br /&gt;SELECT COUNT(*)     &lt;br /&gt;WHERE 1=2;&lt;/p&gt;  &lt;p&gt;--Naturally we&amp;#39;d get nothing back if we weren&amp;#39;t grouping    &lt;br /&gt;SELECT &amp;#39;Something&amp;#39;     &lt;br /&gt;WHERE 1=2;&lt;/p&gt;  &lt;p&gt;--But HAVING forces the grouping functionality as well    &lt;br /&gt;SELECT &amp;#39;Something&amp;#39;     &lt;br /&gt;WHERE 1=2     &lt;br /&gt;HAVING 1=1;&lt;/p&gt;  &lt;p&gt;So the answer to the question posed is that you get a single row, containing the text provided. The fact that I used the text &amp;#39;No Rows&amp;#39; was just a bit of fun.&lt;/p&gt;  &lt;p&gt;Now, to remove the trivia a little…&lt;/p&gt;  &lt;p&gt;When would you ever use HAVING without GROUP BY in a practical situation? &lt;/p&gt;  &lt;p&gt;How about this:&lt;/p&gt;  &lt;p&gt;Using sp_MSforeachdb, find the number of objects in non-system databases. It’s an undocumented system stored procedure which runs a query on each database, replacing a question mark in the query with the name of the database. It can be quite handy, just don’t look at how it’s implemented.&lt;/p&gt;  &lt;p&gt;EXEC sp_MSforeachdb &amp;#39;SELECT &amp;#39;&amp;#39;?&amp;#39;&amp;#39;, COUNT(*) FROM ?.sys.objects WHERE &amp;#39;&amp;#39;?&amp;#39;&amp;#39; NOT IN (&amp;#39;&amp;#39;master&amp;#39;&amp;#39;,&amp;#39;&amp;#39;tempdb&amp;#39;&amp;#39;,&amp;#39;&amp;#39;model&amp;#39;&amp;#39;,&amp;#39;&amp;#39;msdb&amp;#39;&amp;#39;);&amp;#39;;&lt;/p&gt;  &lt;p&gt;But this won&amp;#39;t do it. It will still return the entries for the system databases, but with zeroes (because none of the objects satisfied the WHERE clause). Replace WHERE with HAVING and it&amp;#39;s just fine – the rows get eliminated from the resultset.&lt;/p&gt;  &lt;p&gt;EXEC sp_MSforeachdb &amp;#39;SELECT &amp;#39;&amp;#39;?&amp;#39;&amp;#39;, COUNT(*) FROM ?.sys.objects HAVING &amp;#39;&amp;#39;?&amp;#39;&amp;#39; NOT IN (&amp;#39;&amp;#39;master&amp;#39;&amp;#39;,&amp;#39;&amp;#39;tempdb&amp;#39;&amp;#39;,&amp;#39;&amp;#39;model&amp;#39;&amp;#39;,&amp;#39;&amp;#39;msdb&amp;#39;&amp;#39;);&amp;#39;;&lt;/p&gt;  &lt;p&gt;Honestly, HAVING doesn&amp;#39;t require a GROUP BY clause. It doesn&amp;#39;t require anything. It filters based on groups, and if there are no groups yet, it makes some – like how using an aggregate will count the rows in an empty set and return one row representing that group.&lt;/p&gt;  &lt;p&gt;It&amp;#39;s generally taught as &amp;quot;HAVING is for filtering based on aggregates&amp;quot;, and that&amp;#39;s true, but only half the story. And I find that if I&amp;#39;m teaching people to write better queries, I want them to have a thorough understanding of what each construct is really doing.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1749663" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/robfarley/archive/tags/sql/default.aspx">sql</category><category domain="http://msmvps.com/blogs/robfarley/archive/tags/fun/default.aspx">fun</category><category domain="http://msmvps.com/blogs/robfarley/archive/tags/t-sql+tuesday/default.aspx">t-sql tuesday</category></item><item><title>T-SQL Tuesday - T-SQL Puzzle with HAVING</title><link>http://msmvps.com/blogs/robfarley/archive/2010/01/12/t-sql-tuesday-t-sql-puzzle-with-having.aspx</link><pubDate>Tue, 12 Jan 2010 00:01:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1749661</guid><dc:creator>Rob Farley</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/robfarley/rsscomments.aspx?PostID=1749661</wfw:commentRss><comments>http://msmvps.com/blogs/robfarley/archive/2010/01/12/t-sql-tuesday-t-sql-puzzle-with-having.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://sqlblog.com/blogs/adam_machanic/default.aspx" target="_blank"&gt;Adam&lt;/a&gt;’s hosting another T-SQL Tuesday, for which this post is jumping in. He’s themed it around &lt;a href="http://sqlblog.com/blogs/adam_machanic/archive/2010/01/04/invitation-for-t-sql-tuesday-002-a-puzzling-situation.aspx" target="_blank"&gt;T-SQL Puzzles&lt;/a&gt;, which I found quite interesting, because the world is full of them.&lt;/p&gt;  &lt;p&gt;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.&lt;/p&gt;  &lt;p&gt;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.&lt;/p&gt;  &lt;p&gt;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.&lt;/p&gt;  &lt;p&gt;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.&lt;/p&gt;  &lt;p&gt;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.&lt;/p&gt;  &lt;p&gt;SELECT &amp;#39;No rows&amp;#39;    &lt;br /&gt;WHERE 1=2     &lt;br /&gt;HAVING 1=1;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1749661" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/robfarley/archive/tags/sql/default.aspx">sql</category><category domain="http://msmvps.com/blogs/robfarley/archive/tags/fun/default.aspx">fun</category><category domain="http://msmvps.com/blogs/robfarley/archive/tags/t-sql+tuesday/default.aspx">t-sql tuesday</category></item><item><title>Things You Know Now</title><link>http://msmvps.com/blogs/robfarley/archive/2009/03/17/things-you-know-now.aspx</link><pubDate>Mon, 16 Mar 2009 23:44:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1678911</guid><dc:creator>Rob Farley</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/robfarley/rsscomments.aspx?PostID=1678911</wfw:commentRss><comments>http://msmvps.com/blogs/robfarley/archive/2009/03/17/things-you-know-now.aspx#comments</comments><description>&lt;p&gt;This blog meme is doing the rounds&amp;hellip; I&amp;rsquo;ve been tagged at least twice now (&lt;a href="http://blogs.digineer.com/blogs/jasons/archive/2009/02/16/things-you-know-now.aspx" target="_blank"&gt;Jason Strate&lt;/a&gt; and &lt;a href="http://blogs.sqlserver.org.au/blogs/greg_linwood/archive/2009/03/16/1488.aspx" target="_blank"&gt;Greg Linwood&lt;/a&gt;), so I suppose subconsciously I&amp;rsquo;ve been thinking about this stuff for a few weeks already.&lt;/p&gt;
&lt;p&gt;Since I do a lot of training, I tend to explain these things to my students anyway. I have a lot of opportunity to stand up in front of people and tell them important stuff &amp;ndash; so this kind of thing definitely comes up now and then.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Things I wish I had known years ago &lt;/b&gt;(career-wise that I would teach new people in the SQL field)&lt;/p&gt;
&lt;p&gt;&lt;b&gt;The Importance of technical communities&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;I remember when &lt;a href="http://www.craigbailey.net/live/post/2008/10/20/My-ideal-role.aspx" target="_blank"&gt;Craig Bailey wrote about his ideal role&lt;/a&gt;. It wasn&amp;rsquo;t new stuff &amp;ndash; I had heard it all before, but it certainly got me thinking about how people can influence where they are in that &lt;a href="http://craigbailey.net/live/image.axd?picture=WindowsLiveWriter/Myidealrole_D6FF/image_2.png" target="_blank"&gt;Venn diagram&lt;/a&gt;. For Craig, he wanted his ideal role to be a job that he was good at and that he enjoyed. Obviously to be a job, someone has to be prepared to pay him sufficiently too.&lt;/p&gt;
&lt;p&gt;Being good at something you enjoy isn&amp;rsquo;t hard, and you can invest your own time (outside of the job that you don&amp;rsquo;t enjoy) developing your skills. For people in IT, I suggest they pick a particular area they find interesting, and start getting their skills up. If they can become expert-level in that area, then great. &lt;/p&gt;
&lt;p&gt;The next problem though, is moving that skill into something that lets you can earn money. Community can help that. Community can help you develop your skills, because you&amp;rsquo;re spending time with other people in your field. But as you become an expert, presenting at community events, developing a profile, you find yourself being differentiated from the rest. If nothing else, people know you have presentation skills. Every presentation can become like a job interview &amp;ndash; showing your skills and ability to communicate information to clients, colleagues, whoever. &lt;/p&gt;
&lt;p&gt;Presenting isn&amp;rsquo;t easy, but there are plenty of other communities that can help develop those skills. You can get along to a &lt;a href="http://www.toastmasters.net.au/" target="_blank"&gt;ToastMasters&lt;/a&gt; group, or offer to do presentations in a group to which you already belong. &lt;/p&gt;
&lt;p&gt;You might be the best in the world at what you do &amp;ndash; but you need to get out there. I enjoy the technical communities, and run the &lt;a href="http://www.sqlserver.org.au/" target="_blank"&gt;Adelaide SQL Server User Group&lt;/a&gt; because I enjoy it. But I can&amp;rsquo;t deny that it&amp;rsquo;s been useful for my career. Now, I&amp;rsquo;m wishing that I had got involved many years ago.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Enjoy public speaking&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;According to the old saying, more people are afraid of public speaking than death (so at a funeral, they&amp;rsquo;d rather be in the coffin than giving the eulogy). But it&amp;rsquo;s a useful skill to have, so learn to enjoy it.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Keep in touch with old friends&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;This isn&amp;rsquo;t quite so career-related, but is actually very important for your career nonetheless.&lt;/p&gt;
&lt;p&gt;There are people that I haven&amp;rsquo;t seen in years, who I have no idea how to contact. Facebook (and the internet in general) has proved very useful for that, but still there are many people that I wish I could find. Most of them are just people I would like to spend time with now and then, but some are people that I&amp;rsquo;d happily offer to do some work for. And perhaps some of them would contact me to do some consulting if they knew how to reach me (clue, there&amp;rsquo;s contact information to the side of my blog site!). &lt;/p&gt;
&lt;p&gt;Far too many people fall out our lives, and it&amp;rsquo;s sad. I&amp;rsquo;m still not great at it, but I do think I should take the time to write people letters now and then (emails, Facebook comments, Instant Messages are all fine too &amp;ndash; I&amp;rsquo;m just talking about touching base to keep the contact there).&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Certifications aren&amp;rsquo;t worth studying for (but they are worth taking)&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;I used to study for exams. I first became a Microsoft Certified Professional back in 1998, passing an exam called &amp;ldquo;Architecture I&amp;rdquo;. Since then I&amp;rsquo;ve passed over 30 exams, and earned plenty of certifications. But a few years ago I worked out at that it&amp;rsquo;s just not worth studying for these things. &lt;/p&gt;
&lt;p&gt;A MCP exam is not like high school or university. If you fail, you can just try again. Fails don&amp;rsquo;t appear on your transcript, only the passes do. It&amp;rsquo;s like your driving test &amp;ndash; if you fail, you just try again. Once you pass, you get access to the roads like everyone else. &lt;/p&gt;
&lt;p&gt;If you spend weeks studying for a MCP exam, you probably won&amp;rsquo;t even improve your chances of passing &amp;ndash; you&amp;rsquo;ll just be spending precious family time trying to learn those things to get you past the line. You might even start losing sleep over it.&lt;/p&gt;
&lt;p&gt;Nowadays, I tell my students (and myself) to care less. Plenty of people say &amp;ldquo;No, you don&amp;rsquo;t understand &amp;ndash; I can&amp;rsquo;t fail at anything.&amp;rdquo;, and I understand that. I&amp;rsquo;m not particularly good with failure either. But I&amp;rsquo;ve learned to not care so much. I don&amp;rsquo;t want to waste time sitting an exam only find that I fail (or spend $180 on the privilege), but I also don&amp;rsquo;t want to waste time studying for an exam that I could probably pass anyway. With the Second Shot offer that is often around you&amp;rsquo;ve paid for two attempts, so go into the first one blind.&lt;/p&gt;
&lt;p&gt;The amount of time you invest in getting a certification is largely the study time. So if you can reduce that, the certification becomes a lot cheaper &amp;ndash; in which case, it&amp;rsquo;s probably worth taking the few hours to give it a try. If you do fail, you know you have a weak area, so you can improve that with study &amp;ndash; just don&amp;rsquo;t bother studying before the first try. &lt;/p&gt;
&lt;p&gt;[Edited: I should make it very clear that I definitely approve of learning new skills, and preparation for an exam is a great prompt for this learning. Better still is learning for the sake of getting those new skills, with the focus being an upcoming project or new role. My advice above is focussed on people who have the skills necessary to pass an exam.]&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Reading execution plans, and understanding indexes&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;ve always been good at solving problems with T-SQL (or PL/SQL for that matter) &amp;ndash; I just took to it naturally when I got into databases. But it took me several years to actual venture into understanding what the query is actually doing when it runs. Now, I look at the execution plan for every query I write, as default behaviour, and I consider the indexes that I want up front.&lt;/p&gt;
&lt;p&gt;Perhaps it&amp;rsquo;s because I was a programmer first, but I had always trusted the compiler to do things the right way. I had looked a bit past my code when studying Prolog at university, but it took me a long time to make that my default behaviour. &lt;/p&gt;
&lt;p&gt;So when I find people who are just getting into T-SQL, I encourage them to look at the execution plans, and start getting a feel for what&amp;rsquo;s going on behind the scenes. You can often improve a query without looking at the execution plan, but if you want to write really good T-SQL and have well-performing queries, you need to make the execution plan part of the process.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;The significance of BI to businesses&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;I was involved in data warehouses in some of my first projects when I left university, I just didn&amp;rsquo;t realise at the time. I first got involved in SQL Server in version 6.0, and quite early on I migrated a system to 6.5, and created a data warehouse to allow for various reports. In hindsight, I was making a data warehouse. I had an ETL process, calculated aggregates, considered the dimensions and granularity, all that. But it wasn&amp;rsquo;t called a data warehouse, and I only realised a few years later that it really was one.&lt;/p&gt;
&lt;p&gt;If I had&amp;rsquo;ve realised, then I&amp;rsquo;m sure I would&amp;rsquo;ve jumped into the BI space much earlier. Companies love BI &amp;ndash; it&amp;rsquo;s one of the most empowering areas of database technology for any business. &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;ve picked a few things here &amp;ndash; and I hope people somehow get some benefit from reading it. I have put it in my &amp;lsquo;must read&amp;rsquo; list to find &lt;a href="http://www.straightpathsql.com/blog/2009/2/13/dear-old-self-read-these-blogs.html" target="_blank"&gt;other people&amp;rsquo;s responses&lt;/a&gt;, because I&amp;rsquo;m sure there are things that I&amp;rsquo;m still to learn.&lt;/p&gt;
&lt;p&gt;Tagging some other people: &lt;a href="http://sqlblogcasts.com/blogs/simons" target="_blank"&gt;Simon Sabin&lt;/a&gt;, &lt;a href="http://blogs.conchango.com/jamiethomson/" target="_blank"&gt;Jamie Thomson&lt;/a&gt;, &lt;a href="http://www.onedotnetway.com/" target="_blank"&gt;Deepak Kapoor&lt;/a&gt;, &lt;a href="http://www.angrykoala.com.au/_blog/Blog" target="_blank"&gt;Grant Paisley&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1678911" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/robfarley/archive/tags/community/default.aspx">community</category><category domain="http://msmvps.com/blogs/robfarley/archive/tags/certification/default.aspx">certification</category><category domain="http://msmvps.com/blogs/robfarley/archive/tags/sql/default.aspx">sql</category><category domain="http://msmvps.com/blogs/robfarley/archive/tags/fun/default.aspx">fun</category><category domain="http://msmvps.com/blogs/robfarley/archive/tags/professional+development/default.aspx">professional development</category></item></channel></rss>