Set user variable from result of query
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
In MySQL, you can set a user-defined variable from a query result using SELECT ... INTO @variable or SET @variable = (SELECT ...). User variables are session-scoped (prefixed with @), persist until the session ends, and can be used in subsequent queries. This is useful for storing intermediate results, building dynamic queries, and avoiding repeated subqueries. PostgreSQL and SQL Server have different syntax for the same concept.
MySQL: SELECT ... INTO @variable
MySQL: SET @variable = (SELECT ...)
The subquery must return exactly one row and one column. If it returns multiple rows, MySQL returns an error.
MySQL: SELECT @variable := expression
The := operator assigns within a SELECT. This is deprecated in MySQL 8.0+ in favor of window functions.
Using Variables in Subsequent Queries
Calculating Running Totals (Pre-Window Functions)
In MySQL 8.0+, use window functions instead:
PostgreSQL: Variables in PL/pgSQL
PostgreSQL does not have session-level user variables like MySQL. Use DO blocks or functions:
SQL Server: Variables with DECLARE and SET
Practical Example: Pagination
Practical Example: Conditional Logic
Common Pitfalls
- Subquery returns multiple rows:
SET @var = (SELECT col FROM table)fails if the subquery returns more than one row. AddLIMIT 1or use an aggregate function (MAX,MIN,COUNT) to ensure a single result. - Variable type is determined by the assigned value: MySQL user variables have no declared type. Assigning a string then comparing to a number may produce unexpected results due to implicit type conversion. Be consistent with the types you assign.
SELECT @var := exprevaluation order: MySQL does not guarantee the order of evaluation of expressions in aSELECTclause. Using@var := exprin one column and reading@varin another column of the sameSELECTmay produce unpredictable results. Use window functions in MySQL 8.0+ instead.- Variables do not persist across sessions: User variables (
@var) exist only for the current session/connection. A new connection starts with no variables set. For persistent storage, use tables. - PostgreSQL has no user variables: PostgreSQL does not support
@variablesyntax. UseDOblocks withDECLARE, CTEs, or temporary tables to achieve similar functionality.
Summary
- MySQL:
SELECT ... INTO @var,SET @var = (SELECT ...), or@var := exprin SELECT - User variables are session-scoped, prefixed with
@, and do not require declaration in MySQL - SQL Server requires
DECLARE @var TYPEbefore use - PostgreSQL uses
DOblocks withDECLAREor CTEs instead of user variables - Avoid
SELECT @var := exprin MySQL 8.0+ — use window functions for row-level calculations - Subqueries used to set variables must return exactly one row and one column
Related reading
- Set value for particular cell in pandas DataFrame using index
- Set value to NULL in MySQL
- Setting default values for columns in JPA
- Setting Django up to use MySQL
- Setting Django up to use MySQL
- Setting global sql_mode in MySQL
- setting multiple column using one update
- Setting the MySQL root user password on OS X

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.