{"id":3967,"date":"2025-06-07T08:28:31","date_gmt":"2025-06-07T08:28:31","guid":{"rendered":"https:\/\/w3buddy.com\/?post_type=cposts&#038;p=3967"},"modified":"2025-06-07T10:00:26","modified_gmt":"2025-06-07T10:00:26","slug":"sql-select-into","status":"publish","type":"cposts","link":"https:\/\/w3buddy.com\/blog\/notes\/sql\/sql-select-into\/","title":{"rendered":"SQL SELECT INTO"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><code>SELECT INTO<\/code> lets you <strong>create a new table<\/strong> and <strong>insert data<\/strong> from an existing query \u2014 all in one step.<br>It\u2019s handy for quick backups, snapshots, or creating temporary tables.<\/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>SELECT column1, column2, ...\nINTO new_table\nFROM existing_table\nWHERE conditions;<\/code><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 How It Works<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Creates <code>new_table<\/code> with the <strong>same columns<\/strong> and <strong>data types<\/strong> as selected columns.<\/li>\n\n\n\n<li>Copies the <strong>data returned by the query<\/strong> into this new table.<\/li>\n\n\n\n<li>Only works if <code>new_table<\/code> <strong>does not already exist<\/strong>.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 Example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Create a table <code>high_salary_employees<\/code> with employees earning more than 70000:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>SELECT employee_id, employee_name, salary\nINTO high_salary_employees\nFROM employees\nWHERE salary &gt; 70000;<\/code><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 Notes<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Supported by <strong>SQL Server<\/strong> and some other DBMS (not standard in MySQL or Oracle).<\/li>\n\n\n\n<li>In MySQL, similar effect achieved with:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code><code>CREATE TABLE new_table AS\nSELECT column1, column2\nFROM existing_table\nWHERE conditions;<\/code><\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In Oracle, use <code>CREATE TABLE AS SELECT<\/code> (CTAS).<\/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>Key Point<\/th><th>Explanation<\/th><\/tr><\/thead><tbody><tr><td>Purpose<\/td><td>Create new table and fill it with query results<\/td><\/tr><tr><td>Table must not exist<\/td><td><code>new_table<\/code> should not exist before execution<\/td><\/tr><tr><td>DBMS differences<\/td><td><code>SELECT INTO<\/code> (SQL Server), <code>CREATE TABLE AS<\/code> (MySQL, Oracle)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udca1 Use <code>SELECT INTO<\/code> to quickly clone or filter data into a new table without writing separate CREATE and INSERT commands.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SELECT INTO lets you create a new table and insert data from an existing query \u2014 all in one step.It\u2019s handy for quick backups, snapshots, or creating temporary tables. \ud83d\udd39 Basic Syntax \ud83d\udd39 How It Works \ud83d\udd39 Example Create a table high_salary_employees with employees earning more than 70000: \ud83d\udd39 Notes \ud83e\udde0 Quick Recap Key Point [&hellip;]<\/p>\n","protected":false},"template":"","meta":{"googlesitekit_rrm_CAowu461DA:productID":""},"categories":[952,979],"class_list":["post-3967","cposts","type-cposts","status-publish","hentry","category-notes","category-sql"],"_links":{"self":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/cposts\/3967","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=3967"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/categories?post=3967"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}