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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:59:03+00:00 2026-06-11T17:59:03+00:00

I have a Many-to-Many relationship between two tables. I’d like to sort the first

  • 0

I have a Many-to-Many relationship between two tables. I’d like to sort the first table by the first relationship with the second table and only return a single result from that table. This is on SQL Server. I’d like something like this:

SELECT a.retrieve_me
FROM table_A AS a
JOIN table_B AS b ON a.foo = b.foo
JOIN table_C AS c ON b.bar = c.bar
ORDER BY c.sort_me

Unfortunately it returns MN(k) results, where M is the count of “table_A” and N(k) is the number of relations of a single row k with “table_C.” To have it return just the results I wanted without post filtering I tried using DISTINCT on the SELECT clause and using TOP(SELECT COUNT(*) FROM table_A) but neither are valid syntax.

Any ideas? Hoping I can make this as performant as possible.

EDIT:

For clarity

table A
------------
"joe" 1
"betty" 2
"george" 3

table B
------------
1 2
1 3
2 3
2 4
3 1

table C
------------
1 "ashton"
2 "harding"
3 "spring"
4 "merry lane"

I’d like the results returned in the order of “george”, “joe”, and “betty” which is in the order (george -> ashton, joe -> harding, betty -> merry lane.)

  • 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-11T17:59:05+00:00Added an answer on June 11, 2026 at 5:59 pm

    If I understood correctly what you need, cause I think is very hard to follow you .. this should do it:

    SELECT a.nm
    FROM tablea a
    cross apply (select top 1 * 
             from tableb b
             join tablec c on b.id2 = c.id
             where a.id = b.id1
             order by c.nm) bc
    order by bc.nm
    

    http://sqlfiddle.com/#!3/661c0/5/0

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

Sidebar

Related Questions

I have a one to many relationship between two tables. The many table contains
I have a many-to-many relationship between two tables, let's say Friends and Foods. If
Is it possible to have a many to many relationship between two tables, and
android noob... I have two tables, with a one to many relationship between country_tbl
I have two tables, Books and Authors , with many-to-many relationship between them through
I have a many to many relationship between two existing tables (User: UserId, Name)
I have a 1 to many relationship between two tables... say Organisation and Members
I have a many-to-many relationship between two tables: Order and Item. I need to
I have a question about One-To-Many relationship between two tables when there is the
Is there a way to map one-to-many relationship between two tables that have the

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.