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

Can any one explain this query behavior

pai
db_TBO db_TBT
-----------------------------
--------------------------------------------------------
TBOID | Date TBTID | TBOID | Date
---------------------------- ---------------------------------------------------------
rp01 | 01/08/2006 ap01 | rp01 | 02/08/2006
many rows ap02 | rp01 | 05/08/2006
ap03 | rp03 | 04/08/2006

I want to find TBTTD field of table db_TBT
who have of db_TBO date db_TBT table date
and TBOID should be 'rp01' of both tables

when I give a query as it works as I needed

Select TBT.Date
from db_TBO TBO , db_TBT TBT
where TBT.TBOID = 'rp01'
and TBO.TBOID ='rp01'
and TBO.Date TBT.Date

My doubt is when I run the following query
" Select TBT.Date from db_TBO TBO , db_TBT TBT where TBT.TBOID = 'rp01'
"
it gives me more than 7 records

when I run the query using some change
" Select TBT.Date , TBT.TBID from db_TBO TBO , db_TBT TBT where
TBT.TBOID = 'rp01' "
it gives more rows than previous row

Can u give me any explations.

Thanks
Pai

Aug 24 '06 #1
1 1494
On 23 Aug 2006 22:54:21 -0700, pai wrote:
> db_TBO db_TBT
-----------------------------
--------------------------------------------------------
TBOID | Date TBTID | TBOID | Date
---------------------------- ---------------------------------------------------------
rp01 | 01/08/2006 ap01 | rp01 | 02/08/2006
many rows ap02 | rp01 | 05/08/2006
ap03 | rp03 | 04/08/2006

I want to find TBTTD field of table db_TBT
who have of db_TBO date db_TBT table date
and TBOID should be 'rp01' of both tables
(snip)
>
My doubt is when I run the following query
" Select TBT.Date from db_TBO TBO , db_TBT TBT where TBT.TBOID = 'rp01'
"
it gives me more than 7 records
Hi Pai,

Of course it does. The FROM clause lists two tables, comma-seperated.
That means that you'll get a cross join, also known as cartesian
product, between the two tables: each row from the first table will be
paired with each row from the second table. After that (*), the WHERE
clause removes rows based on TBT.TBOID. The result set willl include
only TBT-rows with TBOID equal to 'rp01' - but each of those rows will
still be paired against each of the rows in db_TBO.
>when I run the query using some change
" Select TBT.Date , TBT.TBID from db_TBO TBO , db_TBT TBT where
TBT.TBOID = 'rp01' "
it gives more rows than previous row
I don't understand this. The only difference between this query and the
previous query is the addition of one more column in the WHERE clause.
That shoould never affect the number of rows returned. Are you sure that
you didn't make a mistake when you copied the SQL into your message?

(snipped from above:)
>when I give a query as it works as I needed

Select TBT.Date
from db_TBO TBO , db_TBT TBT
where TBT.TBOID = 'rp01'
and TBO.TBOID ='rp01'
and TBO.Date TBT.Date
Indeed. This query also starts (*) with the cross join, but then retains
only rows with both TBT.TBOID and TBO.TBOOID equal to 'rp01'. That means
that you're left with each TBT-row for 'rp01' paired to each TBO-row for
'rp01'. These results are then further filtered by the date comparison.

I think your problems arise out of the use of the "old-style" FROM
clause. The newer style, with explicit joins, makes it much harder to
make this kind of mistakes since it forces you to explicitly write down
the join criteria:

SELECT some columns
FROM db_TBO AS TBO
INNER JOIN db_TBT AS TBT
ON TBO.TBOID = TBT.TBOID
WHERE TBO.Date TBT.Date;

(*) The order of evaluation described here is only a logical explanation
of the process. The query optimizer is free to (and definitely will, in
this case) change the order of evaluation, as long as the results remain
the same.

--
Hugo Kornelis, SQL Server MVP
Aug 24 '06 #2

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

Similar topics

1
by: David | last post by:
Hi everyone. I have read every page that Google returns on this topic, but can't find anything that resolves my problem. Basically, I have an Access Database that does a number of different...
25
by: REH | last post by:
Someone more articulate than me please explain to Ioannis Vranos why the following two programs both exhibit undefined behavior: > #include <iostream> > #include <cstring> > > class SomeClass...
0
by: Bruce D | last post by:
I'm trying to understand MySQL better. I can't seem to figure out why the second query takes 2 minutes and the first query takes 12 seconds. It should be noted that the table, KBM, has 250...
10
by: Greg Stark | last post by:
This query is odd, it seems to be taking over a second according to my log_duration logs and according to psql's \timing numbers. However explain analyze says it's running in about a third of a...
3
by: Mark Harrison | last post by:
I have indexed two columns in a table. Can somebody explain to me why the first query below uses an Index Scan while the second uses a Seq Scan? Many TIA! Mark planb=# \d abcs Table...
22
by: Jaspreet | last post by:
I was recently asked this question in an interview. Unfortunately I was not able to answer it and the interviewer made a decision on my C strengths (or weekness) based on this single question and...
7
by: bryant | last post by:
Hi all. I am new to ASP and working in Expression Web. The following query displays the information I need in the gridview for a single record. SELECT "OE_HDR"."ORD_NO", "OE_HDR"."CUST_NAM",...
2
by: davidaustinarcher | last post by:
Hello, I run the following query on a database (SQL 2005): delete from person where pers_companyid in (select pers_companyid from company where comp_expiry is not null) I wanted to delete...
7
by: skaushik | last post by:
Hi all, I was working on a SQL query where a history table is joined with a small table to get some information. There is an index on the history table column but the explain plan tells that there...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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...

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.