Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2018 July SQL Puzzle: Get the last three Records of a table

SQL Puzzle: Get the last three Records of a table

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

Check the below input data and expected output to get the last three records of a table.

Input Data:

1
2
3
4
5
6
7
8
9
10
11
ID Name
----------- ----------
1 ABC
2 XYZ
3 ERZ
4 HYU
5 BNM
6 WER
7 WER
8 CVC
9 ASD

Expected Output:

1
2
3
4
5
ID Name
----------- ----------
7 WER
8 CVC
9 ASD

Create a sample table:

1
2
3
4
5
6
CREATE TABLE tbl_TestTable (ID INT, Name VARCHAR(10))
 
INSERT INTO tbl_TestTable
VALUES (1,'ABC'),(2,'XYZ'),(3,'ERZ')
,(4,'HYU'),(5,'BNM'),(6,'WER')
,(7,'WER'),(8,'CVC'),(9,'ASD')

Solution:

1
2
3
4
5
6
7
;WITH CTE AS
(
SELECT TOP (SELECT COUNT(1)-3 FROM tbl_TestTable) ID
FROM tbl_TestTable
)
SELECT * FROM tbl_TestTable
WHERE ID NOT IN (SELECT ID FROM CTE)

Please try the different solution for this puzzle and share it via comment...

Jul 16, 2018Anvesh Patel
SQL Server: Script to check the file exists or not (Using xp_fileexist )SQL Server: Basic performance test between SELECT INTO and INSERT INTO SELECT
Comments: 11
  1. Sai Reddy
    July 17, 2018 at 5:09 am

    select * from tbl_TestTable
    ORDER BY id ASC
    OFFSET 6 ROWS
    FETCH NEXT 3 ROWS ONLY

    ReplyCancel
  2. Shamanth
    July 17, 2018 at 7:42 am

    Select * From (Select top 3 ID,Name from tbl_TestTable TT order by TT.Id desc) T
    Order By T.ID Asc

    ReplyCancel
    • Dinesh
      July 19, 2018 at 5:50 am

      Good

      ReplyCancel
  3. lymo
    July 25, 2018 at 4:49 am

    select * from tbl_testtable offset (select count(1)-3 from tbl_testtable) limit 3;

    ReplyCancel
    • Aamir Ali
      August 20, 2018 at 11:31 am

      SELECT * FROM public.employees
      ORDER BY employee_number desc limit 3

      ReplyCancel
  4. Sunil Jadhav
    September 25, 2018 at 12:02 pm

    SELECT * FROM tbl_TestTable ORDER BY ID ASC LIMIT 3
    OFFSET (SELECT COUNT(*) FROM tbl_TestTable) – 3;

    ReplyCancel
  5. sudesh kumar
    September 28, 2018 at 1:19 pm

    select * from (
    select top 3 * from tbl_TestTable order by 1 desc
    ) s order by 1

    ReplyCancel
  6. ganag
    October 4, 2018 at 12:46 pm

    ;with cte as (
    select top 3 * from tbl_TestTable order by 1 desc
    )
    select * from cte order by 1 asc

    ReplyCancel
  7. Praful Raut
    October 10, 2018 at 5:42 am

    SELECT ID, NAME FROM #TBL_TESTTABLE WHERE ID >=7

    ReplyCancel
  8. MB
    October 11, 2018 at 1:44 pm

    Answers relying on ID being correlated to the position of a record are probably bad practice given that there are easy general solutions available.

    ReplyCancel
  9. HIC
    April 9, 2019 at 6:28 pm

    select *
    from (
    select * from tbl_TestTop order by id desc)
    where rownum <= 3
    order by id;

    ReplyCancel

Leave a Reply Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

July 16, 2018 11 Comments SQL PuzzleAnvesh Patel, database, database research and development, dbrnd, SQL Advance Query, sql commands, SQL Interview, SQL Problem, SQL Programming, SQL Puzzle, SQL Query, sql quiz, SQL Tips and Tricks, sqlcode
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....