Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>Group By X</code> means <strong>put all those with the same value for X in the one group</strong>.</p> <p><code>Group By X, Y</code> means <strong>put all those with the same values for both X and Y in the one group</strong>.</p> <p>To illustrate using an example, let's say we have the following table, to do with who is attending what subject at a university:</p> <pre><code>Table: Subject_Selection Subject Semester Attendee --------------------------------- ITB001 1 John ITB001 1 Bob ITB001 1 Mickey ITB001 2 Jenny ITB001 2 James MKB114 1 John MKB114 1 Erica </code></pre> <p>When you use a <code>group by</code> on the subject column only; say:</p> <pre><code>select Subject, Count(*) from Subject_Selection group by Subject </code></pre> <p>You will get something like:</p> <pre><code>Subject Count ------------------------------ ITB001 5 MKB114 2 </code></pre> <p>...because there are 5 entries for ITB001, and 2 for MKB114</p> <p>If we were to <code>group by</code> two columns:</p> <pre><code>select Subject, Semester, Count(*) from Subject_Selection group by Subject, Semester </code></pre> <p>we would get this:</p> <pre><code>Subject Semester Count ------------------------------ ITB001 1 3 ITB001 2 2 MKB114 1 2 </code></pre> <p>This is because, when we group by two columns, it is saying <strong>"Group them so that all of those with the same Subject and Semester are in the same group, and then calculate all the aggregate functions</strong> (Count, Sum, Average, etc.) <strong>for each of those groups"</strong>. In this example, this is demonstrated by the fact that, when we count them, there are <strong>three</strong> people doing ITB001 in semester 1, and <strong>two</strong> doing it in semester 2. Both of the people doing MKB114 are in semester 1, so there is no row for semester 2 (no data fits into the group "MKB114, Semester 2")</p> <p>Hopefully that makes sense.</p>
 

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