Skip to content
Advertisement

Mysql When then and temp variable. Explanation

I have this sql query that I am trying to understand. Can I get a line by line explanation please. I know the first line Initialise variable. Second line gets the minimum value.

Advertisement

Answer

The variables are enumerating the rows for each occupation separately. You can see this by running the subquery to see the results.

It is doing the equivalent of:

This is a better way to write the query in MySQL 8+, because such use of variables has been deprecated. What it is doing is putting the occupations in separate columns:

Turns into:

The intermediate step is:

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement