{"id":1481,"date":"2025-01-06T08:51:32","date_gmt":"2025-01-06T01:51:32","guid":{"rendered":"https:\/\/www.pgtutorial.com\/?page_id=1481"},"modified":"2025-01-06T09:28:08","modified_gmt":"2025-01-06T02:28:08","slug":"plpgsql-select-into","status":"publish","type":"page","link":"https:\/\/www.pgtutorial.com\/plpgsql\/plpgsql-select-into\/","title":{"rendered":"PL\/pgSQL SELECT INTO"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: In this tutorial, you&#8217;ll learn how to PL\/pgSQL <code>SELECT INTO<\/code> statement to select data from a table and assign it to a variable.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='overview-of-the-plpgsql-select-into-statement'>Overview of the PL\/pgSQL SELECT INTO Statement <a href=\"#overview-of-the-plpgsql-select-into-statement\" class=\"anchor\" id=\"overview-of-the-plpgsql-select-into-statement\" title=\"Anchor for Overview of the PL\/pgSQL SELECT INTO Statement\">#<\/a><\/h2>\n\n\n\n<p>The <code>SELECT INTO<\/code> statement retrieves data from a table and assigns it to a variable:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span> column1, column2, ...\n<span class=\"hljs-keyword\">INTO<\/span> variable1, variable2, ...\n<span class=\"hljs-keyword\">FROM<\/span> <span class=\"hljs-built_in\">table_name<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, provide one or more columns of the table_name to retrieve data in the <code>SELECT<\/code> clause.<\/li>\n\n\n\n<li>Second, specify a variable list in the <code>INTO<\/code> clause.<\/li>\n\n\n\n<li>Third, specify the table name from which you retrieve data in the <code>FROM<\/code> clause.<\/li>\n<\/ul>\n\n\n\n<p>The <code>SELECT<\/code> statement assigns the values retrieved from <code>column1<\/code>, <code>column2<\/code>, \u2026 to the corresponding <code>variable1<\/code>, <code>variable2<\/code>, \u2026 If the <code>SELECT<\/code> statement returns no rows, the variables will retain their initial values.<\/p>\n\n\n\n<p>Besides table columns, you can use expressions in the <code>SELECT<\/code> clause. The <code>SELECT<\/code> statement may include clauses such as <code><a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-inner-join\/\">JOIN<\/a><\/code>, <code><a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-group-by\/\">GROUP BY<\/a><\/code>, and <code><a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-having\/\">HAVING<\/a><\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='plpgsql-select-into-statement-examples'>PL\/pgSQL SELECT INTO Statement Examples <a href=\"#plpgsql-select-into-statement-examples\" class=\"anchor\" id=\"plpgsql-select-into-statement-examples\" title=\"Anchor for PL\/pgSQL SELECT INTO Statement Examples\">#<\/a><\/h2>\n\n\n\n<p>Let&#8217;s explore some examples of using the <code>SELECT INTO<\/code> statement. We&#8217;ll use the products and inventories tables:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"378\" height=\"254\" src=\"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/products-inventories-1.png\" alt=\"PL\/pgSQL SELECT INTO - Sample Tables\" class=\"wp-image-1050\" srcset=\"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/products-inventories-1.png 378w, https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/products-inventories-1-300x202.png 300w\" sizes=\"auto, (max-width: 378px) 100vw, 378px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id='assigning-a-value-to-a-variable'>Assigning a Value to a Variable <a href=\"#assigning-a-value-to-a-variable\" class=\"anchor\" id=\"assigning-a-value-to-a-variable\" title=\"Anchor for Assigning a Value to a Variable\">#<\/a><\/h3>\n\n\n\n<p>The following statement uses the <code>SELECT INTO<\/code> statement to retrieve a price from the <code>products<\/code> table and assign it to a variable:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">DO<\/span>\n$$<span class=\"pgsql\">\n<span class=\"hljs-keyword\">DECLARE<\/span>\n    v_price <span class=\"hljs-type\">DECIMAL<\/span>;\n<span class=\"hljs-keyword\">BEGIN<\/span>\n    <span class=\"hljs-keyword\">SELECT<\/span> price <span class=\"hljs-keyword\">INTO<\/span> v_price\n    <span class=\"hljs-keyword\">FROM<\/span> products\n   <span class=\"hljs-keyword\">WHERE<\/span> product_id = <span class=\"hljs-number\">1<\/span>;\n\n   <span class=\"hljs-keyword\">RAISE<\/span> <span class=\"hljs-keyword\">NOTICE<\/span> <span class=\"hljs-string\">'The price is %'<\/span>, v_price;\n<span class=\"hljs-keyword\">END<\/span>;\n$$<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">NOTICE<\/span>:  The price <span class=\"hljs-keyword\">is<\/span> <span class=\"hljs-number\">999.99<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='assigning-values-to-multiple-variables'>Assigning Values to Multiple Variables <a href=\"#assigning-values-to-multiple-variables\" class=\"anchor\" id=\"assigning-values-to-multiple-variables\" title=\"Anchor for Assigning Values to Multiple Variables\">#<\/a><\/h3>\n\n\n\n<p>The following statement uses the <code>SELECT INTO<\/code> statement to retrieve a price and safety stock from the <code>products<\/code> table and assign them to multiple variables:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">DO<\/span>\n$$<span class=\"pgsql\">\n<span class=\"hljs-keyword\">DECLARE<\/span>\n    v_price <span class=\"hljs-type\">DECIMAL<\/span>;\n    v_safety_stock <span class=\"hljs-type\">DECIMAL<\/span>;\n<span class=\"hljs-keyword\">BEGIN<\/span>\n    <span class=\"hljs-keyword\">SELECT<\/span> price, safety_stock <span class=\"hljs-keyword\">INTO<\/span> v_price, v_safety_stock\n    <span class=\"hljs-keyword\">FROM<\/span> products\n   <span class=\"hljs-keyword\">WHERE<\/span> product_id = <span class=\"hljs-number\">1<\/span>;\n\n   <span class=\"hljs-keyword\">RAISE<\/span> <span class=\"hljs-keyword\">NOTICE<\/span> <span class=\"hljs-string\">'Price: %, Safety Stock: %'<\/span>, v_price, v_safety_stock;\n<span class=\"hljs-keyword\">END<\/span>;\n$$<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">NOTICE<\/span>:  Price: <span class=\"hljs-number\">999.99<\/span>, Safety Stock: <span class=\"hljs-number\">20<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='using-the-plpgsql-select-into-statement-with-join-clause'>Using the PL\/pgSQL SELECT INTO Statement with JOIN Clause <a href=\"#using-the-plpgsql-select-into-statement-with-join-clause\" class=\"anchor\" id=\"using-the-plpgsql-select-into-statement-with-join-clause\" title=\"Anchor for Using the PL\/pgSQL SELECT INTO Statement with JOIN Clause\">#<\/a><\/h3>\n\n\n\n<p>The following example shows how to use the <code>SELECT INTO<\/code> statement with <code><a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-inner-join\/\">JOIN<\/a><\/code> to retrieve the inventory amount of the <code>Apple iPhone 15 Pro Max<\/code> product and assign it to a variable:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">DO<\/span>\n$$<span class=\"pgsql\">\n<span class=\"hljs-keyword\">DECLARE<\/span>\n    v_inventory_amount <span class=\"hljs-type\">DECIMAL<\/span>;\n<span class=\"hljs-keyword\">BEGIN<\/span>\n   <span class=\"hljs-keyword\">SELECT<\/span> quantity * price \n    <span class=\"hljs-keyword\">INTO<\/span>  v_inventory_amount \n   <span class=\"hljs-keyword\">FROM<\/span> inventories\n   <span class=\"hljs-keyword\">JOIN<\/span> products <span class=\"hljs-keyword\">USING<\/span> (product_id)\n   <span class=\"hljs-keyword\">WHERE<\/span> product_name = <span class=\"hljs-string\">'Apple iPhone 15 Pro Max'<\/span>;\n\n   <span class=\"hljs-keyword\">RAISE<\/span> <span class=\"hljs-keyword\">NOTICE<\/span> <span class=\"hljs-string\">'iPhone 15 Pro Max Inventory Amount: $%'<\/span>, v_inventory_amount;\n<span class=\"hljs-keyword\">END<\/span>;\n$$<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='type-copying'>Type Copying <a href=\"#type-copying\" class=\"anchor\" id=\"type-copying\" title=\"Anchor for Type Copying\">#<\/a><\/h2>\n\n\n\n<p>PL\/pgSQL allows declaring a variable with the type copying from a table column using the following syntax:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">variable_name <span class=\"hljs-built_in\">table_name<\/span>.<span class=\"hljs-built_in\">column_name<\/span><span class=\"hljs-meta\">%TYPE<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>For example, you can declare the <code>v_price<\/code> variable with the type copying from the <code>price<\/code> column of the <code>products<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">v_price products.price<span class=\"hljs-meta\">%TYPE<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Type copying ensures variable types align with the values retrieved from the columns that they reference.<\/p>\n\n\n\n<p>When the column&#8217;s data types change, the variables will automatically inherit the new data types without needing manual code changes.<\/p>\n\n\n\n<p>Additionally, you can declare a variable with the type copying from another variable with the fllowing syntax<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">variable1 variable2<span class=\"hljs-meta\">%TYPE<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following example shows how to declare variables by copying types from table columns:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">DO<\/span>\n$$<span class=\"pgsql\">\n<span class=\"hljs-keyword\">DECLARE<\/span>\n    v_price products.price<span class=\"hljs-meta\">%TYPE<\/span>;\n    v_safety_stock products.safety_stock<span class=\"hljs-meta\">%TYPE<\/span>;\n<span class=\"hljs-keyword\">BEGIN<\/span>\n    <span class=\"hljs-keyword\">SELECT<\/span> price, safety_stock <span class=\"hljs-keyword\">INTO<\/span> v_price, v_safety_stock\n    <span class=\"hljs-keyword\">FROM<\/span> products\n   <span class=\"hljs-keyword\">WHERE<\/span> product_id = <span class=\"hljs-number\">1<\/span>;\n\n   <span class=\"hljs-keyword\">RAISE<\/span> <span class=\"hljs-keyword\">NOTICE<\/span> <span class=\"hljs-string\">'Price: %, Safety Stock: %'<\/span>, v_price, v_safety_stock;\n<span class=\"hljs-keyword\">END<\/span>;\n$$<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output<strong>:<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">NOTICE<\/span>:  Price: <span class=\"hljs-number\">999.99<\/span>, Safety Stock: <span class=\"hljs-number\">20<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the <code>SELECT INTO<\/code> statement to retrieve data from a table and assign it to variables.<\/li>\n\n\n\n<li>Leverage type copying to write more robust and maintainable code.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=plpgsql-select-into\"\n  height=\"700\"\n  class=\"iframe\"\n><\/iframe>\n\n\n\n<h2 class=\"wp-block-heading\" id='exercises'>Exercises <a href=\"#exercises\" class=\"anchor\" id=\"exercises\" title=\"Anchor for Exercises\">#<\/a><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id='exercise-1-basic-select-into-statement'>Exercise 1: Basic SELECT INTO statement <a href=\"#exercise-1-basic-select-into-statement\" class=\"anchor\" id=\"exercise-1-basic-select-into-statement\" title=\"Anchor for Exercise 1: Basic SELECT INTO statement\">#<\/a><\/h3>\n\n\n\n<p>Write a PL\/pgSQL block to retrieve the price from the <code>products<\/code> table with id 100 and display it using the <code>RAISE NOTICE<\/code> statement.<\/p>\n\n\n\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary>Solution<\/summary><pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">DO<\/span>\n$$<span class=\"pgsql\">\n<span class=\"hljs-keyword\">DECLARE<\/span>\n    v_price <span class=\"hljs-type\">DECIMAL<\/span>;\n<span class=\"hljs-keyword\">BEGIN<\/span>\n    <span class=\"hljs-keyword\">SELECT<\/span> price <span class=\"hljs-keyword\">INTO<\/span> v_price\n    <span class=\"hljs-keyword\">FROM<\/span> products\n   <span class=\"hljs-keyword\">WHERE<\/span> product_id = <span class=\"hljs-number\">100<\/span>;\n\n   <span class=\"hljs-keyword\">RAISE<\/span> <span class=\"hljs-keyword\">NOTICE<\/span> <span class=\"hljs-string\">'The price is %'<\/span>, v_price;\n<span class=\"hljs-keyword\">END<\/span>;\n$$<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><\/details>\n\n\n\n<h3 class=\"wp-block-heading\" id='exercise-2-multiple-value-retrieval-and-type-copying'>Exercise 2: Multiple value retrieval and Type Copying <a href=\"#exercise-2-multiple-value-retrieval-and-type-copying\" class=\"anchor\" id=\"exercise-2-multiple-value-retrieval-and-type-copying\" title=\"Anchor for Exercise 2: Multiple value retrieval and Type Copying\">#<\/a><\/h3>\n\n\n\n<p>Write a PL\/pgSQL block to retrieve the min, max, and average product prices from the <code>products<\/code> table and display them using the <code>RAISE NOTICE<\/code> statement. Use type copying for the min and max variables.<\/p>\n\n\n\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary>Solution<\/summary><pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">DO<\/span>\n$$<span class=\"pgsql\">\n<span class=\"hljs-keyword\">DECLARE<\/span>\n    v_min products.price<span class=\"hljs-meta\">%TYPE<\/span>;\n    v_max products.price<span class=\"hljs-meta\">%TYPE<\/span>;\n    v_avg <span class=\"hljs-type\">DEC<\/span>;\n<span class=\"hljs-keyword\">BEGIN<\/span>\n    <span class=\"hljs-keyword\">SELECT<\/span> MIN(price), MAX(price), ROUND(AVG(price),<span class=\"hljs-number\">2<\/span>)\n    <span class=\"hljs-keyword\">INTO<\/span> v_min, v_max, v_avg\n    <span class=\"hljs-keyword\">FROM<\/span> products;\n\n   <span class=\"hljs-keyword\">RAISE<\/span> <span class=\"hljs-keyword\">NOTICE<\/span> <span class=\"hljs-string\">'Min: $%, Max: $%, Average: $%'<\/span>, v_min, v_max, v_avg;\n<span class=\"hljs-keyword\">END<\/span>;\n$$<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><\/details>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"1481\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/plpgsql\/plpgsql-select-into\/\"\n\t\t\t\tdata-post-title=\"PL\/pgSQL SELECT INTO\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"1481\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/plpgsql\/plpgsql-select-into\/\"\n\t\t\t\tdata-post-title=\"PL\/pgSQL SELECT INTO\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: In this tutorial, you&#8217;ll learn how to PL\/pgSQL SELECT INTO statement to select data from a table and assign it to a variable. Overview of the PL\/pgSQL SELECT INTO Statement # The SELECT INTO statement retrieves data from a table and assigns it to a variable: In this syntax: The SELECT statement assigns the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1420,"menu_order":3,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1481","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PL\/pgSQL SELECT INTO<\/title>\n<meta name=\"description\" content=\"In this tutorial, you&#039;ll learn how to PL\/pgSQL SELECT INTO statement to select data from a table and assign it to a variable.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pgtutorial.com\/plpgsql\/plpgsql-select-into\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PL\/pgSQL SELECT INTO\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you&#039;ll learn how to PL\/pgSQL SELECT INTO statement to select data from a table and assign it to a variable.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pgtutorial.com\/plpgsql\/plpgsql-select-into\/\" \/>\n<meta property=\"og:site_name\" content=\"PostgreSQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2025-01-06T02:28:08+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/products-inventories-1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"378\" \/>\n\t<meta property=\"og:image:height\" content=\"254\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/plpgsql\\\/plpgsql-select-into\\\/\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/plpgsql\\\/plpgsql-select-into\\\/\",\"name\":\"PL\\\/pgSQL SELECT INTO\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/plpgsql\\\/plpgsql-select-into\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/plpgsql\\\/plpgsql-select-into\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pgtutorial.com\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/products-inventories-1.png\",\"datePublished\":\"2025-01-06T01:51:32+00:00\",\"dateModified\":\"2025-01-06T02:28:08+00:00\",\"description\":\"In this tutorial, you'll learn how to PL\\\/pgSQL SELECT INTO statement to select data from a table and assign it to a variable.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/plpgsql\\\/plpgsql-select-into\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pgtutorial.com\\\/plpgsql\\\/plpgsql-select-into\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/plpgsql\\\/plpgsql-select-into\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/products-inventories-1.png\",\"contentUrl\":\"https:\\\/\\\/www.pgtutorial.com\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/products-inventories-1.png\",\"width\":378,\"height\":254,\"caption\":\"Using PostgreSQL AVG Window Function with JOIN\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/plpgsql\\\/plpgsql-select-into\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PL\\\/pgSQL\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/plpgsql\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"PL\\\/pgSQL SELECT INTO\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/\",\"name\":\"PostgreSQL Tutorial\",\"description\":\"Learn PostgreSQL from Scratch\",\"alternateName\":\"PostgreSQL\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.pgtutorial.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PL\/pgSQL SELECT INTO","description":"In this tutorial, you'll learn how to PL\/pgSQL SELECT INTO statement to select data from a table and assign it to a variable.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pgtutorial.com\/plpgsql\/plpgsql-select-into\/","og_locale":"en_US","og_type":"article","og_title":"PL\/pgSQL SELECT INTO","og_description":"In this tutorial, you'll learn how to PL\/pgSQL SELECT INTO statement to select data from a table and assign it to a variable.","og_url":"https:\/\/www.pgtutorial.com\/plpgsql\/plpgsql-select-into\/","og_site_name":"PostgreSQL Tutorial","article_modified_time":"2025-01-06T02:28:08+00:00","og_image":[{"width":378,"height":254,"url":"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/products-inventories-1.png","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.pgtutorial.com\/plpgsql\/plpgsql-select-into\/","url":"https:\/\/www.pgtutorial.com\/plpgsql\/plpgsql-select-into\/","name":"PL\/pgSQL SELECT INTO","isPartOf":{"@id":"https:\/\/www.pgtutorial.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pgtutorial.com\/plpgsql\/plpgsql-select-into\/#primaryimage"},"image":{"@id":"https:\/\/www.pgtutorial.com\/plpgsql\/plpgsql-select-into\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/products-inventories-1.png","datePublished":"2025-01-06T01:51:32+00:00","dateModified":"2025-01-06T02:28:08+00:00","description":"In this tutorial, you'll learn how to PL\/pgSQL SELECT INTO statement to select data from a table and assign it to a variable.","breadcrumb":{"@id":"https:\/\/www.pgtutorial.com\/plpgsql\/plpgsql-select-into\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pgtutorial.com\/plpgsql\/plpgsql-select-into\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pgtutorial.com\/plpgsql\/plpgsql-select-into\/#primaryimage","url":"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/products-inventories-1.png","contentUrl":"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/products-inventories-1.png","width":378,"height":254,"caption":"Using PostgreSQL AVG Window Function with JOIN"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pgtutorial.com\/plpgsql\/plpgsql-select-into\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pgtutorial.com\/"},{"@type":"ListItem","position":2,"name":"PL\/pgSQL","item":"https:\/\/www.pgtutorial.com\/plpgsql\/"},{"@type":"ListItem","position":3,"name":"PL\/pgSQL SELECT INTO"}]},{"@type":"WebSite","@id":"https:\/\/www.pgtutorial.com\/#website","url":"https:\/\/www.pgtutorial.com\/","name":"PostgreSQL Tutorial","description":"Learn PostgreSQL from Scratch","alternateName":"PostgreSQL","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pgtutorial.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/1481","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/comments?post=1481"}],"version-history":[{"count":9,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/1481\/revisions"}],"predecessor-version":[{"id":1492,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/1481\/revisions\/1492"}],"up":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/1420"}],"wp:attachment":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/media?parent=1481"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}