473,396 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Finding a way to avoid joins/inner selects

In SQL 2005 I'm writing a query that works fine as an inner select or
a join but I know there's gotta be a way to do it without doing either
of those, for obvious performance reasons. I just want to hit the
database once.

Lets say you have a table with 2 columns, users and shipments. A user
can have multiple shipments but those shipments can only be inbound or
outbound so the table would look like this (I'm keeping it very basic
for the sake of this example):

Shipments
User Shipment
1 Inbound
1 Inbound
1 Inbound
1 Inbound
2 Inbound
2 Outbound
2 Inbound
3 Inbound
3 Inbound
3 Inbound
3 Inbound
4 Outbound
4 Outbound
4 Outbound

I want to pull back all users that did not have any outbound
shipments. I know I can do it with an inner select:

select user
from users
where user not in
(
select user
from shipments
where shipment = 'Outbound'
)

This seems so simple but I don't know how I'd do it without hitting
the database twice. Any ideas?
Nov 14 '08 #1
3 3191
Not sure how much you simplified your scenario, but with the sample data
provided you can use this query to get users who have only Inbound
shipments:

SELECT [user]
FROM Shipments
GROUP BY [user]
HAVING MAX(shipment) = 'Inbound';

As alternative you can rewrite the query you had using NOT EXISTS:

SELECT [user]
FROM Shipments AS A
WHERE NOT EXISTS(SELECT *
FROM Shipments AS B
WHERE B.[user] = A.[user]
AND B.shipment = 'Outbound');

--
Plamen Ratchev
http://www.SQLStudio.com
Nov 14 '08 #2
(np****@gmail.com) writes:
I want to pull back all users that did not have any outbound
shipments. I know I can do it with an inner select:

select user
from users
where user not in
(
select user
from shipments
where shipment = 'Outbound'
)

This seems so simple but I don't know how I'd do it without hitting
the database twice.
Who says that you hit it twice? Keep in mind that SQL is a declarative
language. You state your intent, the optimizer finds out the best way
do it. OK, so in practice it does matter quite a bit how your queries,
but there are also a lot of cases where the optimizer will implement
your query in a different way.

Which is the best way to express a query, is often difficult to tell
beforehand. When you have different ways to go, benchmarking is
about compulsory. But even then you need to be careful, because what
is true for 10000 rows and one distribution, may not be true for 10
million rows and another distribution.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinf...ons/books.mspx

Nov 14 '08 #3
Thank you guys for all your help. I will try different methods and
check the execution plans.
Nov 17 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Prem | last post by:
Hi, I am having many problems with inner join. my first problem is : 1) I want to know the precedance while evaluating query with multiple joins. eg. select Employees.FirstName,...
4
by: Sri | last post by:
I am writing a download process in which i have a condition where i need to join four tables. Each table have lot of data say around 300000 recs. my question is when i am doing the joins on...
1
by: gnog | last post by:
hello we have 3 tables bugs bugid | bugname custom_field_value bugid | fieldid | fieldvalue
0
by: Alpenk | last post by:
I'm trying to update an empty field in a table using the update statment. The table has a number of joins and the update statement comes up with an error when I enter the main table to update. should...
2
by: gkellymail | last post by:
the following query works fine: select link.idx, link.x_table, link.x_id_a, link.x_id_z, a.strandid, b.strandid from link_detail, link, strand A, strand B where link_detail.x_table =...
1
by: imranpariyani | last post by:
Hi i have a severe performance problem with one of my views which has 6 to 8 joins .. any help will be appreciated.. the view is: CREATE OR REPLACE VIEW thsn.trade_view AS SELECT...
2
by: narendra vuradi | last post by:
Hi I have a requirement where in i haev to convert the SQL from Oracle to the one which will run on the SQL server. in the Oracle Query i am doing multiple joins, between some 13 tables. and...
9
by: shapper | last post by:
Hello, I am used to SQL but I am starting to use LINQ. How can I create Left, Right and Inner joins in LINQ? How to distinguish the different joins? Here is a great SQL example:...
6
by: loffing4 | last post by:
I'm trying to write a query that selects the top five members of a forum based on how many total points they have. A member's total points are calculated as follows: (3 * # answers posted) + (10 * #...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.