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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:27:08+00:00 2026-05-24T17:27:08+00:00

I think this is a simple SQL query problem, but I am just not

  • 0

I think this is a simple SQL query problem, but I am just not familiar enough with the different SQL join types to get the best solution to this problem.

I have a system that manages various items and the owners of those items. A user of the system has access to view all information about the items and the owners of such items if the user has permissions to view that owner. My schema looks like:

Users:
| UserId |

Items:
| ItemId | OwnerId | Name | Age |

Owners:
| OwnerId | FirstName | LastName | State |

OwnerPermissions:
| UserId | OwnerId |

It is possible that an Item does not have an owner — then OwnerId is NULL for that row in the Items table.

So if an entry exists for UserId=0 and OwnerId=0 and OwnerId=1 in OwnerPermissions then UserId=0 can view all the item information owned by OwnerId 0 and 1.

I want to write a query that given a userId, it returns all Item and Owner information for the owners the user has permissions to see, and also shows all items that don’t have owners

The query:

select Items.*, Owners.* 
from Items
inner join Owners on Items.OwnerId = Owners.OwnerId
inner join OwnerPermissions on OwnerPermissions.OwnerId = Owners.OwnerId
where OwnerPermissions.UserId = $userId

Gets me all the Item information that are owned by people I have permission to see, but it misses the Items that are not owned by anyone (OwnerId is NULL).

What is the best way to write this query? I use SQL Server, but I prefer an answer that doesn’t rely on it (but would be interested if there is a neat trick SQL Server can do)

  • 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-24T17:27:09+00:00Added an answer on May 24, 2026 at 5:27 pm

    Change it to a LEFT JOIN and every item is shown, with NULL if it has no owner.

    select Items.*, Owners.* 
    from Items
    left join Owners on Items.OwnerId = Owners.OwnerId
    left join OwnerPermissions on OwnerPermissions.OwnerId = Owners.OwnerId
    where OwnerPermissions.UserId = $userId
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think this must be simple but I can't get it right... I have
I think this is a simple question, but I can not find the answer
I'm trying to get this simple PowerShell script working, but I think something is
I want to perform a simple sql query but cannot manage to get it
This might be a very simple thing. Check out the normal sql query below
i think this is a simple question but I've searched around and can't seem
I think this should be simple but I am having some difficulty implementing it.
I think this is going to be one of those simple-when-you-see-it problems, but it
I think this is a simple question although I do not know how to
What is the function of the namespace here? I would think in this simple

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.