-2

I want Last Five Record from table. Condition is Column contains duplicate and null values.

CREATE TABLE [dbo].[Student] (
    [Student_Name] [VARCHAR](50) NULL
)

INSERT INTO Student
VALUES
      ('Mukesh')
    , ('Vinod')
    , ('Mukesh')
    , (NULL)
    , ('Shree')
    , ('Raj')
    , (NULL)
    , ('Abhijit')
    , ('Raju')
    , ('Sharon')
    , ('Ashok')
    , ('Meena')
    , ('Mukesh')
    , (NULL)

SELECT * FROM Student

enter image description here

Note: I want Last Five Record From Above Table

Result like that: enter image description here

Devart
  • 119,203
  • 23
  • 166
  • 186
  • 4
    How do you define the last records? – Felix Pamittan Mar 14 '16 at 07:21
  • 1
    Without an explicit `ORDER BY` clause, the order of the result set will not be guaranteed. So unless you specify which column define the _last records_, the answers you'll be getting here will not be accurate. – Felix Pamittan Mar 14 '16 at 07:29
  • last record is last row of entire table data – Surendra Mar 14 '16 at 07:30
  • I suggest you read [this](http://stackoverflow.com/questions/26236352/default-row-order-in-select-query-sql-server-2008-vs-sql-2012). – Felix Pamittan Mar 14 '16 at 07:31
  • 1
    Why do you want to do this? Why is the logic behind your desired result? – FLICKER Mar 14 '16 at 07:31
  • 2
    This can't be done. without a column that specifies the order of inserted values (like an identity column or a create date column) there is no possible way to guarantee the order of the select statement will be the same order the records was inserted to the database. – Zohar Peled Mar 14 '16 at 07:33
  • suppose i have 120 record in student table, now i want last 5 recently inserted record in student table. I don't have any student id or serial no column which can be describe order of data – Surendra Mar 14 '16 at 07:37
  • Well then I guess you sool. Unless you can add a column to specify the order of inserts (like identity or create date) it simply can't be done. – Zohar Peled Mar 14 '16 at 07:39
  • 2
    You can try Devart's answer, but it will not return the same set of data everytime. – Felix Pamittan Mar 14 '16 at 07:40

3 Answers3

1
DECLARE @Student TABLE (Student_Name VARCHAR(50)) 

INSERT INTO @Student
VALUES
      ('Mukesh'), ('Vinod'), ('Mukesh'), (NULL)
    , ('Shree'), ('Raj'), (NULL), ('Abhijit'), ('Raju')
    , ('Sharon'), ('Ashok'), ('Meena'), ('Mukesh'), (NULL)

SELECT TOP(5) Student_Name
FROM (
    SELECT *, rn = ROW_NUMBER() OVER (ORDER BY 1/0)
    FROM @Student
) t
ORDER BY rn DESC
Devart
  • 119,203
  • 23
  • 166
  • 186
0
declare @c int
set @c = (select COUNT(*) from Student)
select *
from
(select ROW_NUMBER() over (order by student_id) as row, *
from Student) t
where t.row between (@c-5) and @c

Try this one.

Update:

declare @c int
set @c = (select COUNT(*) from Student)
select *
from
(select ROW_NUMBER() over(order by (select 1)) as row, *
from Student) t
where t.row between (@c-5) and @c
ThanhPT
  • 57
  • 7
-4

Use DISTINCT to remove duplicates from the result set. example:

SELECT DISTINCT expressions
FROM tables
[WHERE conditions];

and use LIMIT to limit the result count

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 5
    `LIMIT` is **not** a valid keword/operation in **Microsoft SQL Server** or standard ANSI / ISO SQL - – marc_s Mar 14 '16 at 07:28