{"id":3454,"date":"2011-05-12T16:20:55","date_gmt":"2011-05-12T14:20:55","guid":{"rendered":"http:\/\/code.openark.org\/blog\/?p=3454"},"modified":"2011-05-12T16:20:55","modified_gmt":"2011-05-12T14:20:55","slug":"pop-quiz-answered-what-would-be-the-results-of-the-following-queries","status":"publish","type":"post","link":"https:\/\/code.openark.org\/blog\/mysql\/pop-quiz-answered-what-would-be-the-results-of-the-following-queries","title":{"rendered":"Pop quiz answered: &#8220;what would be the results of the following queries?&#8221;"},"content":{"rendered":"<p>The quiz presented poses with an uncommon, though valid SQL syntax: <em>one is allowed to use quoted name aliases<\/em>. Thus, it is valid to write:<\/p>\n<blockquote>\n<pre>SELECT Name AS 'n', Continent AS 'c' FROM countries<\/pre>\n<\/blockquote>\n<p>But what does the above mean? Let&#8217;s see the results of our three questions:<!--more--><\/p>\n<h4>Question #1<\/h4>\n<blockquote>\n<pre>SELECT\r\n  id,\r\n  Name AS 'n',\r\n  Continent AS 'c'\r\nFROM\r\n  countries\r\nORDER BY\r\n  c\r\n;\r\n+----+--------------------------+---------------+\r\n| id | n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 | c\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 |\r\n+----+--------------------------+---------------+\r\n| 15 | North Korea\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 | Asia\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 |\r\n| 12 | Nepal\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 | Asia\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 |\r\n| 11 | Norway\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 | Europe\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 |\r\n| 10 | Netherlands\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 | Europe\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 |\r\n|\u00a0 8 | Nicaragua\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 | North America |\r\n|\u00a0 1 | Netherlands Antilles\u00a0\u00a0\u00a0\u00a0 | North America |\r\n|\u00a0 3 | Namibia\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 | Africa\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 |\r\n|\u00a0 5 | Niger\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 | Africa\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 |\r\n|\u00a0 7 | Nigeria\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 | Africa\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 |\r\n|\u00a0 9 | Niue\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 | Oceania\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 |\r\n|\u00a0 4 | New Caledonia\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 | Oceania\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 |\r\n|\u00a0 2 | Northern Mariana Islands | Oceania\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 |\r\n| 13 | Nauru\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 | Oceania\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 |\r\n| 14 | New Zealand\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 | Oceania\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 |\r\n|\u00a0 6 | Norfolk Island\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 | Oceania\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 |\r\n+----+--------------------------+---------------<\/pre>\n<\/blockquote>\n<p>Result is sorted by continent, as may be expected. We have aliased continent as <strong>&#8216;c&#8217;<\/strong>, and <strong>ORDER BY c<\/strong> went well. The quotes were meaningless here.<\/p>\n<h4>Question #2<\/h4>\n<blockquote>\n<pre>SELECT\r\n  MIN(Name) AS 'n',\r\n  Continent AS 'c'\r\nFROM\r\n  countries\r\nGROUP BY\r\n  c\r\n;\r\n+----------------------+---------------+\r\n| n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 | c\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 |\r\n+----------------------+---------------+\r\n| Nepal\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 | Asia\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 |\r\n| Netherlands\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 | Europe\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 |\r\n| Netherlands Antilles | North America |\r\n| Namibia\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 | Africa\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 |\r\n| Nauru\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 | Oceania\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 |\r\n+----------------------+---------------+<\/pre>\n<\/blockquote>\n<p>As with first query, the quoted <strong>&#8216;c&#8217;<\/strong> acts just like non-quoted <strong>c<\/strong>.<\/p>\n<h4>Question #3<\/h4>\n<blockquote>\n<pre>SELECT\r\n  MIN(Name) AS 'n',\r\n  Continent AS 'c'\r\nFROM\r\n  countries\r\nGROUP BY\r\n  'c'\r\n;\r\n+---------+---------------+\r\n| n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 | c\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 |\r\n+---------+---------------+\r\n| Namibia | North America |\r\n+---------+---------------+<\/pre>\n<\/blockquote>\n<p>Obviously something went wrong here. The query is valid, yet returns a single row. Why is that?<\/p>\n<p>What we&#8217;ve just done is to group by the <em>constant<\/em> <strong>&#8216;c&#8217;<\/strong>, not on the <em>alias<\/em> <strong>c<\/strong>. Results are complete wrong.<\/p>\n<p>This query is a simplified version of a true customer&#8217;s query. The <strong>GROUP BY<\/strong> clause included some <strong>5<\/strong> fields, the constant literal being one of them. The results of that query were used in very important business decision making. Well, decisions were misinformed&#8230;<\/p>\n<h4>Quoted aliases?<\/h4>\n<p>I&#8217;m not sure why anyone would want these. Why is this even a valid syntax?<\/p>\n<p>Multi-word aliases? Seems like wrong coding convention. But, if you must, do not use single quotes: use <em>`backticks`<\/em>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The quiz presented poses with an uncommon, though valid SQL syntax: one is allowed to use quoted name aliases. Thus, it is valid to write: SELECT Name AS &#8216;n&#8217;, Continent AS &#8216;c&#8217; FROM countries But what does the above mean? Let&#8217;s see the results of our three questions:<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[5],"tags":[21],"class_list":["post-3454","post","type-post","status-publish","format-standard","hentry","category-mysql","tag-sql"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2bZZp-TI","_links":{"self":[{"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/posts\/3454","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/comments?post=3454"}],"version-history":[{"count":12,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/posts\/3454\/revisions"}],"predecessor-version":[{"id":3624,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/posts\/3454\/revisions\/3624"}],"wp:attachment":[{"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/media?parent=3454"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/categories?post=3454"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/tags?post=3454"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}