Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Verify SQL statement or suggest a better way

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

610 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