<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>UPDATE &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/update/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.dbrnd.com</link>
	<description>BIGData &#124; NoSQL &#124; MSSQL &#124; MySQL &#124; PostgreSQL</description>
	<lastBuildDate>Mon, 05 Mar 2018 20:08:49 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.6</generator>
	<item>
		<title>SQL Puzzle: SQL Advance Query – UPDATE Table from SELECT JOIN Statement</title>
		<link>https://www.dbrnd.com/2018/03/sql-puzzle-sql-advance-query-update-table-from-select-join-statement/</link>
					<comments>https://www.dbrnd.com/2018/03/sql-puzzle-sql-advance-query-update-table-from-select-join-statement/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 12 Mar 2018 16:16:16 +0000</pubDate>
				<category><![CDATA[SQL Puzzle]]></category>
		<category><![CDATA[Advance SQL Query]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[SQL Advance Query]]></category>
		<category><![CDATA[SQL Interview]]></category>
		<category><![CDATA[SQL Problem]]></category>
		<category><![CDATA[SQL Programming]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[UPDATE]]></category>
		<category><![CDATA[UPDATE from SELECT]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=7207</guid>

					<description><![CDATA[Check the below input data and expected output for updating the table using SELECT JOIN statement. Create sample tables with data: [crayon-6ab390ec689ee095783867/] UPDATE Statement: [crayon-6ab390ec689f1343115748/] Result: [crayon-6ab390ec689f2865506956/] Please try the different solution for this puzzle and share it via comment...]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F03%2Fsql-puzzle-sql-advance-query-update-table-from-select-join-statement%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20UPDATE%20Table%20from%20SELECT%20JOIN%20Statement" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F03%2Fsql-puzzle-sql-advance-query-update-table-from-select-join-statement%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20UPDATE%20Table%20from%20SELECT%20JOIN%20Statement" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F03%2Fsql-puzzle-sql-advance-query-update-table-from-select-join-statement%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20UPDATE%20Table%20from%20SELECT%20JOIN%20Statement" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F03%2Fsql-puzzle-sql-advance-query-update-table-from-select-join-statement%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20UPDATE%20Table%20from%20SELECT%20JOIN%20Statement" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F03%2Fsql-puzzle-sql-advance-query-update-table-from-select-join-statement%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20UPDATE%20Table%20from%20SELECT%20JOIN%20Statement" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F03%2Fsql-puzzle-sql-advance-query-update-table-from-select-join-statement%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20UPDATE%20Table%20from%20SELECT%20JOIN%20Statement" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F03%2Fsql-puzzle-sql-advance-query-update-table-from-select-join-statement%2F&#038;title=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20UPDATE%20Table%20from%20SELECT%20JOIN%20Statement" data-a2a-url="https://www.dbrnd.com/2018/03/sql-puzzle-sql-advance-query-update-table-from-select-join-statement/" data-a2a-title="SQL Puzzle: SQL Advance Query – UPDATE Table from SELECT JOIN Statement"></a></p><p>Check the below input data and expected output for updating the table using SELECT JOIN statement. </p>
<p><strong>Create sample tables with data:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE tbl_abc (ID INT, Name VARCHAR(10))
CREATE TABLE tbl_xyz (ID INT, Name VARCHAR(10))
GO

INSERT INTO tbl_abc VALUES 
(1,'a'),(2,'b'),(3,'c')
INSERT INTO tbl_xyz VALUES
(1,'x'),(2,'y'),(3,'z')
GO</pre> </p>
<p><strong>UPDATE Statement: </strong></p>
<pre class="crayon-plain-tag">UPDATE tbl_abc
	SET tbl_abc.Name = tbl_xyz.Name
FROM tbl_abc 
INNER JOIN tbl_xyz 
	ON tbl_abc.ID = tbl_xyz.ID
GO</pre> </p>
<p><strong>Result:</strong></p>
<pre class="crayon-plain-tag">SELECT *FROM tbl_abc
GO

ID          Name
----------- ----------
1           x
2           y
3           z</pre> </p>
<p><strong><code>Please try the different solution for this puzzle and share it via comment...</code></strong></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2018/03/sql-puzzle-sql-advance-query-update-table-from-select-join-statement/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>PostgreSQL: Increase the speed of UPDATE query using HOT UPDATE (Heap only tuple)</title>
		<link>https://www.dbrnd.com/2016/12/postgresql-increase-the-speed-of-update-query-using-hot-update-heap-only-tuple-mvcc-fill-factor-vacuum-fragmentation/</link>
					<comments>https://www.dbrnd.com/2016/12/postgresql-increase-the-speed-of-update-query-using-hot-update-heap-only-tuple-mvcc-fill-factor-vacuum-fragmentation/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Sun, 25 Dec 2016 13:28:47 +0000</pubDate>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[Fill Factor]]></category>
		<category><![CDATA[fragmentation]]></category>
		<category><![CDATA[HOT]]></category>
		<category><![CDATA[Hot only tuple]]></category>
		<category><![CDATA[MVCC]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[plpgsql]]></category>
		<category><![CDATA[Postgres Query]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[PostgreSQL Administrator]]></category>
		<category><![CDATA[PostgreSQL Error]]></category>
		<category><![CDATA[PostgreSQL Programming]]></category>
		<category><![CDATA[PostgreSQL Tips and Tricks]]></category>
		<category><![CDATA[UPDATE]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=3708</guid>

					<description><![CDATA[What is HOT UPDATE (Heap Only Tuple) and How It helps to increase the performance of UPDATE Query? In this post, I am going to explain the answer of this question. This is one of the internal of PostgreSQL, but I am sure that most of you guys don&#8217;t know about PostgreSQL HOT. PostgreSQL requires [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F12%2Fpostgresql-increase-the-speed-of-update-query-using-hot-update-heap-only-tuple-mvcc-fill-factor-vacuum-fragmentation%2F&amp;linkname=PostgreSQL%3A%20Increase%20the%20speed%20of%20UPDATE%20query%20using%20HOT%20UPDATE%20%28Heap%20only%20tuple%29" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F12%2Fpostgresql-increase-the-speed-of-update-query-using-hot-update-heap-only-tuple-mvcc-fill-factor-vacuum-fragmentation%2F&amp;linkname=PostgreSQL%3A%20Increase%20the%20speed%20of%20UPDATE%20query%20using%20HOT%20UPDATE%20%28Heap%20only%20tuple%29" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F12%2Fpostgresql-increase-the-speed-of-update-query-using-hot-update-heap-only-tuple-mvcc-fill-factor-vacuum-fragmentation%2F&amp;linkname=PostgreSQL%3A%20Increase%20the%20speed%20of%20UPDATE%20query%20using%20HOT%20UPDATE%20%28Heap%20only%20tuple%29" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F12%2Fpostgresql-increase-the-speed-of-update-query-using-hot-update-heap-only-tuple-mvcc-fill-factor-vacuum-fragmentation%2F&amp;linkname=PostgreSQL%3A%20Increase%20the%20speed%20of%20UPDATE%20query%20using%20HOT%20UPDATE%20%28Heap%20only%20tuple%29" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F12%2Fpostgresql-increase-the-speed-of-update-query-using-hot-update-heap-only-tuple-mvcc-fill-factor-vacuum-fragmentation%2F&amp;linkname=PostgreSQL%3A%20Increase%20the%20speed%20of%20UPDATE%20query%20using%20HOT%20UPDATE%20%28Heap%20only%20tuple%29" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F12%2Fpostgresql-increase-the-speed-of-update-query-using-hot-update-heap-only-tuple-mvcc-fill-factor-vacuum-fragmentation%2F&amp;linkname=PostgreSQL%3A%20Increase%20the%20speed%20of%20UPDATE%20query%20using%20HOT%20UPDATE%20%28Heap%20only%20tuple%29" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F12%2Fpostgresql-increase-the-speed-of-update-query-using-hot-update-heap-only-tuple-mvcc-fill-factor-vacuum-fragmentation%2F&#038;title=PostgreSQL%3A%20Increase%20the%20speed%20of%20UPDATE%20query%20using%20HOT%20UPDATE%20%28Heap%20only%20tuple%29" data-a2a-url="https://www.dbrnd.com/2016/12/postgresql-increase-the-speed-of-update-query-using-hot-update-heap-only-tuple-mvcc-fill-factor-vacuum-fragmentation/" data-a2a-title="PostgreSQL: Increase the speed of UPDATE query using HOT UPDATE (Heap only tuple)"></a></p><p><strong>What is HOT UPDATE (Heap Only Tuple) and How It helps to increase the performance of UPDATE Query?</strong><br />
In this post, I am going to explain the answer of this question. </p>
<blockquote><p>This is one of the internal of PostgreSQL, but I am sure that most of you guys don&#8217;t know about PostgreSQL HOT. </p></blockquote>
<p><strong>PostgreSQL requires HOT because of MVCC architecture.</strong><br />
Please read below articles, before start to knowing about the HOT. </p>
<blockquote><p><a href="https://www.dbrnd.com/2015/05/what-is-mvcc-multi-version-concurrency-control/" target="_blank"><strong>What is MVCC?</strong></a></p>
<p><a href="https://www.dbrnd.com/2016/03/postgresql-the-awesome-table-fillfactor-to-speedup-update-and-select-statement/" target="_blank"><strong>What is Fillfactor in PostgreSQL?</strong></a></p>
<p><a href="https://www.dbrnd.com/2015/11/postgresql-short-note-on-vacuum-vacuum-full-and-analyze/" target="_blank"><strong>What is Vacuum in PostgreSQL?</strong></a>
</p></blockquote>
<blockquote><p>In the MVCC architecture, When we update any record, internally MVCC architecture inserts a new record with new values and marks old record as deleted or discarded.<br />
When we vacuum, we can clean that deleted records and utilize that space.
</p></blockquote>
<p><strong>What is HOT (Hot Only Tuple)?</strong></p>
<blockquote><p>Now imagine that, we have total 500 data pages and we are updating record of page number 3, but unfortunately new record insert in page number 500. </p></blockquote>
<blockquote><p>It looks like very big internal fragmentation because of MVCC architecture.<br />
But actually this is not happening.</p></blockquote>
<blockquote><p>PostgreSQL has a concept of HOT, With a HOT dead tuple space can be automatically reclaimed at the time of INSERT or UPDATE if no changes are made to indexed columns. This allows for more consistent performance.</p></blockquote>
<blockquote><p>HOT means, creating a new update tuple if possible on the same page as the old tuple, and maintaining a chain of updated tuples linking a new version to the old tuple.</p></blockquote>
<blockquote><p>HOT updates can only reuse dead tuples from previous transactions directly, not from the same or concurrent ones.<br />
If you have big updates, changing large portions of the table at once, you might want to split them up in a couple of chunks.</p></blockquote>
<blockquote><p>HOT updates only work when the changed columns are not involved in indexes in any way because the indexes pointing the the old tuples need to point to the new version of it as of transaction id. </p></blockquote>
<blockquote><p>You might be blocking HOT updates with indexes on updated columns. If those are expendable, you might get better overall performance without them.</p></blockquote>
<p><strong>Let&#8217;s understand PostgreSQL HOT practically.</strong></p>
<p><strong>Create a table with Sample data: </strong></p>
<pre class="crayon-plain-tag">CREATE TABLE tbl_Employees
(
	EmpID INT
	,EmpName CHARACTER VARYING 	
	,EmpDepartment CHARACTER VARYING
	,EmpSalary INT
);
 
INSERT INTO tbl_Employees
VALUES 
(1,'Anvesh','Database',90000)
,(2,'Jenny','JAVA',65000)
,(3,'Martin','PHP',85000)
,(4,'Roy','PHP',94000)
,(5,'Eric','PHP',70000)
,(6,'Rajesh','Animation',50000)
,(7,'Mahi','Database',40000)
,(8,'Sofia','JAVA',60000);</pre> </p>
<p><strong>Delete one record:</strong></p>
<pre class="crayon-plain-tag">DELETE FROM tbl_Employees WHERE EmpID = 7;</pre> </p>
<p><strong>Insert one New Record:</strong></p>
<pre class="crayon-plain-tag">INSERT INTO tbl_Employees
VALUES 
(7,'Junik','Database',10000);</pre> </p>
<p><strong>Please check If any HOT Update or not on table:</strong></p>
<pre class="crayon-plain-tag">SELECT pg_stat_get_tuples_hot_updated('tbl_employees'::regclass::oid);
/*
Result:
pg_stat_get_tuples_hot_updated
------------------------------
0
*/</pre> </p>
<p><strong>Please update the statestic of that table:</strong></p>
<pre class="crayon-plain-tag">ANALYZE tbl_Employees;</pre> </p>
<p><strong>Now execute one more update command:</strong></p>
<pre class="crayon-plain-tag">UPDATE tbl_Employees SET EmpName = 'Robort' WHERE EmpID=7;</pre> </p>
<p><strong>Please check If any HOT Update or not on table:</strong></p>
<pre class="crayon-plain-tag">SELECT pg_stat_get_tuples_hot_updated('tbl_employees'::regclass::oid);
/*
Result:
pg_stat_get_tuples_hot_updated
------------------------------
1
*/</pre> </p>
<p><strong>Again, do more update:</strong></p>
<pre class="crayon-plain-tag">UPDATE tbl_Employees SET EmpName = 'Manish' WHERE EmpID=8;
UPDATE tbl_Employees SET EmpName = 'Cerlin' WHERE EmpID=4;</pre> </p>
<p><strong>Please check If any change in number of HOT Update of a table:</strong></p>
<pre class="crayon-plain-tag">SELECT pg_stat_get_tuples_hot_updated('tbl_employees'::regclass::oid);
/*
Result:
pg_stat_get_tuples_hot_updated
------------------------------
3
*/</pre> </p>
<p><strong>Now Apply, Index on EmpName Column:</strong></p>
<pre class="crayon-plain-tag">CREATE INDEX idx_tbl_employees_empname 
ON tbl_employees USING BTREE (EmpName);</pre> </p>
<p><strong>After Index, execute few more update:</strong></p>
<pre class="crayon-plain-tag">UPDATE tbl_Employees SET EmpName = 'Kulin' WHERE EmpID=3;
UPDATE tbl_Employees SET EmpName = 'Priya' WHERE EmpID=5;</pre> </p>
<p><strong>After Index, Please check If any change in number of HOT Update of a table:</strong><br />
The result is same because If column has an Index, HOT Update does not work.</p>
<pre class="crayon-plain-tag">SELECT pg_stat_get_tuples_hot_updated('tbl_employees'::regclass::oid);
/*
Result:
pg_stat_get_tuples_hot_updated
------------------------------
3
*/</pre> </p>
<p><strong>You can also check number of HOT update of current transaction:</strong></p>
<pre class="crayon-plain-tag">SELECT pg_stat_get_xact_tuples_hot_updated('tbl_employees'::regclass::oid);</pre> </p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2016/12/postgresql-increase-the-speed-of-update-query-using-hot-update-heap-only-tuple-mvcc-fill-factor-vacuum-fragmentation/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>PostgreSQL: Update the Table data using Subquery</title>
		<link>https://www.dbrnd.com/2016/03/postgresql-update-the-table-data-using-subquery/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Sat, 19 Mar 2016 21:03:58 +0000</pubDate>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[plpgsql]]></category>
		<category><![CDATA[Postgres Query]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[PostgreSQL Administrator]]></category>
		<category><![CDATA[PostgreSQL Error]]></category>
		<category><![CDATA[PostgreSQL Programming]]></category>
		<category><![CDATA[PostgreSQL Tips and Tricks]]></category>
		<category><![CDATA[subquery]]></category>
		<category><![CDATA[UPDATE]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=1910</guid>

					<description><![CDATA[In this post, I am going to share a demonstration on how to update the table data using a Subquery in the PostgreSQL. This is a fundamental help, but I found that most of the beginners always try to find the script for Subquery or Joins. Below is a small demonstration: Create two sample tables [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F03%2Fpostgresql-update-the-table-data-using-subquery%2F&amp;linkname=PostgreSQL%3A%20Update%20the%20Table%20data%20using%20Subquery" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F03%2Fpostgresql-update-the-table-data-using-subquery%2F&amp;linkname=PostgreSQL%3A%20Update%20the%20Table%20data%20using%20Subquery" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F03%2Fpostgresql-update-the-table-data-using-subquery%2F&amp;linkname=PostgreSQL%3A%20Update%20the%20Table%20data%20using%20Subquery" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F03%2Fpostgresql-update-the-table-data-using-subquery%2F&amp;linkname=PostgreSQL%3A%20Update%20the%20Table%20data%20using%20Subquery" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F03%2Fpostgresql-update-the-table-data-using-subquery%2F&amp;linkname=PostgreSQL%3A%20Update%20the%20Table%20data%20using%20Subquery" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F03%2Fpostgresql-update-the-table-data-using-subquery%2F&amp;linkname=PostgreSQL%3A%20Update%20the%20Table%20data%20using%20Subquery" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F03%2Fpostgresql-update-the-table-data-using-subquery%2F&#038;title=PostgreSQL%3A%20Update%20the%20Table%20data%20using%20Subquery" data-a2a-url="https://www.dbrnd.com/2016/03/postgresql-update-the-table-data-using-subquery/" data-a2a-title="PostgreSQL: Update the Table data using Subquery"></a></p><p>In this post, I am going to share a demonstration on how to update the table data using a Subquery in the PostgreSQL.</p>
<p>This is a fundamental help, but I found that most of the beginners always try to find the script for  Subquery or Joins.</p>
<p><strong>Below is a small demonstration:</strong></p>
<p><strong>Create two sample tables for Employee:</strong></p><pre class="crayon-plain-tag">CREATE TABLE tbl_EmployeeDetails 
(
	EmpID SERIAL PRIMARY KEY
	,EmpName CHARACTER VARYING(50)
	,TotalPerformanceCount INTEGER DEFAULT 0
);

CREATE TABLE tbl_EmployeePerformanceCounter
(
	PerformanceCounterID SERIAL PRIMARY KEY
	,EmpID INTEGER
	,PerformanceCounter INTEGER
	,RecordDateTime TIMESTAMP WITHOUT TIME ZONE
	,CONSTRAINT fk_tbl_EmployeePerformanceCounter_EmpID 
         FOREIGN KEY (EmpID) REFERENCES tbl_EmployeeDetails(EmpID)
);</pre><p>
<p><strong>Insert sample data into both the tables:</strong></p><pre class="crayon-plain-tag">INSERT INTO tbl_EmployeeDetails (EmpName)
VALUES ('Anvesh'),('Neevan'),('Martin'),('Roy'),('Jenny');

INSERT INTO tbl_EmployeePerformanceCounter
(EmpID,PerformanceCounter,RecordDateTime)
VALUES
(1,300,'20150101')
,(1,100,'20150201')
,(1,200,'20150301')
,(2,140,'20150101')
,(2,320,'20150201')
,(3,120,'20150101')
,(3,150,'20150201')
,(4,150,'20150101')
,(4,200,'20150201')
,(4,100,'20150301');</pre><p>
<p><strong>Update the count of the employee performance counter using Subquery:</strong></p><pre class="crayon-plain-tag">UPDATE tbl_EmployeeDetails
SET TotalPerformanceCount=T.TotalCount   
FROM (SELECT EmpID,SUM(PerformanceCounter) AS TotalCount
      FROM tbl_EmployeePerformanceCounter GROUP BY EmpID) AS T
WHERE tbl_EmployeeDetails.EmpID=T.EmpID;</pre><p>
<p><strong>The Result:</strong></p><pre class="crayon-plain-tag">SELECT *FROM tbl_EmployeeDetails ORDER BY 1;</pre><p>
<a href="https://www.dbrnd.com/wp-content/uploads/2016/03/PostgreSQLUpdateDataUsingSubquery.png" rel="attachment wp-att-1912"><img decoding="async" class="alignnone wp-image-1912 size-full" title="PostgreSQL Update Data Using Subquery" src="https://www.dbrnd.com/wp-content/uploads/2016/03/PostgreSQLUpdateDataUsingSubquery.png" alt="PostgreSQL Update Data Using Subquery" width="322" height="128" srcset="https://www.dbrnd.com/wp-content/uploads/2016/03/PostgreSQLUpdateDataUsingSubquery.png 322w, https://www.dbrnd.com/wp-content/uploads/2016/03/PostgreSQLUpdateDataUsingSubquery-300x119.png 300w, https://www.dbrnd.com/wp-content/uploads/2016/03/PostgreSQLUpdateDataUsingSubquery-150x60.png 150w" sizes="(max-width: 322px) 100vw, 322px" /></a></p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
