Solved

Verify SQL statement or suggest a better way

Posted on 2006-10-20
4
426 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

726 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