Thursday, October 20, 2011

How to fetch nth row from SQL Server 2008 R2 / 2005


Example Code:

Note: Code explained below

WITH temp_table AS
(
    SELECT TOP (SELECT COUNT(*) FROM [table_1])
        firstname, lastname, mobile, email,
        ROW_NUMBER() OVER (ORDER BY zip_code) AS row_no
    FROM
        [table_1]
    ORDER BY zip_code
)
SELECT
    first_name,
    last_name,
    email

FROM
    temp_table
WHERE
    row_no BETWEEN 1 AND 10


Code Explanation:

WITH temp_table AS
--create a temporary table named temp_table
(
    SELECT TOP (SELECT COUNT(*) FROM [table_1])
    --table_1 is the original table
    --top is an essential keyword when you use orderby keyword in this sub query else you can ignore TOP keyword
    -- I have fetched the rows count to fetch the entire rows, this means no difference when considered as a independent query but just to obey the inner query Syntax, that is TOP keyword should be used while using order by in the sub query
        firstname, lastname, mobile, email,
        --just fetching some fields
        ROW_NUMBER() OVER (ORDER BY zip_code) AS row_no
        --generate row number with a custom field in the temp_table for the rows fetched from the original table
    FROM
        [table_1]
        --original table
    ORDER BY zip_code
    --order by inside sub query is the complex part. I remind, use TOP keyword when using order by keyword.
)
SELECT
    first_name,
    last_name,
    email
    --simple select query from the temp_table
FROM
    temp_table
WHERE
    row_no BETWEEN 1 AND 10
    --now we can set between values to fetch records. Hope this is self explanatory, you can set 11 and 20 to fetch the next set of 10 records and so on.


Please share your comments and suggestions. You may also post your questions and doubts here.

Wednesday, October 19, 2011

How to fetch first 10 rows in SQL Server 2008 R2 / 2005

It is pretty simple to fetch first n records in SQL Server 2008 R2. Following is the syntax and example SQL Query to fetch the first 10 rows from a SQL Server 2008 R2 / 2005 database.

Syntax:

SELECT TOP (n) * FROM

--n could be any unsigned bigint value
-- You may also use TOP n without parenthesis. Using TOP keyword without parenthesis gives backward compatibility with SQL Server 2000 but I guess most don't require such a backward compatibility. So it is recommended by Microsoft to use TOP with parenthesis.


For advanced users: 

n is actually an expression. That is, it is capable of performing calculations to derive the final number of records to be retrieved.

Optionally you may also use PERCENT keyword next to the n to denote the input for fetching number of records is on percentage. That is, the following Example 2 will fetch 10 records from a table containing 100 records. When n is used as percentage, n will be treated as float.

Example 1:

SELECT TOP (10) * FROM  employees



Example 2:

SELECT TOP (10) PERCENT * FROM employees


It is as simple as above. I am publishing this on my blog because this is the most frequent database related question most people asked me.

Thursday, October 06, 2011

Steve Jobs - The i Genius

Steve Jobs, the i Genius, co-founder of Apple Computers, passed away last night. I really miss him as a tech brain and a genius business man who brought world a new way of computing.


Steve Jobs on Apple Home Page Today

Remembering Steve

Steve Jobs on Apple Home Page

Top 3 Companies by Market Share in NASDAQ (USA)
  1. Apple Inc - $350.67 Billion
  2. Microsoft - $216.91 Billion
  3. IBM - $211.21 Billion
Read more about Apple & Steve Jobs on wikipedia

What's inside the link?
Steven Paul "Steve" Jobs (February 24, 1955 – October 5, 2011) was an American computer entrepreneur and inventor. He was co-founder, chairman, and chief executive officer of Apple Inc. Jobs also previously served as chief executive of Pixar Animation Studios; he became a member of the board of directors of The Walt Disney Company in 2006, following the acquisition of Pixar by Disney. He was credited in Toy Story (1995) as an executive producer. Click here to read more