Database Essentials using Postgres - Writing Basic SQL Queries - Performing Aggregations

Performing Aggregations

This article explores how to aggregate data using SQL commands. It covers global aggregations and aggregations by key, providing practical examples to help readers understand and apply the concepts discussed.

Explanation for the video


Key Concepts Explanation

Global Aggregations

  • Get total number of orders.
  • Get revenue for a given order id.
  • Get number of records with order_status either COMPLETED or CLOSED.

Aggregations by Key - Using GROUP BY

  • Get number of orders by date or status.
  • Get revenue for each order_id.
  • Get daily product revenue (using order date and product id as keys).

Using HAVING Clause

  • Apply filtering on aggregated data.
  • Get daily product revenue where revenue is greater than $500.

Rules for Using GROUP BY

  • Understand the rules and limitations when using GROUP BY in SQL commands.

Hands-On Tasks

  1. Perform a global aggregation to get the total number of orders.
  2. Find the count of distinct order dates in the dataset.
  3. Retrieve order items for a specific order_id.
  4. Calculate the total revenue for a specific order_id.
  5. Count the number of orders with status COMPLETED or CLOSED.
  6. Group the orders by date and calculate the count.
  7. Group the orders by status and calculate the count.
  8. Calculate the order revenue for each order_id.
  9. Calculate the daily product revenue by date and product id.
  10. Apply filtering to get daily product revenue greater than $500.


In conclusion, this article delved into the world of aggregating data using SQL commands, exploring various key concepts and hands-on tasks to solidify the understanding. Practice these tasks and engage with the community for further learning opportunities.

Performing Aggregations

The article provides in-depth explanations and practical tasks to guide the readers in performing data aggregations using SQL commands effectively. Enjoy learning and applying these concepts!

Watch the video tutorial here