0% found this document useful (0 votes)
4 views3 pages

SQL Query Execution Order Explained

The SQL query retrieves the youngest sailor's age for each rating from the 'Sailors' table, filtering for sailors aged 18 or older. It groups the results by rating and only includes groups with at least two sailors. The final output displays the rating alongside the youngest age in each qualifying group.

Uploaded by

avatharmanohar78
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

SQL Query Execution Order Explained

The SQL query retrieves the youngest sailor's age for each rating from the 'Sailors' table, filtering for sailors aged 18 or older. It groups the results by rating and only includes groups with at least two sailors. The final output displays the rating alongside the youngest age in each qualifying group.

Uploaded by

avatharmanohar78
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

SQL Query Execution Steps (Ordered Execution)

SQL Query:

SELECT Rating, MIN(Age) AS youngest_age


FROM Sailors
WHERE Age >= 18
GROUP BY Rating
HAVING COUNT(*) >= 2;

Step 1: FROM Clause (Selecting Table)


Loads the 'Sailors' table.

| Sid | Sname | Rating | Age |


|------|--------|--------|------|
| 22 | John | 7 | 45 |
| 23 | Mike | 1 | 33 |
| 24 | Alex | 8 | 55.5 |
| 25 | Sam | 8 | 25.5 |
| 26 | Tom | 10 | 35 |
| 27 | Jerry | 7 | 35 |
| 28 | Chris | 9 | 40 |
| 29 | Steve | 3 | 25.5 |
| 30 | Rick | 3 | 63.5 |

Step 2: WHERE Clause (Filter Eligible Sailors)


Filters sailors who are at least 18 years old.

| Sid | Sname | Rating | Age |


|------|--------|--------|------|
| 22 | John | 7 | 45 |
| 23 | Mike | 1 | 33 |
| 24 | Alex | 8 | 55.5 |
| 25 | Sam | 8 | 25.5 |
| 26 | Tom | 10 | 35 |
| 27 | Jerry | 7 | 35 |
| 28 | Chris | 9 | 40 |
| 29 | Steve | 3 | 25.5 |
| 30 | Rick | 3 | 63.5 |

Step 3: GROUP BY Clause (Grouping by Rating)


Groups sailors based on their rating.

| Rating | Sailors (Age) |


|--------|--------------------|
| 7 | (45, 35) |
| 1 | (33) |
| 8 | (55.5, 25.5) |
| 10 | (35) |
| 9 | (40) |
| 3 | (25.5, 63.5) |

Step 4: HAVING Clause (Filter Ratings with at Least 2 Sailors)


Removes ratings with fewer than 2 sailors.

| Rating | Sailors (Age) |


|--------|--------------------|
| 7 | (45, 35) |
| 8 | (55.5, 25.5) |
| 3 | (25.5, 63.5) |

Step 5: SELECT Clause (Find Minimum Age in Each Group)


Finds the youngest sailor in each rating group.

| Rating | Youngest Age |


|--------|-------------|
| 7 | 35 |
| 8 | 25.5 |
| 3 | 25.5 |

Summary of Execution Order:

1. FROM → Load the 'Sailors' table.


2. WHERE → Filter sailors aged 18 or older.
3. GROUP BY → Group sailors by 'Rating'.
4. HAVING → Keep only ratings with at least 2 sailors.
5. SELECT → Retrieve the minimum age for each rating.

You might also like