Google News
logo
PostgreSQL - Interview Questions
How Does GROUP BY Work in PostgreSQL?
Knowing the GROUP BY clause can demonstrate a slightly more advanced knowledge of SQL. Thus, you may be asked how the GROUP BY clause works in PostgreSQL.
 
The GROUP BY clause allows you to group the data that results from your queries. Organizing data into groups is useful in making sense of data and in the use of aggregate functions.
 
Imagine you have an eCommerce website that sells several product types. In your database, you have a table that stores information about the inventory you have in stock. If you want to find the count of each product type, you can use GROUP BY with the COUNT() aggregate function. (Note: Aggregate functions are covered in more detail in the next section.) Here’s what the query would look like:
SELECT product_type, COUNT(product_id)
FROM stock
GROUP BY product_type
Advertisement