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 8051453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:18:26+00:00 2026-06-05T07:18:26+00:00

Could anyone explain to me the difference between the SQL statements below? I can

  • 0

Could anyone explain to me the difference between the SQL statements below? I can see there is a difference but I can’t nail down the exact conditions which can make them produce different results. By the way, I’m thinking the distinct clause doesn’t make a difference on the user.id field since all the ids are already unique. The purpose of the query is to count the number of unique (non-empty) last names. If the last name is empty, then count as unique.

I suppose the general case for this problem would be the use of an aggregate function within a case-when statement.

Count within Case-When:

SELECT 
    (case when (substr(u.name,40,40) <> '                                        ')
        then count(distinct(substr(u.name,40,40)))
        else count(u.id) 
    end) as "LAST_NAME",
FROM 
    users u
GROUP BY
    substr(u.name,40,40)

Case-When within Count:

SELECT 
    count (distinct case when (substr(u.name,40,40) <> '                                        ')
        then substr(u.name,40,40)
        else to_char(u.id)
    end) as "LAST_NAME",
FROM 
    users u
GROUP BY
    substr(u.name,40,40)
  • 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-05T07:18:27+00:00Added an answer on June 5, 2026 at 7:18 am

    If user.id is a PRIMARY KEY, these queries are identical semantically, though they are likely to produce different execution plans.

    They will return 1 for all non-empty last names, since you are counting distinct values of the group-by expession within its group, which, by definition, will be exactly one.

    For empty last names, the first query will essentialy return COUNT(u.id) and the second one will return COUNT(DISTINCT TO_CHAR(u.id)), which, given that u.id is unique, is the same.

    I believe you need to remove GROUP BY from the second query:

    SELECT  count (distinct case when (substr(u.name,40,40) <> '                                        ')
            then substr(u.name,40,40)
            else to_char(u.id)
        end) as "LAST_NAME",
    FROM 
        users u
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may be a newbie question, but could anyone please explain the difference between
Could anyone please explain with examples difference between monolithic and micro kernel? Also other
Could anyone explain the difference between filter and filter_by functions in SQLAlchemy? Which one
Could someone please explain the significant difference in speed between a firefox updatepanel async
Could anyone explain differences between error_reporting(E_ALL); and error_reporting(E_ALL & ~E_NOTICE); ? I noticed that
Could anyone explain to me the differences between multi-CPU, multi-core, and hyper-thread? I am
What is difference between setFlags and addFlags for intent. Could any one explain it
Could anyone explain how Java executes this code? I mean the order of executing
Could anyone explain me what information should I put from this URL: http://webservices.amazon.com/onca/xml? Service=AWSECommerceService&
Could anyone explain why $property is 'name' outside of the function and null inside?

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.