Data Engineering using Spark SQL - Basic Transformations - Aggregating Data

In this article, we will delve into the concept of aggregating data using Spark SQL. The video linked here provides a comprehensive visual guide to understanding data aggregation.

[Link to the video will be placed here as a placeholder]

Key Concepts Explanation

Global Aggregations

Global aggregations involve computing metrics that span the entire dataset. This includes tasks like getting the total number of orders, revenue for a specific order ID, and count of records with order status ‘COMPLETED’ or ‘CLOSED’.

Aggregations by Key - using GROUP BY

Aggregations by key involve grouping data based on certain keys such as date, status, or product ID. Examples include getting the number of orders by date, revenue for each order ID, and daily product revenue using date and product ID as keys.

Filtering with HAVING Clause

The HAVING clause is used to filter aggregated data based on specified conditions. For instance, getting daily product revenue where revenue exceeds $500.

Rules with GROUP BY

Rules include having grouped columns in the SELECT clause, using aggregate functions for derived columns, and not using non-aggregate functions on derived columns. Filtering based on aggregated results is done using the HAVING clause.

Hands-On Tasks

  1. Get the total count of orders.
  2. Compute the total revenue for a specific order ID.
  3. Group orders by date and count the number of orders per date.
  4. Compute revenue for each order item, grouped by order ID.
  5. Filter daily product revenue where revenue is greater than $500.


This article explored the fundamental concepts of aggregating data using Spark SQL. By practicing the provided hands-on tasks and referencing the video guide, readers can deepen their understanding of data aggregation techniques. Remember to engage with the community for further learning opportunities.

Using Spark SQL with Python or Scala:

# Perform Spark SQL queries in Python or Scala
spark.sql("SELECT count(order_id) FROM orders").show()
spark.sql("SELECT count(DISTINCT order_date) FROM orders").show()

# Example of a more complex query
SELECT round(sum(order_item_subtotal), 2) AS order_revenue
FROM order_items 
WHERE order_item_order_id = 2

Feel free to run these queries to practice data aggregation with Spark SQL.

Watch the video tutorial here