Column Values Reverse Order in SQL

Step 1. Create table alfa.

CREATE TABLE alfa(
id INT PRIMARY KEY IDENTITY(1,1) NOT NULL,
alfa CHAR(1)
)

Step 2. Now in step two, we need to insert a value in the above-created table.

DECLARE @Start int 
set @Start=65 
while(@Start<=90) 
begin 
 - print char(@Start)
INSERT INTO alfa(alfa) VALUES(CHAR(@Start)); 
set @Start=@Start+1 
end

Step 3. In step 3, we need to understand the logic.

There are some predefined functions in SQL Server that I have used here.

ROW_NUMBER Function in SQL

ROW_NUMBER function is a SQL ranking function that assigns a sequential rank number to each new record in a partition.
When the SQL Server ROW NUMBER function detects two identical values in the same partition, it assigns different rank numbers to both. The rank number will be determined by the sequence in which they are displayed.

SQL ROW_NUMBER Syntax

The syntax for the ROW_NUMBER function in SQL is as follows.

ROW_NUMBER() OVER (
 [PARTITION BY expr1, expr2,…]
 ORDER BY expr1 [ASC | DESC], expr2,…
)

Now, let us look at the different clauses used in the syntax above.

OVER

This clause specifies the window or set of rows that the window function operates. The PARTITION BY and ORDER BY are the two possible clauses of the OVER clause.

Over clause


Similar Articles