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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:23:44+00:00 2026-05-31T09:23:44+00:00

I am struggling to come up with the SQL query to find the all

  • 0

I am struggling to come up with the SQL query to find the all the products accessible to user under a particular division.

A division can have many products.

Division table has division_id and division_name.

Product table has product_id, product_name and division_id.

I have another table called UserProducts where I have columns like

user_id, division_id and product_id.

In the UI part while assigning products to user, division is selected first and then based on selected division products are populated.

In addition to the products from products table, the product drop down has one more option called as ALL.

When ALL option from product is selected all products under the division are assigned to the user.

But in the User products I do not enter entry for all the products. It has only one entry like

user_id = 1, division_id = 1 and product_id = -1 
where -1 says its ALL products under division_id = 1

Also user can have access to many divisions. So User can have access to all products under division A but only two products under divison B.

Now, with this schema how do I get all the products accessible to user under division?

EDIT 1

I will have user id and product id as inputs and i would want to get all the products accessible to given user under given division.

Input : user_id, division_id
output : List of all products by user_id and division_id

Please help.

  • 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-31T09:23:45+00:00Added an answer on May 31, 2026 at 9:23 am
    SELECT product_id, product_name and division_id
      FROM Products AS P
     WHERE EXISTS (
                   SELECT * 
                     FROM UserProducts AS U
                    WHERE U.division_id = P.division_id
                          AND U.product_id = -1
                  )
    UNION
    SELECT product_id, product_name and division_id
      FROM Products AS P
     WHERE EXISTS (
                   SELECT * 
                     FROM UserProducts AS U
                    WHERE U.division_id = P.division_id
                          AND U.product_id = P.product_id
                  );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a mock up of a sql query that will represent a real
I'm currently struggling to come up with a regex that can split up a
I've posted about this before but have been struggling to come up with a
I have a question about a particular query I'd like to execute against a
We're struggling to come up with a command name for our all purpose developer
I'm struggling to get my head around LINQ and have come to the conclusion
I've been struggling with event handling in backgroundworker threads. All the documentation I've come
I'm struggling with a situation that I come up again time and time again
Struggling with styling the mouse over for a button ... I have managed to
Iam struggling with NHibernate and its lazyload. I have a structure which I simplified

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.