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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:57:42+00:00 2026-06-15T03:57:42+00:00

I need help writing a complex exclusion sql query (at least for me). First

  • 0

I need help writing a complex exclusion sql query (at least for me). First I am describing about requirement and queries here…

Say

  1. I have a table “my_users” where user’s name and user_ids are maintained.

  2. I have a table “my_skills” where I maintain a skill name and corresponding skill_id.

  3. I have a table “my_users_skills” where users and their corresponding skill_ids are maintained.

  4. I have a table “my_companies” where I maintain a company name and corresponding company_ids.

  5. I have a table “my_employer_profile” that contians employer-users id, and employer-company-id

  6. I have a table “my_users_blocked_companies” , it contains users ids and corresponding blocked company ids.
    Purpose of “my_users_blocked_companies” table, is to maintain 1 or more companies for users as blocked companies.

When HR person of one company search for users, users who has set that company (where HR is working) as a blocked company should not come in search result.

Lets get to SQL queries

1.

DROP TABLE IF EXISTS my_users;

CREATE TABLE my_users
(
user_id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
name varchar(20) NOT NULL UNIQUE,
age int NULL
)


INSERT INTO my_users VALUES
(1, "John", 20),
(2, "Kelly", 25),
(3, "Xavier", 50),
(4, "Krishna", 40);

2.

DROP TABLE IF EXISTS my_skills;

CREATE TABLE my_skills
(
skill_id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
skill varchar(20) NOT NULL UNIQUE
);


INSERT INTO my_skills VALUES
(1, "MySql"),
(2, "Php"),
(3, "Linux"),
(4, "Java");

3.

DROP TABLE IF EXISTS my_users_skills;

CREATE TABLE my_users_skills
(
user_id int NOT NULL,
skill_id int NOT NULL
);

INSERT INTO my_users_skills VALUES
(1,2),
(1,4),
(2,1),
(2,4),
(3,2),
(3,3);

4.

DROP TABLE IF EXISTS my_companies;

CREATE TABLE my_companies
(
company_id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
company varchar(20) NOT NULL UNIQUE
);


INSERT INTO my_companies VALUES
(1, "Motorola"),
(2, "Logica"),
(3, "Microsft"),
(4, "Texas Instruements");

5.

DROP TABLE IF EXISTS my_employer_profile;

CREATE TABLE my_employer_profile
(
user_id int NOT NULL,
comp_id int NOT NULL
);

INSERT INTO my_employer_profile VALUES
(4,1),

Now it is clear that “Krishna” is an employee in company Motorola.

6.

DROP TABLE IF EXISTS my_users_blocked_companies;

CREATE TABLE my_users_blocked_companies
(
user_id int NOT NULL,
comp_id int NOT NULL
);

INSERT INTO my_users_blocked_companies VALUES
(1,1),
(1,4),
(2,4)

Now you can clearly see John has set Motorola and Texas Instruements as blocked companies.
Also kelly has set Texas Instruements as blocked company.

Now if Krishna is searching for users who has skill set ‘java’,’mysql’, we can have two possible cases:

Case 1

The search result, without considering blocked companies will fetch records for John and Kelly.

The SQL query for this case is

SELECT x.name
, GROUP_CONCAT(DISTINCT y2.skill ORDER BY y2.skill) skills
FROM my_users x
JOIN my_users_skills xy1
ON xy1.user_id = x.user_id
JOIN my_skills y1
ON y1.skill_id = xy1.skill_id
AND y1.skill IN ('java','mysql')
JOIN my_users_skills xy2
ON xy2.user_id = xy1.user_id
JOIN my_skills y2
ON y2.skill_id = xy2.skill_id
GROUP
BY x.user_id; 

Case 2

And the search result, with considering blocked companies will fetch records for Kelly as John has set Motorola as a blocked company, and Krishna is from Motorola

For this case, I have struggled a lot, but not able to write the right query.

  • 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-15T03:57:44+00:00Added an answer on June 15, 2026 at 3:57 am

    You can do it like this:

    SELECT x.name , GROUP_CONCAT(DISTINCT y2.skill ORDER BY y2.skill) skills 
       FROM my_users x 
       JOIN my_users_skills xy1 
           ON xy1.user_id = x.user_id 
       JOIN my_skills y1 
           ON y1.skill_id = xy1.skill_id AND y1.skill IN ('java','mysql') 
       JOIN my_users_skills xy2 
           ON xy2.user_id = xy1.user_id 
       JOIN my_skills y2 
           ON y2.skill_id = xy2.skill_id 
       GROUP BY x.user_id 
           HAVING x.user_id NOT IN (
             SELECT bc.user_id FROM my_users_blocked_companies bc 
             LEFT JOIN my_companies c ON bc.comp_id = c.company_id
             WHERE c.company IN ('Motorola')
           );
    

    sqlfiddle execution here

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

Sidebar

Related Questions

I need help writing a query that will validate a first expression and than,
I need help writing this progress query: find first a no-lock where a.a =
I need help writing a T-SQL query that will generate 52 rows of data
need help/guide for sql select query, I have 2 table stock and stock_history, in
I need to make a rather complex query, and I need help bad. Below
I need help writing a conditional where clause. here is my situation: I have
I need help writing some SQL. I hate to ask such a rookie question,
Here is what the html looks like, I need help writing the jquery? Initially
Need help in writing the query to get the last month data as well
Need help writing a script downloads data from google insight using c# this is

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.