NOT EXISTS works like EXISTS, except the WHERE clause in which it is used is satisfied if no rows are returned by the subquery.
Questions tagged [not-exists]
403 questions
386
votes
29 answers
Array.push() if does not exist?
How can I push into an array if neither values exist? Here is my array:
[
{ name: "tom", text: "tasty" },
{ name: "tom", text: "tasty" },
{ name: "tom", text: "tasty" },
{ name: "tom", text: "tasty" },
{ name: "tom", text:…

tarnfeld
- 25,992
- 41
- 111
- 146
267
votes
7 answers
Add a column to a table, if it does not already exist
I want to write a query for MS SQL Server that adds a column into a table. But I don't want any error display, when I run/execute the following query.
I am using this sort of query to add a table ...
IF EXISTS (
SELECT *
FROM …

Tavousi
- 14,848
- 18
- 51
- 70
127
votes
5 answers
SELECT * WHERE NOT EXISTS
I think I'm going down the right path with this one... Please bear with me as my SQL isn't the greatest
I'm trying to query a database to select everything from one table where certain cells don't exist in another. That much doesn't make a lot of…

Ciaran
- 1,878
- 5
- 18
- 31
100
votes
4 answers
MySQL Join Where Not Exists
I have a MySQL query that joins two tables
Voters
Households
They join on voters.household_id and household.id.
Now what I need to do is to modify it where the voter table is joined to a third table called elimination, along voter.id and…

gsueagle2008
- 4,583
- 10
- 36
- 46
59
votes
5 answers
Mysql select where not in table
I have 2 tables (A and B) with the same primary keys. I want to select all row that are in A and not in B. The following works:
select * from A where not exists (select * from B where A.pk=B.pk);
however it seems quite bad (~2 sec on only 100k rows…

BCS
- 75,627
- 68
- 187
- 294
41
votes
8 answers
How can I check whether a RabbitMQ message queue exists or not?
How can I check whether a message Queue already exists or not?
I have 2 different applications, one creating a queue and the other reading from that queue.
So if I run the Client which reads from the queue first, than it crashes.
So to avoid that i…

Jigar Sheth
- 586
- 2
- 5
- 21
23
votes
4 answers
Insert record into table if entry does not exist in another table- with an extra twist
Hi to all you mighty SQLsuperheros out there..
Can anyone rescue me from imminent disaster and ruin?
I'm working with Microsoft Access SQL. I'd like to select records in one table (table1) that don't appear in another (table2) .. and then insert new…

bonzo46
- 341
- 1
- 2
- 8
21
votes
4 answers
How can I do an insert where not exists?
I'd like to combine an insert query with a "where not exists" so as not to violate PK constraints. However, syntax such as the following gives me an Incorrect syntax near the keyword 'WHERE' error -
INSERT INTO…

froadie
- 79,995
- 75
- 166
- 235
20
votes
7 answers
SQL - improve NOT EXISTS query performance
Is there a way I can improve this kind of SQL query performance:
INSERT
INTO ...
WHERE NOT EXISTS(Validation...)
The problem is when I have many data in my table (like million of rows), the execution of the WHERE NOT EXISTS clause if very slow. I…

Melursus
- 10,328
- 19
- 69
- 103
17
votes
2 answers
mysql: select all items from table A if not exist in table B
I am having problem with selecting values from table a (id, room_name) where there are no corresponding events in table b (room_id, room_start, room_finish)
my query looks following
SELECT id, room_name FROM rooms
WHERE NOT EXISTS
(SELECT * FROM…

m1k3y3
- 2,762
- 8
- 39
- 68
11
votes
3 answers
MySQL monthly Sale of last 12 months including months with no Sale
SELECT DATE_FORMAT(date, "%b") AS month, SUM(total_price) as total
FROM cart
WHERE date <= NOW()
and date >= Date_add(Now(),interval - 12 month)
GROUP BY DATE_FORMAT(date, "%m-%Y")
This query displaying result for only existing month. I need all 12…

Wasim A.
- 9,660
- 22
- 90
- 120
11
votes
3 answers
Using a table variable inside of a exists statement
I am trying to update a column inside of a table variable based on a condition, the condition being that the ID of the table variable does not exist in a different table:
DECLARE @BugRep TABLE(BugCode VARCHAR(50),DevFirstName VARCHAR(50),…

Developer
- 17,809
- 26
- 66
- 92
9
votes
3 answers
The purpose of SQL's EXISTS and NOT EXISTS
Every now and then I see these being used, but it never seems to be anything that can't be performed as equally well, if not better, by using a normal join or subquery.
I see them as being misleading (they're arguably harder to accurately visualize…

Riedsio
- 9,758
- 1
- 24
- 33
7
votes
4 answers
Searching not exists in Neo4j via Cypher
I have some relations between persons in my graph.
my data (generate script below)
create (s:Person {name: "SUE"})
create(d:Person {name: "DAVID"})
create(j:Person {name: "JACK"})
create(m:Person {name: "MARY"})
create(js:Person {name: "JASON"})…

AgonyClanKios
- 102
- 1
- 8
7
votes
2 answers
How do I correctly use two Not Exists statements in a where clause using Access SQL VBA?
I have 3 Tables: NotHeard,analyzed,analyzed2. In each of these tables I have two columns named UnitID and Address.
What I'm trying to do right now is to select all of the records for the columns UnitID and Address from NotHeard that don't appear in…

Bryan
- 1,851
- 11
- 33
- 56