Open In App

How to Declare a Variable in SQL?

Last Updated : 26 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Variables in SQL is a fundamental step towards building efficient and powerful database applications. It enhances the flexibility and efficiency of our database queries. Understanding how to declare variables in SQL is very important to write scalable code. Variables act as placeholders for data which enable us to manipulate and store information within our SQL programs.

In this article, we’ll explore the various methods and best practices for declaring variables in SQL along with the syntax and examples, which help us to write more dynamic and effective queries.

How to Declare a Variable

When working with SQL we may encounter various situations where we need to store and manipulate temporary data within our queries effectively. Variables act as containers for values and enable various operations on the stored data. SQL provides several methods to declare and use variables including the use of the WITH clause, SET command, enhances, and various methods. Below are the methods that are used to declare variables in SQL effectively. The methods are as follows:

  1. Using SET Command
  2. Using WITH Clause
  3. Using Scalar Subqueries
    ,
  4. Using Temporary Tables

1. Using SET Command

Although SQL does not have a built-in SET command like some other database systems, we can use variable assignment using user-defined functions.

Query:

SELECT  "GFG" AS NAME

Output:

num2

Using SET Command

Explanation: The query selects the string “GFG” and renames it as “NAME“. Consequently, the output consists of a single column labeled “NAME” containing the string “GFG” in each row.

2. Using With Clause

The WITH clause is a powerful feature in SQL that allows defining temporary result sets within a query. Variables can be declared and assigned values within the WITH clause.

Query:

WITH Customers AS (
SELECT "GFG" AS name
)
SELECT * FROM Customers;

Output:

num2

Using WITH Clause

Explanation: The output shows a virtual table named “Customers” created using a Common Table Expression (CTE). It consists of a single column labeled “name”, containing the value “GFG“. The SELECT statement retrieves all rows from this “Customers” table, displaying the single row with “GFG” as the name.

3. Using Temporary Variables

Temporary tables can be used to store and manipulate data within a session. We can take advantage of temporary tables to mimic variable declaration.

Query:

CREATE TEMP TABLE Temp_Table (num INTEGER,name STRING);
INSERT INTO Temp_Table VALUES (23,"GFG");
SELECT num,name FROM Temp_Table;

Output:

Screenshot-2024-03-20-162057

Using Temporary Variable

Explanation: The output displays a temporary table named “Temp_Table” created with two columns: “num” of type INTEGER and “name” of type STRING. One row is inserted with values (23, “GFG”). The subsequent SELECT statement retrieves and displays the values from the “num” and “name” columns of “Temp_Table“.

4. Using Subqueries

Subqueries in SQL provide a versatile tool for embedding queries within queries. This article delves into their application in assigning values to variables within SQLite databases, offering insights and examples for effective database manipulation.

Query:

SELECT (SELECT 23) AS num;

Output:

n

Using Subqueries

Explanation: The output simply returns the value 23 as “num” using a subquery within the SELECT statement. It assigns the result of the inner query directly to the column “num” in the outer query’s result set.

Conclusion

In this article, we went through multiple methods to declare variables in SQL, ranging from using with clause to user-defined functions and temporary tables. Each method has its advantages and use cases, depending on the complexity and requirements of our database operations. Understanding these techniques will help you with the flexibility to efficiently manage variables and execute queries in SQL databases. These methods help in flexibility of code.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads