Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8615941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:30:07+00:00 2026-06-12T05:30:07+00:00

In the article Why Arel? , the author poses the problem: Suppose we have

  • 0

In the article Why Arel?, the author poses the problem:

Suppose we have a users table and a photos table and we want to select all user data and a *count* of the photos they have created.

His proposed solution (with a line break added) is

SELECT users.*, photos_aggregation.cnt
FROM users
LEFT OUTER JOIN (SELECT user_id, count(*) as cnt FROM photos GROUP BY user_id)
  AS photos_aggregation
ON photos_aggregation.user_id = users.id

When I attempted to write such a query, I came up with

select users.*, if(count(photos.id) = 0, null, count(photos.id)) as cnt
from users
left join photos on photos.user_id = users.id
group by users.id

(The if() in the column list is just to get it to behave the same when a user has no photos.)

The author of the article goes on to say

Only advanced SQL programmers know how to write this (I’ve often asked this question in job interviews I’ve never once seen anybody get it right). And it shouldn’t be hard!

I don’t consider myself an “advanced SQL programmer”, so I assume I’m missing something subtle. What am I missing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T05:30:08+00:00Added an answer on June 12, 2026 at 5:30 am

    In most DBMSs (MySQL and Postgres are exceptions) the version in your question would be invalid.

    You would need to write the query which does not use the derived table as

    select users.*, CASE WHEN count(photos.id) > 0 THEN count(photos.id) END as cnt
    from users
    left join photos on photos.user_id = users.id
    group by users.id, users.name, users.email /* and so on*/
    

    MySQL allows you to select non aggregated items that are not in the group by list but this is only safe if they are functionally dependant on the column(s) in the group by.

    Whilst the group by list is more verbose without the derived table I would expect most optimisers to be able to transform one to the other anyway. Certainly in SQL Server if it sees you are grouping by the PK and some other columns it doesn’t actually do group by comparisons on those other columns.

    Some discussion about this MySQL behaviour vs standard SQL is in Debunking GROUP BY myths

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

one Article has many Comments, and i want to fetch all Articles that have
The author of this article states that Normally you don't want to have access
I have an article based website where users can login, post articles etc. The
I have three models Article , Author , and AuthorLine presenting relationship between article
I have an article controller, which manages all article relations like authors, translations, related
Tables: Article , Author , Comment (1 article and 1 author can have *
The article here mentions the use of Data Annotations Model Binder that is available
article = get_object_or_404(Article,slug=slug) categories = article.category.all() Using render_to_response() , how can I use the
This article I was following above guide. The management have changed since it was
wikipedia article on C states that Function and data pointers permit ad hoc run-time

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.