Saturday, January 7, 2023

Find out the possible pair but the pair should not repeat with respect to the order of the pair

 --SQL Interview question
--Find out the possible pair but the pair should not repeat with respect to the order of the pair 
--Table has 5 rows maximum possible pair is 10







CREATE TABLE COUNTRY (Country_name nvarchar(100))


insert into COUNTRY values ('INDIA'),('US'),('UK'),('Oman'),('Russia')
SELECT REPLICATE ('*',500) As 'INPUT TABLE'
GO
select * from COUNTRY
GO
SELECT REPLICATE ('*',500) As 'OUTPUT TABLE'
go 

WITH COUNTRY_1 AS
(
SELECT ROW_NUMBER () OVER (ORDER BY Country_name DESC ) as'Country_ID',* FROM COUNTRY
)
SELECT CONCAT(C1.Country_name,'-',C2.Country_name) as 'PAIR_NAME' FROM COUNTRY_1  C1 INNER JOIN 
COUNTRY_1 C2 ON C1.Country_ID > C2.Country_ID


No comments:

Post a Comment

"🚀 Delta Lake's Vectorized Delete: The Secret to 10x Faster Data Operations!"

"🚀 Delta Lake's Vectorized Delete: The Secret to 10x Faster Data Operations!" Big news for data engineers! Delta Lake 2.0+ in...