Lesson 14/100

Tutorials PostgreSQL Tutorial

HAVING — Complete Guide

HAVING — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of PostgreSQL Tutorial on Toolliyo Academy.

On this page

PostgreSQL Tutorial · Lesson 14 of 100

HAVING

SQLAdvanced

SQL · 1 — Queries · ~6 min · PostgreSQL — SQL & Queries

What is this?

HAVING filters groups after aggregation, unlike WHERE which filters raw rows. Use it when the condition involves COUNT, SUM, or other aggregate results.

Why should you care?

Marketing wants Swiggy restaurants with more than 500 orders this week — HAVING COUNT(*) > 500 after GROUP BY restaurant_id.

See it live — copy this example

Run in pgAdmin or psql.

SELECT restaurant_id, COUNT(*) AS orders_this_week
FROM orders
WHERE created_at >= date_trunc('week', now())
GROUP BY restaurant_id
HAVING COUNT(*) >= 100
ORDER BY orders_this_week DESC;

What happened?

  • WHERE trims rows to this week first.
  • GROUP BY buckets by restaurant.
  • HAVING drops restaurants under 100 orders.
  • The result is high-volume partners only.

Practice next

  1. Ensure orders has restaurant_id and created_at columns.
  2. Run the query and note fewer rows than GROUP BY without HAVING.
  3. Change threshold to 10 and compare counts.
  4. HAVING SUM(total_amount) > 50000 for high-GMV restaurants.
  5. Combine HAVING COUNT(*) >= 100 AND AVG(total_amount) > 300.

Remember

HAVING filters summarized groups. WHERE filters individual rows before grouping. Both can appear in one query with different jobs.

PostgresVerse partner tiering

Ops promotes restaurants with HAVING COUNT >= 100 to gold tier for faster payouts.

Outcome: SQL encodes tier rules instead of exporting CSVs to Excel.

Interview prep for this lesson

Practice these questions aloud after reading—each links to a full structured answer.

Junior Detailed
Explain SQL queries in the context of PostgreSQL.
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define SQL queri…
Mid Detailed
What are common mistakes teams make with Schema design when using PostgreSQL?
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define Schema de…
Senior Detailed
How would you debug a production issue related to Transactions in a PostgreSQL application?
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define Transacti…
Mid Detailed
Compare two approaches to Indexing—when would you choose each?
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define Indexing…
Junior Detailed
Describe a real-world scenario where Normalization mattered in a PostgreSQL project.
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define Normaliza…
Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

PostgreSQL Tutorial
Course syllabus

PostgreSQL Tutorial

PostgreSQL — Foundations
PostgreSQL — SQL & Queries
PostgreSQL — Indexing & Performance
PostgreSQL — Transactions & MVCC
PostgreSQL — Functions & Automation
PostgreSQL — JSONB & Modern Features
PostgreSQL — Replication & High Availability
PostgreSQL — Security & Cloud
PostgreSQL — Monitoring & Troubleshooting
PostgreSQL — Real-World Projects
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details