Column Values in Reverse Order in sql

Dynamite Technology
2 min readJun 7, 2022

--

SQL SERVER INTERVIEW QUESTION:
Column Values in 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 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 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.

--

--

Dynamite Technology
Dynamite Technology

Written by Dynamite Technology

0 Followers

Dynamite Technology Private Limited is a software development company and digital marketing agency based in Mumbai India.

No responses yet