Which of the following is true ? i) Having clause exists only with GROUP…

2025

Which of the following is true ?

i) Having clause exists only with GROUP Clause.

ii) The Group clause was added to SQL because the where keyword could not be used with aggregate functions.

  1. A.

    i) only

  2. B.

    ii) only

  3. C.

    i) and ii) only

  4. D.

    neither i) nor ii)

Attempted by 5 students.

Show answer & explanation

Correct answer: D

Concept

HAVING filters GROUPS by an aggregate condition, but it does not itself require a GROUP BY clause — when GROUP BY is absent, SQL treats the whole result set as one implicit group and evaluates HAVING against that single group. GROUP BY's own job is to partition rows so an aggregate function can be computed per group. The reason WHERE cannot filter on an aggregate result is SQL's logical processing order: WHERE runs on raw rows before any grouping or aggregation happens, so no aggregate value exists yet at that stage — HAVING was introduced to close exactly that gap, evaluated after grouping/aggregation, not GROUP BY.

Application

  • Statement i) claims HAVING exists only alongside a GROUP BY clause. This is false: a query such as SELECT COUNT(*) FROM Orders HAVING COUNT(*) > 100; is valid with no GROUP BY at all — the entire table forms one implicit group and HAVING filters that single group.

  • Statement ii) claims the GROUP BY clause was added because WHERE could not be used with aggregate functions. This misattributes the reasoning — the WHERE-versus-aggregate limitation is exactly the documented justification for HAVING's existence, not GROUP BY's. GROUP BY's own purpose is partitioning rows for per-group aggregation, independent of that WHERE limitation.

Cross-check

SQL's logical execution order is FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY. WHERE always runs before any aggregation, and the gap that creates is closed at the HAVING stage — never by GROUP BY. Comparing the two forms below confirms HAVING's independence from GROUP BY:

-- HAVING with GROUP BY (per-group filter)
SELECT Customer, SUM(OrderPrice)
FROM Orders
GROUP BY Customer
HAVING SUM(OrderPrice) < 2000;

-- HAVING alone, no GROUP BY (single implicit group)
SELECT COUNT(*)
FROM Orders
HAVING COUNT(*) > 100;

Since HAVING can run stand-alone against one implicit group, and GROUP BY is not the clause that answers WHERE's aggregate limitation, both statements fail together — the combination that holds is “neither i) nor ii)”.

Explore the full course: Cognizant Preparation