YOUR AD GOES HERE

SQL Interview Questions and Answers | SQL Query To fetch only Even Rows from a Table

Published 07, Oct 2023

Parag Dhawan


Description:
To fetch only even rows from a table, you can use the following SQL query. This query assumes that you have an auto-incrementing primary key or another unique identifier column that allows you to identify even and odd rows:

```sql
SELECT *
FROM (
SELECT *, ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS RowNum
FROM YourTable
) AS Subquery
WHERE RowNum % 2 = 0;
```

In this query:

- `YourTable` should be replaced with the actual name of your table.
- We use a subquery to add a row number to each row in the table using the `ROW_NUMBER()` window function.
- The `RowNum` column is generated and used to identify even rows by checking if the row number modulo 2 (`RowNum % 2`) is equal to 0.

This query will fetch only the even rows from the table based on the row number.

#sql

Releted More Videos

You May Also Like

YOUR AD GOES HERE

YOUR AD GOES HERE