Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat happens if I select two tables with no WHERE clause?
    primarykey
    data
    text
    <p>I had a technical interview last week, and my interviewer asked me what happens if I run the following query:</p> <p><code>SELECT * FROM tbl1, tbl2</code></p> <p>I think I answered it correctly, but it wasn't an in-depth answer. </p> <p>I said that I would select all the columns in both tables. For example if tbl1 has 3 columns, and tbl2 has 4 columns. The result set would have 7 columns. </p> <p>Then he asked me why 7? and I said because I was selecting everything from each table. </p> <p>That was a bad answer, but I couldn't think of anything else. </p> <p>To cut to the chase, after the interviewed I executed the latter statement using two tables. </p> <p>Table A, had 3 animal: dog, cat and elephant.</p> <p>Table B had 2 names: Mat and Beth</p> <p>This is the result set that I got after the statement being executed:</p> <pre><code>********************************************* | id_tbl1 | name_tbl1 | id_tbl2 | name_tbl2 | ********************************************* | 1 | dog | 1 | Mat | | 2 | cat | 1 | Mat | | 3 | elephant | 1 | Mat | | 1 | dog | 2 | Beth | | 2 | cat | 2 | Beth | | 3 | elephant | 2 | Beth | ********************************************* </code></pre> <p>So my question is, why does the statement behaves like that? </p> <p>In other words:</p> <p>Why does the Table B's records repeat themselves until I reach the end of table A, and then it starts all over again? </p> <p>How would you have answered the question in a way that it would've "WOW'd" the interviewer? </p> <p>If this question does not belong to SO, feel free to delete it or close it!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload