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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:34:12+00:00 2026-05-29T22:34:12+00:00

In a sql server database, I am trying to return thegender with the most

  • 0

In a sql server database, I am trying to return thegender with the most about of money mad ethrough bonuses. I therefore have a sub query to calculate the sum of bonuses made by each gender, another subquery to return the max and finally, my main query returns the gender with this max. However my code is not working.

SELECT E.Gender, maxx.mm

   FROM HumanResources.Employee as E, 
   (SELECT MAX(Bonus) as mm 
    FROM (SELECT E.Gender, SUM(SP.Bonus) AS Bonus 
          FROM HumanResources.Employee AS E, Sales.SalesPerson AS SP
          GROUP BY E.Gender) 
     AS gen) AS  maxx

where E.Gender = gen.Gender;

The last line :where E.Gender = gen.Gender doesn’t seem to work as gen.Gender can’t be bound

Any help please

  • 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-05-29T22:34:13+00:00Added an answer on May 29, 2026 at 10:34 pm

    I am not a big fan of the subquery(subquery(subquery( model, as it is often wasteful and usually difficult to parse / understand / re-write. Here is how I initially wanted to re-write it when I saw it:

    SELECT TOP (1) E.Gender, mm = SUM(SP.Bonus)
      FROM Sales.SalesPerson AS sp 
      INNER JOIN HumanResources.Employee AS E
      ON E.EmployeeID = sp.EmployeeID -- guess on relationship
    GROUP BY E.Gender
    ORDER BY mm DESC;
    

    Another way (potentially more efficient depending on the plan, since you will eliminate all but one employee from the join – something that may happen above, but also might not):

    ;WITH sp AS
    (
      SELECT TOP 1 EmployeeID, mm = SUM(Bonus)
        FROM Sales.SalesPerson
        GROUP BY EmployeeID
        ORDER BY mm DESC
    )
    SELECT E.Gender, topsp.mm
      FROM sp
      INNER JOIN HumanResources.Employee AS E
      ON E.EmployeeID = sp.EmployeeID; -- again guessing on relationship
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a SQL Server 2005 database that I'm trying to access as a
I'm trying to drop a SQL Server database using the following code: SqlCommand command
I'm trying to read data from SQL Server database using Perl and the DBI
I'm trying to drop a SQL Server database from .NET code. I've tried using
I'm trying to test the connection of a GoDaddy SQL Server database. I'm getting
I am trying to zip files to an SQL Server database table. I can't
I've encountered this problem (while trying to add SQL Server Database (.mdf) file to
I am having difficulties when trying to insert files into a SQL Server database.
I'm using an Xml field in my Sql Server database table. I'm trying to
I am trying to optimize some stored procedures on a SQL Server 2000 database

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.