Member-only story

SQL Aggregation: Having vs Where Clause

Discussing the difference between where and having a clause with aggregate function

Suraj Mishra
3 min readJun 18, 2023

Introduction

  • SQL provides different types of clauses such as where, having, like, etc. These clauses help us to filter data, perform aggregation and analyze the data.
  • In this article, we will discuss where & having clauses along with aggregation function. We will also cover the difference between them.

Table

  • I created a sample table called book_reviews for this tutorial which looks like below

Table description

  • It contains basic columns for book reviews such as user_id, isbn, and rating.
postgres=# \d book_reviews
Table "public.book_reviews"
Column | Type | Collation | Nullable | Default
-------------+---------+-----------+----------+--------------------------------------------------
reviews_id | integer | | not null | nextval('book_reviews_reviews_id_seq'::regclass)
user_id | text | | |
isbn | text | | |
book_rating | text | | |
Indexes:
"book_reviews_pkey" PRIMARY KEY, btree (reviews_id)

--

--

Suraj Mishra
Suraj Mishra

Written by Suraj Mishra

Staff Software Engineer @PayPal ( All opinions are my own and not of my employer )

No responses yet