sql question set 1
✅ SQL Question #1: Repeating Rows Based on Value
Input Table:
A single-column table with values:
Expected Output:
Each value should be repeated as many times as its value:
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):
| Id | From | To |
|---|---|---|
| 1 | Hyderabad | Bangalore |
| 1 | Bangalore | Chennai |
| 1 | Chennai | Kochi |
| 2 | Bangalore | Hyderabad |
| 2 | Hyderabad | Delhi |
| 3 | Delhi | Chennai |
Expected Output:
| Id | From | To |
|---|---|---|
| 1 | Hyderabad | Kochi |
| 2 | Bangalore | Delhi |
| 3 | Delhi | Chennai |
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
Post a Comment