Compare 3 Numbers using SQL Script
This SQL tutorial shows how to compare 3 numbers using SQL script for Transact-SQL beginners. By changing the below sql codes, developers can easily sort 3 numbers given as variables from biggest to smallest values.
The SQL code first declares variables for input values. These numeric variables are declared in int data type with variable names @s1, @s2 and @s3.
Then 2 internal int numeric data type variables are declared to hold the biggest and smallest numbers. These two internal variables are populated when numbers are compared in pairs with each other in order to compare and sort 3 numbers.
After variable declarations for temporarily data storage, instead of creating temp tables, two logical comparison blocks (SQL IF logical compare commands) are executed in order to compare 3 numbers given as parameters.
The last SELECT statement returns the max value and min value among these 3 numbers given as input for this SQL tutorial for T-SQL learners. You can change the @s1, @s2 and @s3 numeric values in order to test SQL script given above.