{"id":3925,"date":"2025-06-06T20:34:19","date_gmt":"2025-06-06T20:34:19","guid":{"rendered":"https:\/\/w3buddy.com\/?post_type=cposts&#038;p=3925"},"modified":"2025-06-07T09:56:59","modified_gmt":"2025-06-07T09:56:59","slug":"sql-insert-select","status":"publish","type":"cposts","link":"https:\/\/w3buddy.com\/blog\/notes\/sql\/sql-insert-select\/","title":{"rendered":"SQL INSERT SELECT"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><code>INSERT SELECT<\/code> is used to <strong>copy data from one table into another<\/strong>. This is very useful for data migration, backups, or transformations.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 Basic Syntax<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code><code>INSERT INTO target_table (col1, col2, ...)\nSELECT col1, col2, ...\nFROM source_table\nWHERE condition;<\/code><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 Example<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code><code>-- Copy employees from 'IT' department into backup table\nINSERT INTO employees_backup (id, name, department)\nSELECT employee_id, name, department\nFROM employees\nWHERE department = 'IT';<\/code><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 Insert All Columns (If Structure Matches)<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code><code>INSERT INTO employees_archive\nSELECT * FROM employees\nWHERE status = 'inactive';<\/code><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 Oracle Notes<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Works the same way in Oracle.<\/li>\n\n\n\n<li>You can also use <code>INSERT \/*+ APPEND *\/<\/code> for performance tuning in large loads.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udde0 Quick Recap<\/h2>\n\n\n\n<figure class=\"wp-block-table has-small-font-size\"><table><thead><tr><th>Use Case<\/th><th>Example Syntax<\/th><\/tr><\/thead><tbody><tr><td>Copy selected rows<\/td><td><code>INSERT INTO target SELECT ... FROM source<\/code><\/td><\/tr><tr><td>Insert all columns<\/td><td><code>INSERT INTO target SELECT * FROM source<\/code><\/td><\/tr><tr><td>With condition\/filter<\/td><td>Add <code>WHERE<\/code> clause to the <code>SELECT<\/code> part<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 Ideal for copying, archiving, or transforming data between tables<br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>INSERT SELECT is used to copy data from one table into another. This is very useful for data migration, backups, or transformations. \ud83d\udd39 Basic Syntax \ud83d\udd39 Example \ud83d\udd39 Insert All Columns (If Structure Matches) \ud83d\udd39 Oracle Notes \ud83e\udde0 Quick Recap Use Case Example Syntax Copy selected rows INSERT INTO target SELECT &#8230; FROM source Insert [&hellip;]<\/p>\n","protected":false},"template":"","meta":{"googlesitekit_rrm_CAowu461DA:productID":""},"categories":[952,979],"class_list":["post-3925","cposts","type-cposts","status-publish","hentry","category-notes","category-sql"],"_links":{"self":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/cposts\/3925","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/cposts"}],"about":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/types\/cposts"}],"wp:attachment":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/media?parent=3925"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/categories?post=3925"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}