<p>You may use basic <a href="https://www.thoughtco.com/introduction-to-sql-1019778" data-component="link" data-source="inlineLink" data-type="internalLink" data-ordinal="1">SQL queries</a> to <a href="https://www.thoughtco.com/what-is-sql-1019769" data-component="link" data-source="inlineLink" data-type="internalLink" data-ordinal="2">retrieve data from a database</a> but this often doesn’t provide enough intelligence to meet business requirements. SQL also provides you with the ability to group query results based upon row-level attributes in order to apply aggregate functions using the GROUP BY clause. Consider, for example, an order data table consisting of the attributes below:</p><ul><li> <b>OrderID</b> – a numeric value uniquely identifying each order. This field is the <a href="https://www.thoughtco.com/primary-key-definition-1019179" data-component="link" data-source="inlineLink" data-type="internalLink" data-ordinal="3">primary key</a> for the database.</li><li> <b>Salesperson</b> – a text value providing the name of the salesperson who sold the products. This field is a <a href="https://www.thoughtco.com/foreign-key-definition-1019255" data-inlink="6Ga0osPZ-Z1r4_PEErh9cw&#61;&#61;" data-component="link" data-source="inlineLink" data-type="internalLink" data-ordinal="4">foreign key</a> to another table containing personnel information.</li><li> <b>CustomerID</b> – a numeric value corresponding to a customer account number. This field is also a foreign key, referencing a table containing customer account information.</li><li> <b>Revenue</b> – a numeric value corresponding to the dollar amount of the sale.</li></ul>When it comes time to conduct performance reviews for salespeople, the Orders table contains valuable information that may be used for that review. When evaluating Jim, you could, for example, write a simple query that retrieves all of Jim’s sales records: <pre><code> SELECT *\n FROM Orders\n WHERE Salesperson LIKE ‘Jim’\n </code></pre> This would retrieve all records from the database corresponding to sales made by Jim: <pre><code> OrderID Salesperson CustomerID Revenue\n 12482 Jim 182 40000\n 12488 Jim 219 25000\n 12519 Jim 137 85000\n 12602 Jim 182 10000\n 12741 Jim 155 90000\n </code></pre> You could review this information and perform some manual calculations to come up with performance statistics, but this would be a tedious task that you would have to repeat for each salesperson in the company. Instead, you can replace this work with a single GROUP BY query that calculates statistics for each salesperson in the company. You simply write the query and specify that the database should group the results based upon the Salespeson field. You may then use any of the SQL aggregate functions to perform calculations on the results.<br/><br/>Here’s an example. If you executed the following SQL statement: <pre><code> SELECT Salesperson, SUM(Revenue) AS ‘Total’, MIN(Revenue) AS ‘Smallest’, MAX(Revenue) AS ‘Largest’, AVG(Revenue) AS ‘Average’, COUNT(Revenue) AS ‘Number’\n FROM Orders\n GROUP BY Salesperson\n </code></pre> You would get the following results: <pre><code> Salesperson Total Smallest Largest Average Number\n Jim 250000 10000 90000 50000 5\n Mary 342000 24000 102000 57000 6\n Bob 118000 4000 36000 39333 3\n </code></pre> As you can see, this powerful function allows you to generate small reports from within a SQL query, providing valuable business intelligence to the manager conducting the performance reviews. The GROUP BY clause is often used in databases for this purpose and is a valuable tool in the DBA’s bag of tricks.