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

  • Home
  • SEARCH
  • 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 1098509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:35:22+00:00 2026-05-17T00:35:22+00:00

Before delving into the issue, first I will explain the situation. I have two

  • 0

Before delving into the issue, first I will explain the situation. I have two tables such as the following:

USERS TABLE
user_id
username
firstName
lastName

GROUPS TABLE
user_id
group_id

I want to retrieve all users who’s first name is LIKE ‘%foo%’ and who is a part of a group with group_id = ‘givengid’

So, the query would like something like this:

SELECT user_id FROM users WHERE firstName LIKE '%foo'"

I can make a user defined sql function such as ismember(user_id, group_id) that will return 1 if the user is a part of the group and 0 if they are not and this to the WHERE clause in the aforementioned select statement. However, this means that for every user who’s first name matches the criteria, another query has to be run through thousands of other records to find a potential match for a group entry.

The users and groups table will each have several hundred thousand records. Is it more conventional to use the user defined function approach or run a query using the UNION statement? If the UNION approach is best, what would the query with the union statement look like?

Of course, I will run benchmarks but I just want to get some perspective on the possible range of solutions for this situation and what is generally most effective/efficient.

  • 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-17T00:35:23+00:00Added an answer on May 17, 2026 at 12:35 am

    You should use a JOIN to get users matching your two criteria.

    SELECT 
      user_id 
    FROM 
      users 
    INNER JOIN
      groups
      ON groups.user_id = users.users_id
        AND groups.group_id = given_id
    WHERE 
      firstName LIKE '%foo'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Before you answer this I have never developed anything popular enough to attain high
Before I jump headlong into C#... I've always felt that C, or maybe C++,
Before, I have found the Cost in the execution plan to be a good
Before anyone suggests scrapping the table tags altogether, I'm just modifying this part of
Before I start, I know there is this post and it doesn't answer my
Before I do this I figured I would ask if it was the best
Before moving on to use SVN, I used to manage my project by simply
Before I upgraded to Firefox 3 I used to constantly use the View Source
Before you start firing at me, I'm NOT looking to do this, but someone
Before you answer, this question is complicated: We are developing in asp.net / asp.net

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.