SQL Tip #10 – Selecting Random Rows From a Table

Finding random records from a table might not be the important one. However, if you ever find yourself in a position to find random records from a table, then following could be a help:

Below sql queries will return random three(3) records from a table

Method 1: Using NEWID()
NEWID() will help us to generate random result set in a simple way. It creates a unique value of type uniqueidentifier. It will unique for each call. When it is a part of ORDER BY clause then it generates a unique value in memory for each row and then sorted in ascending order.

SELECT TOP 3 * FROM Customer ORDER BY NEWID()

 

Method 2: Using Tablesample()
TABLESAMPLE can quickly return a sample from a large table. TABLESAMPLE clause can be added to any table in the FROM clause. Percentage of rows or number of rows need to be specified as a part of this clause. However, this sampling method will return an approximate number of rows back. If you specify a small number, you might not be able to see any results. Rows returned by this clause are not in order and most likely to be random.

SELECT TOP 3 * FROM Customer Tablesample(1 percent)

If you want to get trully random records then user NEWID().

SQL Tip #9 – All I know about SQL NULL

NULL indicates missing/unknown/inapplicable/absent.

If the above statement makes you think that NULL is useless, then you are mistaken. This is one of the thing that no database developer can avoid it. So it very important to understand how NULL behaves differently in various situations.

Simply we can say, NULL is a placeholder for missing or unknown values at any given time.

At the time of the table creation if we know that values for certain columns won’t be known or will be missing during record insertion then we can make those columns to allow NULL. Later NULL could be replaced with the meaningful values.

Takeaway for NULL:

  • It is very important not to get confused NULL as an space or empty string.
  • NULL is not equal to NULL.
  • Any operation involving NULL will return NULL
  • Comparison operators (<, >, =, <>) cannot be used to find records with the NULL values. IS NULL or IS NOT NULL operators is the way to go.
  • NULL will be ignored for most of the aggregate functions. However, COUNT(*) will include NULL values.

 

Following examples below will return NULL:

SELECT 'Sumon' + NULL
SELECT 0 + NULL
SELECT 10 * NULL

Mood Tracking Iphone App

Have you ever wondered what was your mood like yesterday, what about the day before or last year! 

Wouldn’t it be nice to keep track of your mood? Mood Tracker lets you do that. Mood Tracker is an amazing app allows you to keep track of your “Mood” with a tap!

It’s a very easy to use, simple and handy app is for iphone 3, iphone 4 and iphone 4s

Features:

Lets you to keep track of your mood swings (just tap it)

Over 200 quotes are supplied according to the mood you select.

You can choose to view mood history by date, month or even year!

Download it Today for FREE!!!

Credit:
Sharmila Barua – App Concept 
Sumon Barua – System Architect and Developer 

Very special thanks to Louis Wong 

Contact Us:
ksumonctg@hotmail.com