About Me

My photo
Mumbai, Maharastra, India
He has more than 7.6 years of experience in the software development. He has spent most of the times in web/desktop application development. He has sound knowledge in various database concepts. You can reach him at viki.keshari@gmail.com https://www.linkedin.com/in/vikrammahapatra/ https://twitter.com/VikramMahapatra http://www.facebook.com/viki.keshari

Search This Blog

Showing posts with label Logical Query Processing Phase. Show all posts
Showing posts with label Logical Query Processing Phase. Show all posts

Thursday, June 26, 2014

Logical Query Processing- Fuzzy Query with Order By Clause



Today it happened, I was called by my fellow programmer, he saw me a piece of T-SQL Code and asks few question on it, I will try to replicate the same code here.
There was a query somehow like this

select sub_group,sub_group,name,name from #temp
where sub_group = 'Furniture'
order by sub_group

And the query was failing, and asked me to justify the why it failing. That’s good, I find it interesting to explain the logic behind the scene through Logical Query Processing.  Yeah ;) I didn’t ask tea this time for this: P

Let’s formulate the same scenario by creating a sample table.

create table #temp
(id int identity(1,1),
sub_group varchar(10),
name varchar(10))
Command(s) completed successfully.

Lets insert few records in it.

insert into #temp
select 'Furniture','chair' union all
select 'Vechile','Maruti' union all
select 'Furniture','Desk' union all
select 'Furniture','Dine' union all
select 'Vechile','Honda' union all
select 'H.Vechile','Mahindra'

Now we can have the same Select query. Lets fire the query and see  the output.

select sub_group,sub_group,name,name from #temp
where sub_group = 'Furniture'
order by sub_group
Msg 209, Level 16, State 1, Line 32
Ambiguous column name 'sub_group'.

Now lets take the error, and dig the background of it. Now it says Ambiguous Column name.

If you see in the select list, we have called this column two times, now if we go through logical query processing, it says for this query.
1st from clause gets executed i.e #temp and record set will build up say RS1.
2nd where by clause gets executed which will filter the data and remain with the data which has ‘Furniture’ as sub_group and create a new record set say RS2
3rd Now the Select clause will gets executed with form one more column of same name as sub_group. So the new record set will have this many column “sub_group, sub_group, name, name” say this record set as RS3
4th Now Oder By clause gets the chance, it operate on RS3 record set, now if you see in query, the order by clause is on sub_group, so when it tries to order the RS3 data, it will found two column with same name sub_group, and optimizer find it difficult which column to pick up, thus it throws Ambiguous column error.

Solution to this problem is: make an alias to one of the sub_group column.

select sub_group,sub_group SB ,name,name from #temp
where sub_group = 'Furniture'
order by sub_group

Run the query and you will get your result.

sub_group  SB         name       name
---------- ---------- ---------- ----------
Furniture  Furniture  chair      chair
Furniture  Furniture  Desk       Desk
Furniture  Furniture  Dine       Dine


You just have to make time for your coding and keep it balance J
 

Post Reference: Vikram Aristocratic Elfin Share

Thursday, June 12, 2014

Where Predicate in “JOIN ON” Clause


Today it happened that my junior Engineer came to my desk and asks me a good question, his statement was like this

“What difference it makes if we write filter condition with ON CLAUSE along with join”

His mean to say; All those condition which we write in “WHERE” clause, if we write all those condition along with join, will it make any difference in the output of query?

Let’s stimulate the same, by doing a small practical.

Here we are creating two table, table1 as parent table and table2 as child with common field for joining “id” in parent table and “p_id” as child field as foreign table.

create table table1
(id int primary key,
name varchar(10))
Command(s) completed successfully.

create table table2
(id int primary key,
address varchar(15),
p_id int references table1(id))
Command(s) completed successfully.

Let’s insert some records in table1

insert into table1
select 1,'Lopa' union all
select 2, 'Abhipsha' union all
select 3,'Rubi' union all
select 4,'Soumya' union all
select 5,'Diksha'

Now lets insert some child records in table2

insert into table2
select 1,'Vegas', 1 union all
select 2,'New Jersey', 2 union all
select 3,'San Diego',3 union all
select 4,'San Francisco',3 union all
select 5,'Los Angeles',5

select * from table1
id          name
----------- ----------
1           Lopa
2           Abhipsha
3           Rubi
4           Soumya
5           Diksha

select * from table2
id          address         p_id
----------- --------------- -----------
1           Vegas           1
2           New Jersey      2
3           San Diego       3
4           San Francisco   3
5           Los Angeles     5

Lets write inner join query joining table1 with table2 on id of table1 with p_id of table2

select * from table1
inner join table2
on table1.id = table2.p_id

id          name       id          address         p_id
----------- ---------- ----------- --------------- -----------
1           Lopa       1           Vegas           1
2           Abhipsha   2           New Jersey      2
3           Rubi       3           San Diego       3
3           Rubi       4           San Francisco   3
5           Diksha     5           Los Angeles     5

Now here we will be adding a condition along with the join.  The condition is the p_id should be 3. And the condition is written with WhERE clause.

select * from table1
inner join table2
on table1.id=table2.p_id
where table2.p_id=3
id          name       id          address         p_id
----------- ---------- ----------- --------------- -----------
3           Rubi       3           San Diego       3
3           Rubi       4           San Francisco   3

Now we will write the same condition p_id equals 3 but this time the condition is written along with the JOIN clause.

select * from table1
inner join table2
on table1.id = table2.p_id and table2.p_id=3
id          name       id          address         p_id
----------- ---------- ----------- --------------- -----------
3           Rubi       3           San Diego       3
3           Rubi       4           San Francisco   3

Here we saw writing condition either with JOIN clause or WHERE did not make any difference with INNER JOIN.

Lets try the same with OUTER join.

select * from table1
left outer join table2
on table1.id = table2.p_id

id          name       id          address         p_id
----------- ---------- ----------- --------------- -----------
1           Lopa       1           Vegas           1
2           Abhipsha   2           New Jersey      2
3           Rubi       3           San Diego       3
3           Rubi       4           San Francisco   3
4           Soumya     NULL        NULL            NULL
5           Diksha     5           Los Angeles     5

Now we will put some filter criteria.

select * from table1
left outer join table2
on table1.id=table2.p_id
where table2.p_id=3

id          name       id          address         p_id
----------- ---------- ----------- --------------- -----------
3           Rubi       3           San Diego       3
3           Rubi       4           San Francisco   3

Here we saw after left outer join we have added a criteria of p_id =3… it filter the result set to p_id  equals 3.

Now we will try to write the same query but remove the where predicate and write the table2.p_id=3 condition along with join clause and check the result.

select * from table1
left outer join table2
on table1.id = table2.p_id and table2.p_id=3

id          name       id          address         p_id
----------- ---------- ----------- --------------- -----------
1           Lopa       NULL        NULL            NULL
2           Abhipsha   NULL        NULL            NULL
3           Rubi       3           San Diego       3
3           Rubi       4           San Francisco   3
4           Soumya     NULL        NULL            NULL
5           Diksha     NULL        NULL            NULL

Result are not same in case of Outer Join.

Conclusion: According to Logical Query Processing, the ON clause executed first then the where clause, Now here with the above Outer Join example, the table2 was filtered with p_id =3 condition then the result was joined with table1. So be aware if you are writing condition with ON CLAUSE with OUTER JOIN.

Blend your code with TEA, toast up your logic and enjoy your life with programming forever! J
 

Post Reference: Vikram Aristocratic Elfin Share