HAVING vs. Nested Query

  • 30/10/2023
  • SQL
No Comments

“Find all cities with more than two customers living in the city.”

SELECT customer_city, COUNT(*) AS num_customers

FROM customer GROUP BY customer_city

HAVING COUNT(*) > 2;

  • Or, can write:

SELECT customer_city, num_customers

FROM (SELECT customer_city, COUNT(*)

FROM customer GROUP BY customer_city)

AS counts (customer_city, num_customers)

WHERE num_customers > 2;

🞑 Grouping and aggregation is computed by inner query

🞑 Outer query selects desired results generated by inner query

About us and this blog

We are a digital marketing company with a focus on helping our customers achieve great results across several key areas.

Request a free quote

We offer professional SEO services that help websites increase their organic search score drastically in order to compete for the highest rankings even when it comes to highly competitive keywords.

Subscribe to our newsletter!

More from our blog

See all posts
No Comments

Recent Posts