Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does the outer query return nothing if second subquery returns no results?

I’m trying to figure out why the outer query returns nothing if the second sub query has no return value even if the first does?

enter image description here

SELECT * FROM
(
    SELECT category_id
    FROM story_category
    WHERE story_category.story_id = 9998
) AS c,
(
    SELECT typology_id
    FROM story_typology
    WHERE story_typology.story_id = 9998
) AS t;

If I pick another story_id for the second query which returns a typology_id only then does the main query return a results for both subquery:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

>Solution :

Having two elements in the FROM list means a Cartesian product or cross join, that is, each row from the left side is combined with each row from the right side. If the left side has n rows and the right side has m rows, the result will have n*m rows.

Consequently, if one of the sets is empty, the result is also empty.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading