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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:34:23+00:00 2026-06-18T10:34:23+00:00

I have two tables with one to many relationship. I want to write a

  • 0

I have two tables with one to many relationship. I want to write a query which outputs all records from table with one record and only one record from the table having many records.

So the table having many records with first show the most occurring record. If there are equal occurrences then it will Order by ascending and show the first record.

Table1

Col1 Col2 Col3
a1     1   4
a2     2   5
a3     3   6

Table2

Col1 Col4
a1     10
a1     11
a1     22
a1     11
a2     10
a2     11
a3     19
a3     22
a3     22
a3     23

Query output:

Col1 Col2 Col3 Col4
a1     1   4    11
a2     2   5    10
a3     3   6    22

Hope I made it clear.

  • 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-18T10:34:25+00:00Added an answer on June 18, 2026 at 10:34 am

    First you need to use a group by along with a min() to get the smallest number from table2, then you join to table1 to get the columns you need. I’ve used a left join as I’m assuming there may not be a match in table2 but you can change it to an INNER JOIN if there are always 1 or more corresponding records in table2.

    SELECT  a.col1, a.col2, a.col3, b.col4
    FROM    table1 a
            LEFT JOIN (
                        SELECT  col1, col4 = MIN(col4)
                        FROM    table2
                        GROUP BY col1
                        ) b
                ON a.col1 = b.col1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables connected with one to many relationship. Parent Table is a
Let's say I have two tables with a one-to-many relationship. I want to create
Suppose I have two tables in one to many relationship. And, I want to
I have two tables that have a one-to-many relationship. The primary key of table
I have two tables with a many to one relationship which represent lots and
i have two tables mapped by JPA with One to Many relationship. I want
android noob... I have two tables, with a one to many relationship between country_tbl
I have two tables. One user table and one table in which results are
I have two tables, that relate via a one-to-many relationship i.e tableOne (1)----------(*) tableTwo
Imagine you have two tables, with a one to many relationship. For this example,

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.