52

Let's assume the following:

Table A

id | value
----------
1   | red
2   | orange
5   | yellow
10  | green
11  | blue
12  | indigo
20  | violet

I have a list of id's (10, 11, 12, 13, 14) that can be used to look up id's in this table. This list of id's is generated in my frontend.

Using purely SQL, I need to select the id's from this list (10, 11, 12, 13, 14) that do not have entries in Table A (joining on the 'id' column). The result should be the resultset of id's 13 and 14.

How can I accomplish this using only SQL? (Also, I'd like to avoid using a stored procedure if possible)

The only approach I can think of is something that would create an inline SQL table on the fly to temporarily hold my list of id's. However, I have no idea how to do this. Is this possible? Is there a better way?

Thanks! :)

4
  • This isn't very clear. If your IDs are not in another table there's nothing to join.
    – Cfreak
    May 22, 2012 at 20:15
  • Exactly. :) Therein lies the problem!
    – rinogo
    May 22, 2012 at 20:19
  • What's wrong with NOT IN(<your list>)?
    – Cfreak
    May 23, 2012 at 0:51
  • 3
    @Cfreak: The OP doesn't want id's from the table which aren't in the list; he wants id's from his list which aren't in the table.
    – eggyal
    May 23, 2012 at 1:27

5 Answers 5

110

You can do this from SQL Server 2008 onwards using a table value constructor.

SELECT * FROM (
   VALUES(1, 'red'),
         (2, 'orange'),
         (5, 'yellow'),
         (10, 'green'),
         (11, 'blue'),
         (12, 'indigo'),
         (20, 'violet'))
   AS Colors(Id, Value)

More information here: Table Value Constructor

4
  • 9
    Unfortunately this question is tagged with mysql, not sql-server.
    – trincot
    May 12, 2016 at 20:43
  • 34
    Fortunately for me I still clicked on it and found this Sep 28, 2016 at 16:50
  • 6
    Upvoted because I was searching for SQL Server and because you provided the name of the technique/function/functionality, even though OP asked for another RDBMS.
    – Jahaziel
    Dec 7, 2018 at 0:55
  • ILLEGAL SYMBOL "(".
    – john k
    Oct 29, 2021 at 19:08
33

You can create an "inline table" with a UNION subquery:

(
            SELECT 10 AS id
  UNION ALL SELECT 11 UNION ALL SELECT 12 UNION ALL SELECT 13 UNION ALL SELECT 14
  -- etc.
) AS inline_table
5
  • Will this be a problem if my list is actually a few hundred items long (instead of 5 items as in the question)?
    – rinogo
    May 22, 2012 at 20:16
  • @rinogo: Well, it would make your query quite long (which might mean it takes a while to parse), but I'm not aware of any inherent problems that might cause.
    – eggyal
    May 22, 2012 at 20:17
  • 2
    @rinogo: with really large lists you can hit max_allowed_packet limit which won't allow you to submit the query, though it requires hundreds of thousands records with default settings.
    – Quassnoi
    May 22, 2012 at 20:19
  • Well this is exactly what I needed! PS: I had a very limited need for the number of entries: just from 0 to 6 so this worked out pretty well
    – Hassen Ch.
    Dec 7, 2017 at 16:13
  • ILLEGAL USE OF KEYWORD UNION. TOKEN , FROM INTO WAS EXPECTED.
    – john k
    Oct 29, 2021 at 19:07
8
CREATE TEMPORARY TABLE ids (id INT NOT NULL PRIMARY KEY);

INSERT
INTO    ids
VALUES
(10),
(11),
(12),
(13),
(14);

SELECT  *
FROM    ids
WHERE   id NOT IN
        (
        SELECT  id
        FROM    a
        );
12
  • @mortezakavakebi: what do you mean?
    – Quassnoi
    Jul 26, 2013 at 15:55
  • creating a table is not session base. when different users call a url containing this Query makes conflict on the "ids" table. Jul 26, 2013 at 17:11
  • 5
    @mortezakavakebi: from the docs: A TEMPORARY table is visible only to the current connection, and is dropped automatically when the connection is closed. This means that two different connections can use the same temporary table name without conflicting with each other or with an existing non-TEMPORARY table of the same name.
    – Quassnoi
    Jul 26, 2013 at 17:16
  • I am having a syntax error in : CREATE TEMPORARY TABLE ids (id INT NOT NULL PRIMARY KEY); Here's the message: Unknown object type 'TEMPORARY' used in a CREATE, DROP, or ALTER statement.
    – FrenkyB
    Jan 26, 2016 at 17:00
  • 1
    @FrenkyB: that's normal when you try to run a MySQL query on SQL Server.
    – Quassnoi
    Jan 26, 2016 at 17:27
0

Something like this will work too

    
    SELECT * FROM (
    SELECT 'ds' AS source
    UNION ALL
    SELECT 'cache' AS source
    ) as dataSource
----------
| source |
----------
| ds     |
----------
| cache  |
----------



-1
create table B (id int)
insert into B values (10),(11),(12),(13),(14)

select *
from B
left join A 
on A.id=B.id
where A.id is null

drop table B

http://sqlfiddle.com/#!6/6666c1/30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.