title: How to return result of a SELECT inside a function in PostgreSQL?
body: <p>I have this function in PostgreSQL, but I don't know how to return the result of the query:</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION wordFrequency(maxTokens INTEGER)
    RETURNS SETOF RECORD AS
  $$
  BEGIN
      SELECT text, count(*), 100 / maxTokens * count(*)
      FROM (
          SELECT text
      FROM token
      WHERE chartype = 'ALPHABETIC'
      LIMIT maxTokens
      ) as tokens
      GROUP BY text
      ORDER BY count DESC
  END
  $$
  LANGUAGE plpgsql;
  </code></pre>
  
  <p>But I don't know how to return the result of the query inside the PostgreSQL function.</p>
  
  <p>I found that the return type should be <code>SETOF RECORD</code>, right? But the return command is not right.</p>
  
  <p>What is the right way to do this?</p>