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

Warning: Cannot modify header information - headers already sent by (output started at /www/htdocs/v030397/mysql-qotd/wp-content/plugins/sitepress-multilingual-cms/sitepress.class.php:4991) in /www/htdocs/v030397/mysql-qotd/wp-includes/feed-rss2-comments.php on line 8
Comments on: Question 75: What’s the result of the query? http://mysql-qotd.casperia.net/archives/501 mysql 5.0/5.1 questions for learning purposes Fri, 06 Aug 2010 16:56:36 +0000 http://wordpress.org/?v=abc hourly 1 By: plogi http://mysql-qotd.casperia.net/archives/501/comment-page-1#comment-85 plogi Sun, 27 Jun 2010 10:50:47 +0000 http://mysql-qotd.casperia.net/?p=501#comment-85 <b> Answer b is correct. But why? This query has two subqueries in the FROM-clause: a and b. Subquery b results in <small><pre> +---+ | a | +---+ | 1 | +---+ </pre></small> And a results in <small><pre> +---+ | b | +---+ | 1 | | 2 | +---+ </pre></small> We select column a from subquery b and give it the alias b. This explains the 'b' as the column name in the result. The subqueries are joined together without any restriction: There is no on-clause or where-clause. That means, the result has 1 * 2 rows (cartesian product join). This query is syntactically a bit confusing, but I hope it helps understanding subqueries, aliases and joins.</b> <pre> MariaDB [mysql]> select a b from (select 1 a) b, (select 1 b union select 2) a; a) +---+ | b | +---+ | 1 | | 1 | +---+ 2 rows in set (0.00 sec) </pre>
Answer b is correct. But why?

This query has two subqueries in the FROM-clause: a and b.
Subquery b results in

+---+
| a |
+---+
| 1 |
+---+


And a results in

+---+
| b |
+---+
| 1 |
| 2 |
+---+


We select column a from subquery b and give it the alias b.
This explains the ‘b’ as the column name in the result.
The subqueries are joined together without any restriction:
There is no on-clause or where-clause. That means, the result has 1 * 2 rows (cartesian product join).
This query is syntactically a bit confusing, but I hope it helps understanding subqueries, aliases and joins.

MariaDB [mysql]> select a b from (select 1 a) b, (select 1 b union select 2) a;
a)
+---+
| b |
+---+
| 1 |
| 1 |
+---+
2 rows in set (0.00 sec)
]]>