EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials SQL Tutorial SQL Variables
 

SQL Variables

Priya Pedamkar
Article byPriya Pedamkar

Updated March 13, 2023

SQL Variables

 

 

Introduction to SQL Variables

Variables in standard query language (SQL) are named locations in the memory which are used by the SQL query or program to manipulate the data. These named memory locations act as placeholders for the value of the variable. The size of the memory location depends upon the data type of variable. We can have multiple types of variables in SQL based on the data type of the data that it can hold such as varchar, text, int, numeric, date, timestamp, etc.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

How to declare and initialize variables in SQL?

Initialization and declaration of variables in SQL differ slightly based on the SQL database management server you are using. For example, a variable declaration starts with @ for user-defined variables and @@ for system-defined variables in SQL Server and MS SQL whereas in PL/PgSQL the declaration does not involve such symbols. For more information and syntax, you can refer to the documentation of respective database management servers. In this exercise, we will be working on postgreSQL or pgSQL.

Here are some examples of variable declarations.

DECLARE
counter    INTEGER ;
student_name VARCHAR(50) ;
email  VARCHAR(225)';

So, a variable declaration starts with the keyword “DECLARE”, followed by variable_name and its data type. If you are familiar with variables in C, this should be a piece of cake for you.

Next, initialization is specifying an initial value for the variable. We can do it while declaring the variable as well as later on while using the variable.

When we initialize a variable during the declaration, it looks like something as shown below.

DECLARE
counter    INTEGER := 101 ;
student_name VARCHAR(50) := 'Radhika Sharma' ;
email  VARCHAR(225)' := '[email protected]';

The entire code snippet for variable declaration and initialization in pgSQL looks something as shown below.

DO $$
DECLARE
counter    INTEGER := 101 ;
student_name VARCHAR(255) := 'Radhika Sharma';
email  VARCHAR(225) := '[email protected]';
BEGIN
RAISE NOTICE 'The student name for counter 101 is : %', student_name;
END $$;

SQL Variables output 1

In the above example, we made an inline function, where we have declared three variables namely,

  • counter with data type INTEGER with ‘101’ as the initial value
  • student_name with data type varchar or varying character with ‘Radhika Sharma’ as its initialization
  • the email with data type varchar or varying character with ‘[email protected]’ as an initial value

Examples of SQL Variables

Let’s discuss more examples to understand variable declaration, initialization, and working across some frequently used data types.

Example #1

Working with variables of integer data type

In this example, we have declared three variables x, y, and z, and initialized the former two. We have tried to assign z the result of x and y ‘s subtraction. The code snippet for the same looks as shown below.

Code:

DO $$
DECLARE
x integer := 30;
y integer := 20;
z integer;
BEGIN
z := x - y;
RAISE NOTICE 'Value of z: %', z;
END $$;

Output:

SQL Variables output 2

Example #2

Working with variables of DATE and TIME data types

Code:

DO $$
DECLARE
created_date DATE := NOW();
BEGIN
RAISE NOTICE 'This program was created on %', created_date;
END $$;

Output:

SQL Variables output 3

In this particular example, we have declared a variable of DATE data type and initialized it with current time using the built-in NOW() function. Let’s see some variations of the same.

Code:

DO $$
DECLARE
created_at TIME := NOW();
process_date DATE := '2020-04-30';
BEGIN
RAISE NOTICE 'This program was created at % and will be processed on %',created_at, process_date;
END $$;

Output:

output 4

In the above illustration, we have created the ‘created_at’ variable of the TIME data type. It is different from the date in the manner that it gives a timestamp without the date. Next, we have a ‘process_date’ variable which has an initialized date.

Example #3

Working with variables of NUMERIC data types

Most of the SQL database management servers provide for a variety of numeric data types. The most commonly used numeric data types are SMALLINT, INTEGER, BIGINT, REAL, DOUBLE PRECISION, and NUMERIC(p,s). The first three of the mentioned data types can only hold integer values. We have already discussed integers in example 1. In this section, we will discuss REAL, PRECISION, and NUMERIC(p,s) data types.

Code:

DO $$
DECLARE
x float := 40;
y real := 3;
z numeric(11,2);
BEGIN
z := x / y;
RAISE NOTICE 'Value of z: %', z;
END $$;

Output:

output 5

In the above example, we declared three types of variables. Everything is self-explanatory at this point except numeric(11,2) which stands for 11 digits before the precision or decimal point and 2 digits after it

Example #4

Copying data type of a column to a variable

Some SQL databases allow us to copy the data type of a column in the database table to a variable.

The syntax for the same in postgreSQL is as follows :

variable_name table_name.column_name%TYPE;

For illustration purposes consider the “product_details” table with columns like product_id, product_name, variations, price, etc.

output 6

Here, we want to copy the data type of column_name product_name to a variable called “new_product”.

Code:

DO $$
DECLARE
new_product product_details.product_name%type := 'Books';
BEGIN
RAISE NOTICE 'The recently added product in the store is %', new_product;
END $$;

Output:

output 7

Conclusion

Variables in any programming language acts are specific memory locations that act as placeholders for its actual value. This is true for SQL variables also. The size of the memory location depends upon the data type of the value that the particular variable can hold.

Recommended Articles

We hope that this EDUCBA information on “SQL Variables” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. SQL Administration
  2. ANY in SQL
  3. Ternary Operator in SQL
  4. PostgreSQL if else

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - ENROLL NOW