User-Defined Variables in are a way to store temporary values that can be used within a session. These variables are unique to a session and are not persistent across multiple sessions. They can be very useful for storing intermediate results or values that you want to reuse within a single session.
MySQL also supports the concept of User-defined variables, which allows passing of a value from one statement to another. A user-defined variable in MySQL is written as @var_name where, var_name is the name of the variable and can consist of alphanumeric characters, ., _, and $.
Key Features of a User-Defined Variable
- A User-Defined Variable is session specific i.e variable defined by one client is not shared to other client and when the session ends these variables are automatically expired.
- These variables are not case-sensitive. So, @mark or @Mark both refer to same value.
- Maximum length of variables can be 64 characters.
- Variable name can include other characters like- {!, #, ^, -, ..} in its name, if they are quoted. For ex- @'var@1' or @"var^2" or @`var3`.
- These variables can't be declared, they are only initialized i.e at time of declaration they should be assigned a value.
- An undeclared variable can also be accessed in a SQL statement but their values is set as NULL.
- These variables can take values from the following set of datatypes- { integer, floating-point, decimal, binary, nonbinary string or NULL value}.
Syntax
SET @var_name = expression
Examples
1. Assigning value to a variable using SET command.
mysql>SET @var1 = 2+6;
mysql>SET @var2 := @var1-2;
Values of these variables can be displayed by referring them in SELECT statement-
mysql>SELECT @var1, @var2;
Output:
+-------+-------+
| @var1 | @var2 |
+-------+-------+
| 8 | 6 |
+-------+-------+
2. Accessing a undeclared variable
mysql>SELECT @var3;
Output:
+-------+
| @var3 |
+-------+
| NULL |
+-------+
Here, variable @var3 is undeclared so its default value is NULL.
3. Assigning value to a variable without using SET.
mysql>SELECT @var3 := 4;
Output:
+----------+
| @var3:=4 |
+----------+
| 4 |
+----------+
In the above example- the variable @var3 should be assigned value using only := not =, the latter is treated as comparison in non-SET statement. Like-
mysql>SELECT @var4 = 5;
Output:
+----------+
| @var4=5 |
+----------+
| NULL |
+----------+
How these variables are used for storing values, that are used in future.
Consider, the following Student table-
s_id | s_name | mark |
---|
1 | shagun | 15 |
2 | taruna | 5 |
3 | riya | 15 |
4 | palak | 10 |
5 | neha | 7 |
6 | garima | 17 |
Now, we have to find rank of these students by using user-defined variables.
For this, we initialize two variables- @rank and @prev_mark.
mysql>SET @rank=0, @prev_mark=0;
Query:
mysql>Select s_name, if (@prev_mark != mark, @rank:=@rank+1, @rank) as 'rank',
@prev_mark:=mark as 'marks' from student order by mark desc;
Here, variable @rank is used to store the rank of student and @prev_mark is used to store the marks of previous student marks.
Comparison between marks is made so that in case if two students are having equal marks then increment in @rank variable can be avoided.
Changes in both variables take place after student table is sorted in descending order by “mark” column.
Output:
s_name | rank | marks |
---|
garima | 1 | 17 |
shagun | 2 | 15 |
riya | 2 | 15 |
palak | 3 | 10 |
neha | 4 | 7 |
taruna | 5 | 5 |
Thus, we get the resulted student table sorted by “marks” column in descending order along with rank of students.
Note: In the above query, take care of order of column in select statement. If “marks” column is written before “rank” column then we don’t get desired output. Because every time @prev_mark is assigned the mark of current student which results evaluation @prev_mark!=mark as false. So, the rank of every student is displayed as non-incremented i.e it will remain as 0.
Similar Reads
Solidity - Variables A Variable is basically a placeholder for the data which can be manipulated at runtime. Variables allow users to retrieve and change the stored information. Rules For Naming Variables 1. A variable name should not match with reserved keywords. 2. Variable names must start with a letter or an unders
4 min read
Variable in Programming In programming, we often need a named storage location to store the data or values. Using variables, we can store the data in our program and access it afterward. In this article, we will learn about variables in programming, their types, declarations, initialization, naming conventions, etc. Variab
11 min read
C# Variables In C#, variables are containers used to store data values during program execution. So basically, a Variable is a placeholder of the information which can be changed at runtime. And variables allows to Retrieve and Manipulate the stored information. In Brief Defination: When a user enters a new valu
4 min read
C++ Variables In C++, variable is a name given to a memory location. It is the basic unit of storage in a program. The value stored in a variable can be accessed or changed during program execution.Creating a VariableCreating a variable and giving it a name is called variable definition (sometimes called variable
4 min read
How to avoid Compile Error while defining Variables Variables: A variable is the name given to a memory location. It is the basic unit of storage in a program. The value stored in a variable can be changed during program execution. A variable is only a name given to a memory location, all the operations done on the variable effects that memory locati
3 min read
Variables in Objective-C A variable is a place for data storage that our programs can access or manipulate. In Objective-C, variables have a defined type that specifies their shape and layout. They also cover the gamut of values and operations that we are capable of performing. Before moving further, we understand the guide
4 min read
Variable Declaration in Programming Declaration of Variables is a fundamental concept in programming, where programmers define variables to store data within a program. In this article, we will provide a detailed overview about the declaration of variable, its importance how they are implemented in different programming languages. Tab
3 min read
Go Variables A typical program uses various values that may change during its execution. For Example, a program that performs some operations on the values entered by the user. The values entered by one user may differ from those entered by another user. Hence this makes it necessary to use variables as another
9 min read
User Defined Variables vs Local Variables in MySQL In MySQL, both user-defined and local variables are used to hold temporary data while running SQL queries. User-defined variables, marked with '@', are handy for transferring values between different queries within a session. Meanwhile, local variables are declared using DECLARE inside stored proced
4 min read