'How to find min and max values within a row in SQL?

I have a Numbers table below which contains three numbers (x,y,z):

Numbers table:

+----+----+----+
| x  | y  | z  |
+----+----+----+
| 13 | 15 | 30 |
| 10 | 20 | 15 |
+----+----+----+

I want to produce a new table which contains three more columns:

  1. min_num: smallest number in the row
  2. second_min_num: second smallest number in the row
  3. max_num: largest number in the row

Example output:

+----+----+----+----------+-----------------+----------+
| x  | y  | z  | min_num  | second_min_num  | max_num  |
+----+----+----+----------+-----------------+----------+
| 13 | 15 | 30 |   13     |        15       |    30    |
| 10 | 20 | 15 |   10     |        15       |    20    |
+----+----+----+----------+-----------------+----------+

How can I do this in SQL?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source