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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:34:51+00:00 2026-06-03T10:34:51+00:00

Why is the result of the two query the same? ( I’m using northwind

  • 0

Why is the result of the two query the same? (I’m using northwind database.)

SELECT      ContactTitle
        ,   COUNT(CustomerID) AS NumberOfCustomer
FROM        dbo.Customers
WHERE       ContactTitle LIKE '%sales%'
GROUP BY    ContactTitle
HAVING      COUNT(*) >= 5
ORDER BY    NumberOfCustomer desc

and

SELECT 
DISTINCT    ContactTitle
        ,   COUNT(CustomerID) AS NumberOfCustomer
FROM        dbo.Customers
WHERE       ContactTitle LIKE '%sales%'
GROUP BY    ContactTitle
HAVING      COUNT(*) >= 5
ORDER BY    NumberOfCustomer desc

the result is:

ContactTitle           NumberOfCustomer
---------------------  ----------------
Sales Representative         17
Sales Manager                11
Sales Associate               7
Sales Agent                   5

In my own understanding, the second query gets the distinct titles and count its records so i’m expecting that the result would be none because each title has only record count of 1. Am i right?

  • 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-03T10:34:54+00:00Added an answer on June 3, 2026 at 10:34 am

    This is how the query execution works. In your second statement, DISTINCT doesn’t do any additional function because your GROUP BY containing the same column name ContactTitle has already carried out that operation for you.

    1. FROM
    2. WHERE
    3. GROUP BY <-- You have specified the column `ContactTitle`, 
    -- which means the results would be grouped by that column to product unique 
    --result.
    4. HAVING
    5. SELECT <-- Adding DISTINCT on ContactTitle column here doesn't make much 
    -- difference and it is actually redundant. DISTINCT is applied to the whole
    -- row but the resultset already contains distinct rows grouped by the column 
    -- `ContactTitle`.
    6. ORDER BY
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

These two querys gives me the exact same result: select * from topics where
The following two queries return the same (expected) result when I query my database:
i have to select the common coloumns c1 ,c2,c3 from result of two sql
Is it available to write a query to use same LIMIT (from), (count), but
I have two queries serving the same purpose. SELECT * FROM #user INNER JOIN
I'm using this simple SQL query to retrieve data from two tables in my
I have an sql query that returns rows from two tables with same column
In a SQL Server 2005 database I'm working on this query: select * from
I have two queries that are basically the same: OLD TRANSACTIONS QUERY SELECT t.payment_method,
From what I gather the following two will bring back the same result FB.api

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.