?
Solved

Verify SQL statement or suggest a better way

Posted on 2006-10-20
4
Medium Priority
?
428 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 2000 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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Suggested Courses

777 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