473,386 Members | 1,886 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

how to rewrite this query without a subquery ?

Hi all

For speed reasons, I would like to rewrite the following query without
the subquery :

SELECT * FROM table1 WHERE field1 NOT IN (SELECT field1 FROM table2
where field2=X);

I thought something like this would work fine :
SELECT table1.* FROM table1 LEFT JOIN table2 ON
table1.field1=table2.field1
WHERE (table2.field2 IS NULL OR table2.field2<>X);

.... but it doesn't, because for example, if you have in table2:
table2.field1 = 1 ; table2.field2 = X
table2.field1 = 1 ; table2.field2 = Y

the second line shoudn't be shown because there IS a line where
table2.field1 = 1 and table2.field2 = X.

With the first query, table2.field1 = 1 WILL NOT be returned (which is
correct) BUT in the second query, it WILL be returned (the join will
skip the first line, but will use the second line) ...

Has anybody a better idea on how to rewrite this kind of query ?!

Thanks for your help
Krystoffff
Jul 19 '05 #1
3 3085
>
SELECT * FROM table1 WHERE field1 NOT IN (SELECT field1 FROM table2
where field2=X);

I maybe found a solution:

select * FROM table1 LEFT JOIN table2 ON (table1.field1=table2.field1
AND table2.field2=X) WHERE table2.field2 IS NULL
The result seems to be correct.
What do you think about ?
krystoffff
Jul 19 '05 #2
>
SELECT * FROM table1 WHERE field1 NOT IN (SELECT field1 FROM table2
where field2=X);

I maybe found a solution:

select * FROM table1 LEFT JOIN table2 ON (table1.field1=table2.field1
AND table2.field2=X) WHERE table2.field2 IS NULL
The result seems to be correct.
What do you think about ?
krystoffff
Jul 19 '05 #3
>
SELECT * FROM table1 WHERE field1 NOT IN (SELECT field1 FROM table2
where field2=X);

I maybe found a solution:

select * FROM table1 LEFT JOIN table2 ON (table1.field1=table2.field1
AND table2.field2=X) WHERE table2.field2 IS NULL
The result seems to be correct.
What do you think about ?
krystoffff
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: krystoffff | last post by:
Hi all For speed reasons, I would like to rewrite the following query without the subquery : SELECT * FROM table1 WHERE field1 NOT IN (SELECT field1 FROM table2 where field2=X); I thought...
4
by: shumaker | last post by:
I'm wondering how/why this query works. Trying to get my head wrapped around SQL. Basically the Query deletes from the Import table all records that are already in FooStrings so that when I do an...
0
by: apb18 | last post by:
A bit of query plan strangeness. Suppose you have an inheritance tree such that the columns 'ID' and 'field' appear in the top level table, call that table XXX. tables YYY and ZZZ both inherit...
2
by: Yonatan Goraly | last post by:
I have a query that uses the same view 6 times. It seems that the database engine is calculating the view each time. The result is very poor performance. The same query takes 2 sec with MS SQL,...
5
by: Bob Stearns | last post by:
When I run the following query with the two sections commented out, the response time is between 1 an 2 seconds; with the first indicated section enabled, the response goes up to 15 seconds even...
1
by: Access | last post by:
I made a query with some totals. One of these totals I only get when using a subquery. I believe it must be possible to get the resulkt in one query ArtNr Total #order #customer...
6
by: Mark Reed | last post by:
Hi all, Please help. I have a table with 2 fields of which I am trying to change a select query into a delete query. the select query is: SELECT Table1.Date, Min(Table1.Ball) AS MinOfBall...
7
by: K. Crothers | last post by:
I administer a mechanical engineering database. I need to build a query which uses the results from a subquery as its input or criterion. I am attempting to find all of the component parts of...
24
by: clare at snyder.on.ca | last post by:
I have a SQL query I need to design to select name and email addresses for policies that are due and not renewed in a given time period. The problem is, the database keeps the information for every...
5
by: redstamp | last post by:
Try as I might I cannot find a way to write an access query to return a result set with the records from my database WITHOUT a certain set of values within a field. To explain, I have a table of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.