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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:43:22+00:00 2026-05-27T22:43:22+00:00

I need to return all values from colA that are not in colB from

  • 0

I need to return all values from colA that are not in colB from mytable. I am using:

SELECT DISTINCT(colA) FROM mytable WHERE colA NOT IN (SELECT colB FROM mytable)

It is working however the query is taking an excessively long time to complete.

Is there a more efficient way to do this?

  • 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-27T22:43:22+00:00Added an answer on May 27, 2026 at 10:43 pm

    In standard SQL there are no parentheses in DISTINCT colA. DISTINCT is not a function.

    SELECT DISTINCT colA
    FROM   mytable
    WHERE  colA NOT IN (SELECT DISTINCT colB FROM mytable);
    

    Added DISTINCT to the sub-select as well. If you have many duplicates it could speed up the query.

    A CTE might be faster, depending on your DBMS. I additionally demonstrate LEFT JOIN as alternative to exclude the values in valB, and an alternative way to get distinct values with GROUP BY:

    WITH x AS (SELECT colB FROM mytable GROUP BY colB)
    SELECT m.colA
    FROM   mytable m
    LEFT   JOIN x ON x.colB = m.colA
    WHERE  x.colB IS NULL
    GROUP  BY m.colA;
    

    Or, simplified further, and with a plain subquery (probably fastest):

    SELECT DISTINCT m.colA
    FROM   mytable m
    LEFT   JOIN mytable x ON x.colB = m.colA
    WHERE  x.colB IS NULL;
    

    There are basically 4 techniques to exclude rows with keys present in another (or the same) table:

    • Select rows which are not present in other table

    The deciding factor for speed will be indexes. You need to have indexes on colA and colB for this query to be fast.

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

Sidebar

Related Questions

I need to return a full directory name from inside a specified directory that
I need to return multiple values from a ColdFusion function in an ajax callback
I want to receive an array that contains all the h1 tag values from
I need to write a query that returns the sum of all values that
I need to get all the values from an enum, whose type will only
I need to write a query that returns all object less that or equal
In a shell-script (or Perl) I need a subroutine that returns for all my
Basically, the question says it all. I need a PL\SQL query that returns a
I need to return server time from a webservice. I've got the following code:
I need to return two values at a time, so I have: class IterableObject(object):

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.