473,379 Members | 1,539 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,379 software developers and data experts.

Bizarre Query

Good afternoon,

I have a bizarre question. When running the following query:

select SomeColumnName from TableA where PK_TableA in
(select PK_TableA from TableB)

I get results. This should not be feasible, because the query within
the in clause:

select PK_TableA from TableB

is not possible - there is no PK_TableA column within the TableB table.
Running the sub-query alone gives an error, but when using it as
sub-query in the first statement, I get every row within TableA.

If it helps any, the exact query I'm running is:

select demonstratorid from DirectSalesAgent where DirectSalesAgentId in
(select directsalesagentid from WebsiteSubscriptionPayment)

Shouldn't a query return an error if the sub-query has an invalid
column name?

Jul 5 '06 #1
1 1280
On 5 Jul 2006 15:30:42 -0700, Xeth Waxman wrote:
>Good afternoon,

I have a bizarre question. When running the following query:

select SomeColumnName from TableA where PK_TableA in
(select PK_TableA from TableB)

I get results. This should not be feasible, because the query within
the in clause:

select PK_TableA from TableB

is not possible - there is no PK_TableA column within the TableB table.
Running the sub-query alone gives an error, but when using it as
sub-query in the first statement, I get every row within TableA.

If it helps any, the exact query I'm running is:

select demonstratorid from DirectSalesAgent where DirectSalesAgentId in
(select directsalesagentid from WebsiteSubscriptionPayment)

Shouldn't a query return an error if the sub-query has an invalid
column name?
Hi Xeth,

There are two types of subqueries: correlated and uncorrelated. The
correlated ones refer to columns in the outer query.

If a column name in a subquery is not qualified with table name (or
table alias), SQL Server will first test if it matches a column from a
table used in the subquery. If it doesn't, it will then go on and check
if it matches a column in one of the tables in the outer table, assuming
you want a correlated subquery if it does.

The query you wriite is equivalent to this one (adding table qualifiers
for clarity):

SELECT TableA.SomeColumnName
FROM TableA
WHERE TableA.PK_TableA IN (SELECT TableA.PK_TableA
FROM TableB)

This will return all rows from TableA if at least one row exists in
TableB, or no rows at all if TableB is empty. (If TableB is not empty,
the subqeury will have one row for each row in TableB, but the only
column will have the value of TableA.PK_TableA in each of those rows).

--
Hugo Kornelis, SQL Server MVP
Jul 5 '06 #2

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

Similar topics

4
by: Alan Little | last post by:
This is very bizarre. Could someone else have a look at this? Maybe you can see something I'm overlooking. Go here: http://www.newsletters.forbes.com/enews/admin/deliver.php4 U: bugtest P:...
11
by: Frances Del Rio | last post by:
this is so bizarre, and don't even know if this is right place to ask, but don't know where else: about two days I changed webhosting, changed DNS for my domain, francesdelrio.com, now when I...
1
by: Richard | last post by:
Here's a Bizarre one: I have a C# service program. The service registers and runs fine on one machine but when I copy the EXE to a second machine and try to use InstallUtil to register the...
1
by: Michael Carr | last post by:
I have created a website that responds to the following two domain names: logintest.carr-engineering.com logintest.onualumni.org If you open either of these websites on their own, everything...
3
by: jhcorey | last post by:
I have a stored procedure that takes 14 parameters, including two for start date and end date. The procedure then creates a sql string and does an 'exec sql'. The stored procedure always works...
1
by: zoehart | last post by:
I'm working with VBScript to build a text email message. I'm seeing a variety of bizarre formatting issues. The following lines of code MT = MT & vbCrLf & "Card Type: " & CardType MT = MT &...
29
by: wizofaus | last post by:
I previously posted about a problem where it seemed that changing the case of the word "BY" in a SELECT query was causing it to run much much faster. Now I've hit the same thing again, where...
2
by: billelev | last post by:
Hi There, I am performing a UNION query on a Table containing % formatted values with a Query, also containing % formatted values. The resulting union query displays the table % values as...
12
by: Gerhard | last post by:
This is bizarre... Im having problems with the combobox AfterUpdate event: Im running Access 2003. I created an unbound combobox with 3 columns on a form. The Row Source is from a table....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.