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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:05:27+00:00 2026-06-11T02:05:27+00:00

I have 3 tables : users, roles and roles_users (roles_users is a relational table

  • 0

I have 3 tables : users, roles and roles_users (roles_users is a relational table with 2 columns, user_id and role_id)

A user can have many roles, through the roles_users table.

How can I build a select query that will not select a user, if he has a specific role.

To illustrate :

  • a super admin user has the super admin , admin, login and publisher roles.

  • an admin user has the admin, login and publisher roles.

  • a publisher user has the login and publisher roles.

I want to select all the users that do not have the super admin role.
In other words, if a user has the super admin role, then do not select it.

How can I achieve this in SQL ?

Edit : Here is the query that worked for me at the end : (thanks to András Ottó’s answer)

SELECT DISTINCT(users.id), users.*
FROM users
INNER JOIN roles_users on users.id = roles_users.user_id
INNER JOIN roles ON roles_users.role_id = roles.id
WHERE NOT EXISTS(SELECT roles_users.user_id FROM roles_users
      WHERE roles_users.user_id = users.id AND roles_users.role_id = 2)
  • 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-11T02:05:28+00:00Added an answer on June 11, 2026 at 2:05 am

    In Sql Server on in Oracle you can try:

    SELECT users.*, roles.roleName
    FROM users
    INNER JOIN roles_users on users.userid = roles_users.userid
    INNER JOIN roles ON roles_users.roleID = roles.roleID
    WHERE NOT EXISTS(SELECT roles_users.userID FROM roles_users
          WHERE roles_users.userID = users.userid AND roleName = 'super admin')
    

    Here is an SQL Fiddle.

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

Sidebar

Related Questions

I have 3 tables: USER ===== USER_ID(PK) FISRT_NAME LAST_NAME ... ROLES ====== ROLE_ID (PK)
I have a table containing roles. Many users can be assigned to a role.
I have 3 tables users , roles and roles_users . The roles_users table have
Basicall I have three Tables Users Roles and USerRoles with Many to many Relationship.
Supposing I have 3 tables: Users, Roles and UserRoles. I can fetch the user_roles
Right now I have 3 tables: User, Roles, and User_Roles for the many-to-many association.
I have 3 tables users(id,name),groups(id,name) and users_groups(user_id,group_id). users and groups have many to many
I have two tables: Users and Roles. A user may have more roles, so
I have 3 tables Users, AccessLevels, Roles. My User class have public virtual Dictionary<Role,AccessLevel>
I have 2 tables: Users and Roles, and I have a table that joins

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.