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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:21:07+00:00 2026-06-14T14:21:07+00:00

I got a situation where I want to get the data from one database

  • 0

I got a situation where I want to get the data from one database table which are not in other database table. For that I am using NOT IN clause. This works fine for small amount of data. When tables have large data it takes huge time. I checked for 2000 rows, it is taking more than 8 minutes. I think huge time is because of NOT IN clause. Please anyone suggest alternate method to do this ASAP.

Edit: There is small change in schema. In dept_project_tasks I have ids of association tables.(I am using rails which comes with default ids)

DB and query details are as follows:
(Sqlfiddle link for same: clickhere . Please don’t modify in same sqlfiddle page)

Database 1:

 create table TASKS(task_code VARCHAR(20), task_name VARCHAR(20), project_name VARCHAR(20),dept_code VARCHAR(20));

 insert into TASKS(task_code, task_name, project_name, dept_code) VALUES("task1", "task1", "project1", "dept1");
 insert into TASKS(task_code, task_name, project_name, dept_code) VALUES("task1", "task1", "project1", "dept2");
 insert into TASKS(task_code, task_name, project_name, dept_code) VALUES("task2", "task2", "project2", "dept1");
 insert into TASKS(task_code, task_name, project_name, dept_code) VALUES("task2", "task2", "project2", "dept3");
 insert into TASKS(task_code, task_name, project_name, dept_code) VALUES("task3", "task3", "project3", "dept2");
 insert into TASKS(task_code, task_name, project_name, dept_code) VALUES("task3", "task3", "project3", "dept1");
 insert into TASKS(task_code, task_name, project_name, dept_code) VALUES("task4", "task4", "project4", "dept1");
 insert into TASKS(task_code, task_name, project_name, dept_code) VALUES("task4", "task4", "project4", "dept3");

Database 2:

 create table depts(dept_code VARCHAR(20), dept_name VARCHAR(20));
 create table project_tasks(task_code VARCHAR(20), task_name VARCHAR(20));
 create table dept_project_tasks(dept_code VARCHAR(20), task_code VARCHAR(20));

 insert into depts(dept_code, dept_name) values("dept1", "dept_one");
 insert into depts(dept_code, dept_name) values("dept2", "dept_two");

 insert into project_tasks(task_code, task_name) values("task1", "task1");
 insert into project_tasks(task_code, task_name) values("task2", "task2");
 insert into project_tasks(task_code, task_name) values("task3", "task3");

 insert into dept_project_tasks(dept_code, task_code) values("dept1", "task1");
 insert into dept_project_tasks(dept_code, task_code) values("dept2", "task1");
 insert into dept_project_tasks(dept_code, task_code) values("dept1", "task2");
 insert into dept_project_tasks(dept_code, task_code) values("dept3", "task2");

Query is:

 SELECT distinct task_code 
 from TASKS as TS 
 where TS.dept_code="dept1"  
   AND TS.task_code NOT IN (SELECT `project_tasks`.task_code 
                            FROM `project_tasks` 
                            INNER JOIN `dept_project_tasks` ON `project_tasks`.task_code = `dept_project_tasks`.task_code 
                            WHERE `dept_project_tasks`.dept_code = "dept1"
                           );

Thanks in advance

  • 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-14T14:21:09+00:00Added an answer on June 14, 2026 at 2:21 pm

    Use LEFT JOIN like so:

    SELECT distinct ts.task_code t1
    from TASKS as TS 
    LEFT JOIN `project_tasks` pt ON ts.task_code = pt.task_code
    LEFT JOIN `dept_project_tasks` dpt ON pt.task_code = dpt.task_code 
    WHERE dpt.task_code IS NULL;
    

    Updated SQL Fiddle Demo

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

Sidebar

Related Questions

I've got a situation which I want to fetch data from a database, and
I've got a situation where I want to get a regexp from the user
Here's the situation - its a bit different from the other database/password questions on
I got a situation, I can't get a program compile since I splitted the
I've got a situation like this: Table: FunTable ItemKey ItemName ItemPriceEffectiveDate ItemPrice 11 ItemA
So I've got a situation where I need to pass a bunch of data
I've got the following situation: var totalRecords = (from entry in invalidAccEntryRepository select entry).Where(entry
I've got a situation where I want to have a master list at the
Situation: I have many gzipped input files that I want to process in a
I've got an associative array in php that I want to stick into a

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.