How to fetch alternate records from a table in sql server


CREATE TABLE STUDENTS
(
      STUDENT_ID int NULL,
      [STUDENT_NAME] [varchar](50) NULL,
      DOB date NULL,
      DEPARTMENT_ID int NULL,
      DOJ date NULL
)

INSERT STUDENTS (STUDENT_ID, STUDENT_NAME, DOB, DEPARTMENT_ID, DOJ)
VALUES (1, N'BALACHANDAR', '1983-10-28', 2, GETDATE())

INSERT STUDENTS (STUDENT_ID, STUDENT_NAME, DOB, DEPARTMENT_ID, DOJ)
VALUES (2, N'PREMKUMAR', '1986-06-17', 1, GETDATE())

INSERT STUDENTS (STUDENT_ID, STUDENT_NAME, DOB, DEPARTMENT_ID, DOJ)
VALUES (3, N'MADHUSOODHAN', '1988-06-30', 3, GETDATE())

INSERT STUDENTS (STUDENT_ID, STUDENT_NAME, DOB, DEPARTMENT_ID, DOJ)
VALUES (4, N'SAGARBABU', '1995-10-05', 4, GETDATE())

INSERT STUDENTS (STUDENT_ID, STUDENT_NAME, DOB, DEPARTMENT_ID, DOJ)
VALUES (5, N'SRAVANTHI', '1988-04-06', 5, GETDATE())

SELECT * FROM STUDENTS



SELECT * FROM STUDENTS WHERE STUDENT_ID%2=0

SELECT STUDENT_ID,STUDENT_NAME,DOB,DEPARTMENT_ID,DOJ
FROM(SELECT ROW_NUMBER()OVER (ORDER BY STUDENT_ID)AS ROW,* FROM STUDENTS)
A WHERE ROW%2=0



Comments

Popular posts from this blog

Failed to execute the package or element. Build errors were encountered

Restore of database 'DataBase_Name' failed. (Microsoft.SqlServer.Management.RelationalEngineTasks)

On-premises data gateway December release