sql question set 1

SQL Question #1: Repeating Rows Based on Value

Input Table:

A single-column table with values:

Value
----- 1 2 3

Expected Output:

Each value should be repeated as many times as its value:



Value ----- 1 2 2 3 3 3

Question:

Write an SQL query to return each number repeated as many times as its value.
For example, 1 should appear once, 2 should appear twice, 3 thrice, and so on.


SQL Question #2: Get the Start and End Cities per Journey

Input Table (TravelRoutes):

IdFromTo
1HyderabadBangalore
1BangaloreChennai
1ChennaiKochi
2BangaloreHyderabad
2HyderabadDelhi
3DelhiChennai

Expected Output:

IdFromTo
1HyderabadKochi
2BangaloreDelhi
3DelhiChennai

Question:

Write an SQL query to find the starting and ending cities of each journey (Id).

Assume the path is connected and ordered — you need to find the first From city and the last To city for each Id

Comments

Popular posts from this blog

SCD Type 2 in MySQL/ coforge

3 days running average / capco