All of Query was successfully tested and running well on Microsoft SQL Server 2008

Jumat, 31 Agustus 2012

select top sql

SQL Server Select Top Statement:
SELECT TOP number | number PERCENT ColumnName(s) FROM TableName

Example 1:

select TOP 1 * from (
    select ('Umar')PersonName union
    select ('Zubair') union
    select ('Ali') union
    select ('Muhammad') union
    select ('Bilal')
)v0 Order by PersonName Asc

#Results:
PersonName
Ali

Example 2:
select TOP 1 * from (
    select ('Umar')PersonName union
    select ('Zubair') union
    select ('Ali') union
    select ('Muhammad') union
    select ('Bilal')
)v1 Order by PersonName Desc

#Results:
PersonName
Zubair

Example 3:
select TOP 3 * from (
    select ('Umar')PersonName union
    select ('Zubair') union
    select ('Ali') union
    select ('Muhammad') union
    select ('Bilal')
)v2 Order by PersonName Asc


#Results:
PersonName
Ali
Bilal
Muhammad

Example 4:
select TOP 3 * from (
    select ('Umar')PersonName union
    select ('Zubair') union
    select ('Ali') union
    select ('Muhammad') union
    select ('Bilal')
)v2 Order by PersonName Desc


PersonName 
Zubair
Umar
Muhammad

Example 5:
select TOP 30 PERCENT * from (
    select ('Umar')PersonName union
    select ('Zubair') union
    select ('Ali') union
    select ('Muhammad') union
    select ('Bilal')
)v2 Order by PersonName Desc

#Results:
PersonName 
Zubair
Umar

Tidak ada komentar:

Posting Komentar