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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T21:50:52+00:00 2026-05-19T21:50:52+00:00

The goal of this SQL query is to return the user with the most

  • 0

The goal of this SQL query is to return the user with the most fight wins AND a count of the wins.

I have two tables: users and fights.

User has many fights
Fight belongs to user

Fight has the following columns of concern:

  • challenger_id (user_id fk)
  • challengee_id (user_id fk)
  • challenger_won (boolean)

As you can see, a user can be a challenger or a challengee, but not both.

  • If the user is a challenger and the challenger_won = true, then it is considered a win.
  • If the user is a challengee and the challenger_won = false, then it is considered a win.
  • If the challenger_won = null, then just disregard it.

How would I write the SQL for this?

  • 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-19T21:50:52+00:00Added an answer on May 19, 2026 at 9:50 pm

    What about this?

    SELECT a.fighter, COUNT(*) AS wins
      FROM (SELECT challenger_id AS fighter
              FROM fights
             WHERE challenger_won = TRUE
            UNION ALL
            SELECT challengee_id AS fighter
              FROM fights
             WHERE challenger_won = FALSE
           ) AS a
     GROUP BY a.fighter;
    

    If challenger_won is NULL, both queries in the UNION will ignore the row, as required.

    If you don’t like UNION ALL, you can use, instead:

    SELECT a.fighter, SUM(a.wins) AS wins
      FROM (SELECT challenger_id AS fighter, 'R' AS role, COUNT(*) AS wins
              FROM fights
             WHERE challenger_won = TRUE
             GROUP BY fighter, role
            UNION
            SELECT challengee_id AS fighter, 'E' AS role, COUNT(*) AS wins
              FROM fights
             WHERE challenger_won = FALSE
             GROUP BY fighter, role
           ) AS a
     GROUP BY a.fighter;
    

    The first part of the UNION generates rows with ‘R’ as role (for ‘challengeR won’) and the fighter ID and a count of the fights won. The second part generates rows with ‘E’ as role (for ‘challengeE won’) and the fighter ID and a count of the fights won. The role is necessary in case some fighter won 3 fights as challenger and 3 fights as challengee; without the role, the two entries with the same fighter and count would be collapsed into one. You then sum the wins for each fighter in each role.

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

Sidebar

Related Questions

I'm wondering if this is possible in SQL. Say you have two tables A
I have a view defined in SQL server 2008 that joins 4 tables together.
Suppose i have this sql statement and I have executed a sql command to
I am using Entity framework on a forum website and have two query examples
My class relationship: ClassMaster 1----* ClassSessions My Goal: Return all ClassMasters that have ClassSessions
This is a resource-allocation problem. My goal is to run a query to fetch
My Goal I would like to have a main processing thread (non GUI), and
The goal: To create a .NET dll i can reference from inside SQL Server
The goal: Any language. The smallest function which will return whether a string is
I have a SQL 2005 table consisting of around 10million records (dbo.Logs). I have

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.