Solved

Verify SQL statement or suggest a better way

Posted on 2006-10-20
4
421 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Need Counts 11 40
how to fix this error 14 46
Help with SQL Query 23 39
how open sql server always on replica read_only 13 17
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

705 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

20 Experts available now in Live!

Get 1:1 Help Now