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 TOP. Show all posts
Showing posts with label TOP. Show all posts

Monday, February 11, 2013

TOP 100 PERCENT and ORDER BY obsolete from SQL Server 2005


In SQL 2000 it was an attempt to return all records of the view in the correct order. In SQL 2005 and up you can not simulate ORDER BY using this trick anymore, so SELECT TOP (100) PERCENT has no meaning.

Lets stimulate the scenerio. We create a table testTop100 for our explanation with field id and name

create table testTop100
(id int identity(1,1),
name varchar(10))

Command(s) completed successfully.

Lets insert few records to testTop100

insert into testTop100 values('Abhaya')
insert into testTop100 values('Nitya')
insert into testTop100 values('Ananya')
insert into testTop100 values('Roma')

(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)

Now we will be creating view vwTestTop100 with top 100 Percent and Order by clause

create view vwTestTop100
as
select top 100 percent * from testTop100 order by id desc
go

Command(s) completed successfully.

Lets see the result with simple select query

select * from testTop100
id          name
----------- ----------
1           Abhaya
2           Nitya
3           Ananya
4           Roma

(4 row(s) affected)

Now lets see the result of View which is using Top 100 Percent and order by on id desc

select * from vwTestTop100
id          name
----------- ----------
1           Abhaya
2           Nitya
3           Ananya
4           Roma

(4 row(s) affected)

As we can see SQL Server neither throws error nor it acknowledge the presence of order by clause, it simply ignore the order by clause in View defination.

Now lets try the following

select * from vwTestTop100 order by id desc
id          name
----------- ----------
4           Roma
3           Ananya
2           Nitya
1           Abhaya

(4 row(s) affected)

Now from the output we can see, desired result found

Conclusion :  It is pointless to add ORDER BY clause to the view definition and expect records to come in that ORDER. If you need records ordered, don't put ORDER BY in the view, but rather
select * from myView ORDER BY OrderFields.
This way the correct order will be guaranteed.

Abide by code semantic, u miss all coding fun..Walk off beyond   


Post Reference: Vikram Aristocratic Elfin Share

Thursday, December 29, 2011

Different ways to find TOP N records.


For our explanation we have taken a table ‘parent_tab ‘ having the following seven records data.

select * from parent_tab
first_id    name
----------- --------------------------------------------------
1           Aadarshini
2           Taksha
3           Tanika
4           Vaikunth
5           Ekantika
6           Gargi
7           Sachiv

(7 row(s) affected)

Various ways to achieve this : Our objective is to find first 4 records from the above table

1. Using ROWCOUNT
set rowcount 4
select name from parent_tab
set rowcount 0

name
--------------------------------------------------
Aadarshini
Taksha
Tanika
Vaikunth

(4 row(s) affected)

2. Using ROW_NUMBER
select name from
(
select row_number() over (order by first_id) as rno, name from parent_tab
) as tab where rno <= 4

name
--------------------------------------------------
Aadarshini
Taksha
Tanika
Vaikunth

(4 row(s) affected)

3. Using Top N
select top 4 name from parent_tab

name
--------------------------------------------------
Aadarshini
Taksha
Tanika
Vaikunth

(4 row(s) affected)

If you know any other way to achieve this, please light up your suggestion to me.

Post Reference: Vikram Aristocratic Elfin Share

Wednesday, December 28, 2011

TOP, WITH TIES and ORDER BY? What is WITH TIES clause in SQL Server?

WITH TIES can be used only when TOP and ORDER BY clauses are present in SELECT statement, both the clauses are required in order to use WITH TIES.

Let understand this by taking a small example: Here we are having a table parent_tab with following records:

select * from parent_tab

first_id    name
----------- --------------------------------------------------
1           sneha
3           pratik
3           pratik
1           sneha
4           chitrangada
5           chitrangada
(6 row(s) affected)

Now I am firing a select query with Top clause to retrieve the topmost record from the table [parent_tab] where the name is equal to ‘chitrangada’.

select top 1  * from parent_tab where name like 'chitrangada'

first_id    name
----------- --------------------------------------------------
4           chitrangada

(1 row(s) affected)

Here we can see the top most record has been arrived by the above query.

Now If I want to retrieve the top most record and all the records in the table where the name is equal to ‘chitrangada’?

If  such is my requirement then ‘WITH TIES’ will help you out, let see how:

select top 1 with ties  * from parent_tab where name like 'chitrangada' order by name
first_id    name
----------- --------------------------------------------------
4           chitrangada
5           chitrangada
(2 row(s) affected)

What it does it when you use TOP 1 rows, it will return you only 1 rows, but when used TOP 1 WITH TIES, it will return you all the rows that have same value as that of the last record of TOP 1.

The expected result is based on the column that is specified in ORDER BY. That is it will look for the column used in the ORDER BY to compare its equivalent in rest of the table.

NOTE: WITH TIES Clause can be used only with TOP and ORDER BY, both the clauses are required.

Post Reference: Vikram Aristocratic Elfin Share