Solved

Verify SQL statement or suggest a better way

Posted on 2006-10-20
4
423 Views
Last Modified: 2008-02-01
HI Experts. I need to verify what this statement is doing. We had a situation at work where doubt was cast over query results and I really need to know if this query is good. If it is not can someone assist by suggesting an alternative?

OK I have BIGTABLE and SMALLTABLE. BIGTABLE contains, say 50k rows and smalltable contains say 6k rows. I want to run a query that checks to see if any of smalltable exists in bigtable. I want to search on add1 and postcode This is what I have been doing so far.

select * from smalltable s

where   exists
(select       *
from      bigtable b
where       s.add1 = b.add1 and s.postcode = b.postcode)

Is this query good to return what I want to know, in your expert opinion?

thanks
0
Comment
Question by:pabby061203
  • 2
4 Comments
 
LVL 23

Accepted Solution

by:
adathelad earned 500 total points
ID: 17772231
Hi pabby061203,
Yes, that will find all records in smalltable that have a record in bigtable with the same add1 & postcode combination.

Alternatively you can do:
SELECT s.*
FROM smalltable s
    JOIN bigtable b ON s.add1 = b.add1 AND s.postcode = b.postcode

0
 
LVL 6

Expert Comment

by:Gokulm
ID: 17772248
Yes your query works well... for the required condition
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 17772285
Compare a parent table and a child table and find out if there are any parent records that don't have a match in the child table.

Using NOT EXISTS

SELECT P.Col1 FROM Parent_tbl P
WHERE NOT EXISTS (SELECT * FROM CHILD_tbl C WHERE P.Col1 = C.Col1)

Using LEFT JOIN

SELECT P.Col1 FROM Parent_tbl P
LEFT JOIN CHILD_tbl C ON P.Col1 = C.Col1
WHERE C.Col1 IS NULL

Using NOT IN

SELECT Col1
FROM Parent_tbl
WHERE Col1 NOT IN (SELECT Col1 FROM CHILD_tbl)

The results will be identical. But, which query produces the best performance? Assuming everything else is equal; the best performing version through the worst performing version will be from top to bottom, as displayed above. In other words, the NOT EXISTS variation of this query is generally the most efficient.
coz the indexes found on the tables, along with the number of rows in each table, can influence the results.
0
 
LVL 6

Expert Comment

by:Gokulm
ID: 17772301
Query cost of your query is less than other query. It will not return any duplicates in bigtable where as the join query given above list duplicate in bigtable if any..
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now