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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:15:07+00:00 2026-06-12T22:15:07+00:00

I am trying to write a SQL statement between two tables where there is

  • 0

I am trying to write a SQL statement between two tables where there is a has-many relation between two tables going one way and returns only the rows with more than one relation. I’m probably phrasing this horribly so here’s the example.

Let’s say table A has a column “key_a” that is a primary key for Table A. Table B has two columns. The first is called “key_b” which is a primary key for Table B and a second column called “key_a” which is a foreign key with a relation to “A.has_a”. Application logic has been implemented to ensure that While a row entry in Table A can have a relation from 0 to n rows in Table B, rows in Table B have a relation to exactly one row in Table A.

How can I write an SQL query that will return row entries in “A.key_a” that have more than one row in Table B mapping a relation to it through the “B.key_a” column?

  • 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-12T22:15:08+00:00Added an answer on June 12, 2026 at 10:15 pm
    SELECT key_a, a.<<other columns>>, COUNT( b.key_b )
      FROM table_a a
           JOIN table_b b USING (key_a)
     GROUP BY key_a, a.<<other columns>>
    HAVING COUNT( b.key_b ) > 1
    

    will give you the data from table_a where there are multiple matching rows in table_b. If you are only interested in getting the key_a values that have multiple rows in table_b rather than getting all the data from table_a, you can remove the reference to table_a entirely and just do the GROUP BY and HAVING on table_b.

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

Sidebar

Related Questions

i am trying to write one function which return sql statement like function get_sql($name=0,$date_start=0,$date_end=0)
I am trying to write a sql statement that I have 2 tables Store
Trying to find the best way to write this SQL statement. I have a
I am trying to write a SQL query where I am joining two tables
I am trying to find out how to write an SQL statement that will
I'm trying to write an insert statement for a SQL Server table that inserts
I have a perplexing SQL select statement (ugly) that I'm trying to write in
I am trying to write a SQL statement to fetch City, State and zipcodes
I'm trying to write an SQL statement that duplicates all rows WHERE employee =
I'm trying to write an SQL Statement that needs to loop through a list

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.