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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:25:39+00:00 2026-06-17T16:25:39+00:00

Say we have two tables: Company +—+—–+ |id | name| +—+—–+ |1 | bar

  • 0

Say we have two tables:

Company
+---+-----+
|id | name|
+---+-----+
|1  | bar |
|2  | foo |
+---+-----+

Branch
+----+----+-------+
|cid | id | profit|
+----+----+-------+
|1   | 10 | 100   |
|1   | 11 | 200   |
|2   | 20 | 50    |
+----+----+-------+

--cid in Branch is the foreign key to company id

The query is to find out unique companies with at least one branch having profit > 100 say.

One way is:

SELECT DISTINCT c.id, c.name 
  FROM Company c, Branch b
  WHERE c.id == b.cid AND b.profit > 100;

The particular scenario is that very few companies have branches (basically the entries in Branch table is far fewer than those in Company. Given this information is the above query the best possible? Or is there any alternative?

  • 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-17T16:25:41+00:00Added an answer on June 17, 2026 at 4:25 pm

    A more efficient method may be to use an EXISTS clause:

    SELECT c.id, c.name 
    FROM Company c
    WHERE EXISTS
    (SELECT 1
     FROM Branch b
     WHERE c.id = b.cid AND b.profit > 100)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have two tables tblA ( tableAID INT IDENTITY(1,1), foo VARCHAR(100)) tblB
Say I have two tables called A (fields: id, phase, name) and B(fields: id,
Let's say I have two tables: tableA ------- type (varchar) name (varchar) tableB -------
lets say I have two tables: Persons (P_Id, Name) and Orders (O_Id, OrderNo, P_Id)...
Lets say I have two tables table_1 ID Name Status 1 John 2 Peter
Lets say I have two tables as such: People Id Name FirstDate LastDate --
I have two tables: entreprises(id, name) entreprises_struct(id,entreprise_id, entreprise_child_id) let's say i have these data:
Let's say we have two tables, ORDERS and OFFERS Order POJO @Entity @Table(name =
Say we have two tables: table1 (id, name, type) where id is a primary
Say I have two tables, owner and dog . Both have column name ,

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.