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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:16:50+00:00 2026-06-06T08:16:50+00:00

Background I’ve got a group table, users table, and a group_members table. groups {

  • 0

Background

I’ve got a group table, users table, and a group_members table.

groups { group_ varchar(50) not null, etc... }
users  { user_id varchar(50) not null, etc... }
group_members { group_ varchar(50 not null, member varchar(50) not null }

My requirements state that a group can have other groups as members. The user needs to be considered a member of all groups that any group they are a member of.
For Example, consider the following data in these tables:

group_members              | groups         | users    |
========================== | ============== | ======== |
group_          member     | group_         | user_id  |
-------------------------- | -------------- | -------- |
'SYSTEM_ADMIN'  'OE_ADMIN' | 'SYSTEM_ADMIN' | 'USER    |
'SYSTEM_ADMIN'  'AR_ADMIN' | 'OE_ADMIN'     |          |
'SYSTEM_ADMIN'  'USER'     | 'AR_ADMIN'     |          |

My desired result of asking the question
What groups is ‘USER’ a member of? should be

member
==============
'SYSTEM_ADMIN'
'OE_ADMIN'
'AR_ADMIN'

Question

I’ve got the following query built and providing me with the required results, but it looks a little complex.

WITH GM
AS (
    SELECT GROUP_, MEMBER FROM group_members 
       WHERE member IN (SELECT group_ FROM groups)
)
SELECT group_ FROM group_members WHERE member = 'USER'
UNION
SELECT MEMBER AS GROUP_ FROM GM 
   WHERE group_ in (SELECT group_ FROM group_members WHERE member = 'USER')

Any suggestions on how to make this query simpler, or less cluttered?

  • 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-06T08:16:53+00:00Added an answer on June 6, 2026 at 8:16 am

    Your recursive CTE looks fine.

    How does it perform?

    I don’t think there’s any significant way to make it a lot simpler – the recursive CTEs always look kind of messy.

    The WHERE IN is equivalent to a JOIN, but I don’t see it as being more readable and the execution plan should be pretty equivalent.

    Your root row could be represented as a CTE before the recursive one, but it won’t really save much in readability:

    WITH root AS ()
         ,CTE AS ()
    SELECT FROM ROOT 
        UNION
    SELECT FROM CTE
    

    In any case, you could put this in a view or inline table-valued-function (with a parameter) to make it easy to use from elsewhere in the system so you don’t have to see it very much.

    Something I’ve done for hierarchies is to build a more high-performance flattened/denormalized version and update it on a trigger or timer and use that as a read-only performance enhancer. For instance, in a technical support system we build where a question could be tagged in a hierarchy, we filled in all the parents (printers->hp->laserjet) when a child was selected, so it was easy to query for problems with hp printers on any version of Windows or problems with laserjets on Windows XP.

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

Sidebar

Related Questions

Background: I have a CMS tool that allows users to create a table inside
Background I have a dimension table that has a single record for each day.
Background: I'm writing a 'standard' (nothing special) web application in Ruby (not Rails) and
Background We currently use OpenID for authentication of users, and use the Claimed Identity
Background: I'm testing a function within an ASP.NET 4.0 (Web Forms not MVC) and
Background: I am developing an app which sends an SMS to users after registration
[background below] I've got my data modelled out in SQLObject in Python on the
Background I admit, this question stems from an ultimate lack of deep understanding of
Background: Our web app uses the jquery.constrain.js plugin to handle data entry in some
Background Consider the following: template <unsigned N> struct Fibonacci { enum { value =

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.