title: How to alternative male and female in SQL Server
body: <p>I have a question about SQL Server: how to get the alternate data of male and female data </p>
  
  <pre><code>CREATE TABLE [dbo].[EmpGenders]
  (
      [id] [INT] NULL,
      [Name] [VARCHAR](50) NULL,
      [Gender] [VARCHAR](50) NULL
  ) 
  
  INSERT INTO [dbo].[EmpGenders] ([id], [Name], [Gender]) 
  VALUES (1, N'Manish', N'Male'), (2, N'Nayak', N'Male'),
         (3, N'Martin', N'Male'), (4, N'Roy', N'Male'),
         (5, N'Hardik', N'Male'), (6, N'Manisha', N'Female'),
         (7, N'Kruti', N'Female'), (8, N'Maria', N'Female')
  </code></pre>
  
  <p>Based on the above data, I want an output like this:</p>
  
  <pre><code>Id | Name    | Gender 
  ---+---------+---------
  1  | Manish  | Male
  6  | Manisha | Female
  2  | Nayak   | Male
  7  | Kruti   | Female
  3  | Martin  | Male
  8  | Maria   | Female
  4  | Roy     | Male
  5  | Hardik  | Male 
  </code></pre>
  
  <p>I tried this SQL statement:</p>
  
  <pre><code>select 
      id, name, gender  
  from 
      (select 
           *,
           row_number() over (order by id, gender) as rn
       from  
           [Test].[dbo].[EmpGenders]) a
  where  
      rn % 2 = 1 
  
  union all
  
  select 
      id, name, gender  
  from 
      (select 
           *, row_number() over (order by id desc, gender desc) as rn
       from  
           [Test].[dbo].[EmpGenders]) a  
  where  
      rn % 2 = 0
  </code></pre>
  
  <p>but that query not returning the expected result.</p>
  
  <p>Can you please tell me how to write a query to achieve this task in SQL Server?</p>