Curious Case of ``` Case``` Statement

Curious Case of ``` Case``` Statement

Table of contents

Introduction

  1. In order to use non-aggregated columns in the group-by clause we need to mention them compulsorily in the group-by clause.

  2. For a strict database like PostgreSQL, we will get an error.

  3. A case statement can be used along with group by clause in two ways

    • Either we mention the case statement in the aggregate column, in this case, even if it is not mentioned in the group by clause we will not get an error

    • But if we use case statement in the select clause then all the columns used in the case statement must also be present in the group by clause.

    • This is because the order of execution of group by clause and the select query is the same

  4. Hence we mention case statement which is not inside an aggregated column we need to specify in the group by clause