title: When is GROUP BY required for aggregate functions?
body: <p>I have a table called <code>myEntity</code> as follows:</p>
  
  <pre><code>- id (PK INT NOT NULL)
  - account_id (FK INT NOT NULL)
  - key (INT NOT NULL.  UNIQUE for given account_id)
  - name (VARCHAR NOT NULL.  UNIQUE FOR given account_id)
  </code></pre>
  
  <p>I don't wish to expose the primary key <code>id</code> to the user, and added <code>key</code> for this purpose.  <code>key</code> kind of acts as an auto-increment column for a given <code>accounts_id</code> which will need to be manually done by the application.  I first planned on making the primary key composite <code>id-account_id</code>, however, the table is joined to other tables, and before I knew it, I had four columns in a table which could have been one.  While <code>account_id-name</code> does the same as <code>account_id-key</code>, <code>key</code> is smaller and will minimize network traffic when a client requests multiple records.  Yes, I know it isn't properly normalized, and while not my direct question, would appreciate any constructive criticism comments.</p>
  
  <p>Sorry for the rambling... When is GROUP BY required for an aggregate function?  For instance, what about the following?  <a href="https://stackoverflow.com/a/1547128/1032531">https://stackoverflow.com/a/1547128/1032531</a> doesn't show one.  Is it needed?</p>
  
  <pre><code>SELECT COALESCE(MAX(key),0)+1 FROM myEntity WHERE accounts_id=123;
  </code></pre>