> This page location: PostgreSQL PL/pgSQL > SELECT INTO
> Full Neon documentation index: https://neon.com/docs/llms.txt

# PL/pgSQL Select Into

**Info:** The PL/pgSQL `select into` statement works the same on any PostgreSQL deployment, so the patterns shown here apply wherever you run Postgres. If you're an enterprise looking for managed Postgres built for the AI era, [Lakebase](https://www.databricks.com/product/lakebase) delivers high performance, strong security, and tight integration with the Lakehouse. If you're a developer or startup who needs to ship and scale quickly, [Neon](https://neon.com) is the Postgres platform built for you.

**Summary**: in this tutorial, you will learn how to use the PL/pgSQL `select into` statement to select data from the database and assign it to a variable.

## Introduction to PL/pgSQL Select Into statement

The `select into` statement allows you to [select data from the database](../postgresql-tutorial/postgresql-select) and assign it to a [variable](https://neon.com/postgresql/plpgsql/plpgsql-variables).

Here's the basic syntax of the `select into` statement:

```sql
select column1, column2, ...
into variable1, variable2, ...
from table_expression;
```

In this syntax,

- First, specify one or more columns from which you want to retrieve data in the `select` clause.
- Second, place one or more variables after the `into` keyword.
- Third, provide the name of the table in the `from` clause.

The `select` `into` statement will assign the data returned by the `select` clause to the corresponding variables.

Besides selecting data from a table, you can use other clauses of the `select` statement such as [`join`](../postgresql-tutorial/postgresql-joins), [`group by,`](../postgresql-tutorial/postgresql-group-by) and [`having`](../postgresql-tutorial/postgresql-having).

## PL/pgSQL Select Into statement examples

Let's take some examples of using the `select into` statement.

### 1) Basic select into statement example

The following example uses the `select into` statement to retrieve the number of actors from the `actor` table and assign it to the `actor_count` variable:

```plsql
do
$$
declare
   actor_count integer;
begin
   -- select the number of actors from the actor table
   select count(*)
   into actor_count
   from actor;

   -- show the number of actors
   raise notice 'The number of actors: %', actor_count;
end;
$$;
```

Output:

```
NOTICE:  The number of actors: 200
```

In this example:

- First, declare a variable called `actor_count` that stores the number of actors from the `actor` table.
- Second, assign the number of actors to the `actor_count` using the `select into` statement.
- Third, display a message that shows the value of the `actor_count` variable using the `raise notice` statement.

### 2) Using the select into with multiple variables

The following example uses the `select into` statement to assign the first and last names of the actor id 1 to two variables:

```plsql
do
$$
declare
   v_first_name varchar;
   v_last_name varchar;
begin
   -- select the first_name and last_name of the actor id 1
   select first_name, last_name
   into v_first_name, v_last_name
   from actor
   where actor_id = 1;

   -- show the full name
   raise notice '% %', v_first_name, v_last_name;
end;
$$;
```

Output:

```
NOTICE:  Penelope Guiness
```

How it works.

First, declare two variables `v_first_name` and `v_last_name` with the types `varchar`:

```sql
v_first_name varchar;
v_last_name varchar;
```

Second, retrieve the `first_name` and `last_name` of the actor id 1 from the `actor` table and assign them to the `v_first_name` and `v_last_name` variables:

```sql
select first_name, last_name
into v_first_name, v_last_name
from actor
where actor_id = 1;
```

Third, show the values of `v_first_name` and `v_last_name` variables:

```sql
raise notice '% %', v_first_name, v_last_name;
```

Because we assign data retrieved from the `first_name` and `last_name` columns of the `actor` table, we can use the type-copying technique to declare the `v_first_name` and `v_last_name` variables:

```plsql
do
$$
declare
   v_first_name actor.first_name%type;
   v_last_name actor.last_name%type;
begin
   -- select the first_name and last_name of the actor id 1
   select first_name, last_name
   into v_first_name, v_last_name
   from actor
   where actor_id = 1;

   -- show the full name
   raise notice '% %', v_first_name, v_last_name;
end;
$$;
```

## Summary

- Use the `select into` statement to select data from the database and assign it to a variable.

---

## Related docs (PostgreSQL PL/pgSQL)

- [Introduction](https://neon.com/postgresql/plpgsql/introduction-to-postgresql-stored-procedures)
- [Block Structure](https://neon.com/postgresql/plpgsql/plpgsql-block-structure)
- [Variables](https://neon.com/postgresql/plpgsql/plpgsql-variables)
- [Constants](https://neon.com/postgresql/plpgsql/plpgsql-constants)
- [IF-ELSE Statements](https://neon.com/postgresql/plpgsql/plpgsql-if-else-statements)
- [CASE Statement](https://neon.com/postgresql/plpgsql/plpgsql-case-statement)
- [Loop Statements](https://neon.com/postgresql/plpgsql/plpgsql-loop-statements)
- [FOR Loop](https://neon.com/postgresql/plpgsql/plpgsql-for-loop)
- [WHILE Loop](https://neon.com/postgresql/plpgsql/pl-pgsql-while-loop)
- [EXIT Statement](https://neon.com/postgresql/plpgsql/plpgsql-exit)
- [CONTINUE Statement](https://neon.com/postgresql/plpgsql/pl-pgsql-continue)
- [Cursor](https://neon.com/postgresql/plpgsql/plpgsql-cursor)
- [Function Parameters](https://neon.com/postgresql/plpgsql/plpgsql-function-parameters)
- [Function Overloading](https://neon.com/postgresql/plpgsql/plpgsql-function-overloading)
- [Function Returns Table](https://neon.com/postgresql/plpgsql/plpgsql-function-returns-a-table)
- [Returns SETOF](https://neon.com/postgresql/plpgsql/plpgsql-returns-setof)
- [Record Types](https://neon.com/postgresql/plpgsql/plpgsql-record-types)
- [Row Types](https://neon.com/postgresql/plpgsql/pl-pgsql-row-types)
- [Error Messages](https://neon.com/postgresql/plpgsql/plpgsql-errors-messages)
- [Exception Handling](https://neon.com/postgresql/plpgsql/exception)
- [CREATE FUNCTION](https://neon.com/postgresql/plpgsql/create-function)
- [CREATE PROCEDURE](https://neon.com/postgresql/plpgsql/create-procedure)
- [DROP FUNCTION](https://neon.com/postgresql/plpgsql/drop-function)
- [DROP PROCEDURE](https://neon.com/postgresql/plpgsql/drop-procedure)
- [Stored Procedure with INOUT Parameters](https://neon.com/postgresql/plpgsql/stored-procedure-with-inout-parameters)
- [Introduction to Stored Procedures](https://neon.com/postgresql/plpgsql/introduction-to-postgresql-stored-procedures)
- [ASSERT Statement](https://neon.com/postgresql/plpgsql/pl-pgsql-assert)
- [Dollar-Quoted String Constants](https://neon.com/postgresql/plpgsql/dollar-quoted-string-constants)
