Warning: Creating default object from empty value in /www/htdocs/v030397/mysql-qotd/wp-content/plugins/sitepress-multilingual-cms/sitepress.class.php on line 4991

Warning: Creating default object from empty value in /www/htdocs/v030397/mysql-qotd/wp-content/plugins/sitepress-multilingual-cms/sitepress.class.php on line 4993
More uncommon SQL – MySQL Question of the Day

Skip to content

More uncommon SQL

25. July 2010
By plogi in general

Hi again!
The query below uses quite a bit of strange looking SQL syntax:

select all
  count(1) minus
union all
  select 1 only
order by 1 limit 1  ;

+-------+
| minus |
+-------+
|     1 |
+-------+
1 row in set (0.00 sec)


 
The “ALL” in the “SELECT ALL” is actually the default – so you hardly ever see it.
“COUNT(1)” is mostly used with Oracle, where it may have a performance benefit over “COUNT(*)”. The result however is the same than “COUNT(*)”.
The “minus” and “only” are column aliases.
Last but not least, the “ORDER BY 1″ means, that we order by the first column.

Below the same slightly more readable:

select
  count(*) as cnt
union all
  select 1
order by cnt limit 1  ;

Comment Feed

No Responses (yet)

You must be logged in to post a comment.