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 October SQL Puzzle: SQL Advance Query – Get last five rows without ORDER BY

SQL Puzzle: SQL Advance Query – Get last five rows without ORDER BY

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 list five records of a table without the use of an ORDER BY clause.

Input Data:

1
2
3
4
5
6
7
8
9
10
11
12
ID Name
----------- ----------
1 ABC
2 XYZ
3 FEW
4 CAZ
5 CVB
6 DWA
7 EDZ
8 HTR
9 ABC
10 MNB

Expected Data:

1
2
3
4
5
6
7
ID Name
----------- ----------
6 DWA
7 EDZ
8 HTR
9 ABC
10 MNB

Create table with sample data:

1
2
3
4
5
6
7
CREATE TABLE tbl_TestTop (ID INT, Name VARCHAR(10))
 
INSERT INTO tbl_TestTop VALUES
(1,'ABC'),(2,'XYZ'),(3,'FEW'),(4,'CAZ')
,(5,'CVB'),(6,'DWA'),(7,'EDZ'),(8,'HTR')
,(9,'ABC'),(10,'MNB')
GO

Solution:

1
2
3
4
5
6
7
8
SELECT *
FROM tbl_TestTop
WHERE ID NOT IN (
SELECT TOP (
(SELECT count(*) FROM tbl_TestTop) - 5
) ID
FROM tbl_TestTop
)

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

Oct 22, 2018Anvesh Patel
SQL Server: How to Aggregate data and perform Multiplication?SQL Server: Script to find the list of Triggers
Comments: 7
  1. Sai Reddy
    October 31, 2018 at 9:26 am

    SELECT * FROM tbl_TestTop WHERE ID>(SELECT COUNT(*) FROM tbl_TestTop)-5

    ReplyCancel
  2. Nimit Gupta
    November 11, 2018 at 5:22 pm

    select * from tbl_TestTop offset 5 rows fetch next 5 rows only;

    ReplyCancel
  3. sandeep
    January 10, 2019 at 12:27 pm

    select * from tbl_TestTop order by id OFFSET (SELECT COUNT(*)-5 FROM tbl_TestTop) ROWS FETCH NEXT 5 ROWS ONLY;

    ReplyCancel
  4. HIC
    April 9, 2019 at 6:11 pm

    SELECT * FROM tbl_TestTop
    WHERE id > (select max(id) from tbl_TestTop where ROWNUM <= (select count(*) – 5 as id from tbl_TestTop));

    SELECT * FROM tbl_TestTop
    minus
    select * from tbl_TestTop where ROWNUM <= (select count(*) – 5 as id from tbl_TestTop);

    ReplyCancel
  5. Nikhil Bhosale
    August 14, 2019 at 9:29 am

    ————————————————————————–
    My Query is Write if the ID is Auto-Increment Field
    ————————————————————————–

    Select Top 5 *
    from tbl_TestTop
    Where ID NOT IN
    (Select ID From tbl_TestTop where ID between (Select Min(ID)From tbl_TestTop) AND (Select MAX(ID)-5From tbl_TestTop) )

    ReplyCancel
  6. Honey Jain
    April 7, 2020 at 6:34 pm

    select * from tbl_TestTop
    HAVING ID>(select Max(ID) from tbl_TestTop) – 5;

    ReplyCancel
  7. Tarash Jain
    April 7, 2020 at 6:34 pm

    SELECT ID, name
    FROM
    tbl_TestTop
    HAVING ID>(SELECT MAX(ID) FROM tbl_TestTop)- 5 ;

    ReplyCancel

Leave a Reply to Honey Jain Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

October 22, 2018 7 Comments SQL PuzzleAnvesh Patel, database, database research and development, dbrnd, SQL Advance Query, SQL Interview, SQL Problem, SQL Programming, SQL Puzzle, SQL Query, SQL Tips and Tricks
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....