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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:18:26+00:00 2026-05-27T12:18:26+00:00

I have a table with two columns: a couple id and a number of

  • 0

I have a table with two columns: a couple id and a number of “marks” for that
couple. I’d like a result which lists the number of couples which have x marks
or more for each of the values of x. So my input looks like:

| couple_id | num_marks |
|-----------+-----------|
|         9 |         7 |
|         6 |         6 |
|         8 |         6 |
|         2 |         5 |
|         3 |         4 |
|         5 |         4 |
|         1 |         3 |
|         4 |         3 |
|        10 |         2 |
|         7 |         1 |

And I’d like to get the result:

| num_marks | num_couples |
|-----------+-------------|
|         7 | 1           |
|         6 | 3           |
|         5 | 4           |
|         4 | 6           |
|         3 | 8           |
|         2 | 9           |
|         1 | 10          |

I.e. there was 1 couple with 7 or more marks, 3 couples with 6 or more marks, 4
couples with 5 or more marks, etc. I’ve been able to come up with a query to
return the number of couples with exactly n marks:

SELECT num_marks,
       count(couple_id) AS num_couples
  FROM table_name
  GROUP BY num_marks
  ORDER BY num_marks DESC;

Which yields:

| num_marks | num_couples |
|-----------+-------------|
|         7 |           1 |
|         6 |           2 |
|         5 |           1 |
|         4 |           2 |
|         3 |           2 |
|         2 |           1 |
|         1 |           1 |

I.e. there was 1 couple with 7 marks, 2 couples with 6 marks, 1 with 5, etc. Is
there a convenient way effectively to sum the value of each row with those above
it? I can do it at the application level, but it seems like the kind of thing
which really belongs in the database.

  • 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-27T12:18:27+00:00Added an answer on May 27, 2026 at 12:18 pm

    This might not be particularly efficient but should get the job done:

    SELECT t1.num_marks,       
      (SELECT count(t2.couple_id)
       FROM table_name t2  
       WHERE t2.num_marks >= t1.num_marks
       ) AS num_couples 
    FROM table_name t1  
    GROUP BY t1.num_marks   
    ORDER BY t1.num_marks DESC;
    

    Edit :
    You can use a sub query in the select, from, where, group by and having clauses of a query, and if you reference the main / outer ‘query’ then it will evaluate the subquery for each row, then it is known as a correlated subquery. (Hence the caveat about performance)

    As per Damien’s answer, you could also use a CTE – CTE’s can improve readability and also make recursion and self-joins a lot easier IMO.

    AFAIK subqueries are supported in most SQL’s.

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

Sidebar

Related Questions

I have a table that has two columns: CommunityID PersonID And A People table
Say I have an MSSQL table with two columns: an int ID column that's
Suppose I have a tags table with two columns: tagid and contentid . Each
I have a table y Which has two columns a and b Entries are:
I have a table that holds only two columns - a ListID and PersonID.
I have a table in which there are two columns : 1. import type,
I have a table called Courses with two columns dept_code and course_code that are
I have a table with two columns: City Country I want a result set
We have a table with two columns: in each row there is a label
I have a table with two columns and one row, and 100% width across

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.