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...