SQLforGeeks
  • Home
  • SQL
  • SQL Tutorial
  • SQL Syntax
  • Our Services
Contact
  1. SQL
  2. INSERT INTO SELECT statement

Text copied!

« Previous
Next »

INSERT INTO SELECT statement

April 20, 2023, 7:19 p.m. under SQL

  • Hi! You should check SQL self join post first.

    You can use "INSERT INTO" with "SELECT" statement to copy or transfer data between SQL tables.

    It is commonly referred to as the "INSERT INTO SELECT" statement.

    Here's the syntax for "INSERT INTO SELECT" statement :
    INSERT INTO destination_table_name
    (column1, column2, column3, ...)
    SELECT 
    column1, column2, column3, ...
    FROM source_table_name;
            
    The rules for using the "INSERT INTO SELECT" statement are :

    1. The destination table must exist.

    2. The columns in the source and destination tables must match in terms of data type and order.

    3. The "SELECT" statement must return the same number of columns as the destination table.

    4. INSERT permissions are required on the destination table.

    5. You can specify specific columns in the "INSERT INTO" clause.

    6. SELECT Statement can select data from one or more tables by using UNION or JOIN.

    Here's an example of how to copy or transfer data between SQL tables :

    1. Suppose we have two tables, named [Product1] and [Product2] where [Product1] contains some records and [Product2] is currently empty.

    INSERT INTO SELECT statement

    2. Let's copy or transfer the data from product1 to product2. Run the below code :

    INSERT INTO [Product2] 
    ( [DepartmentID], [Name], [GroupName], [ModifiedDate] )
    SELECT
     [DepartmentID], [Name], [GroupName], [ModifiedDate] 
    FROM
    [Product1];
            
    INSERT INTO SELECT statement

    3. Above statement will copy or transfer the data from [Product1] table to [Product2] table for the all specified columns.

    INSERT INTO SELECT statement

    That's it! You have successfully copy or transfer data between SQL tables.

    Frequently Asked Questions :

    What does the INSERT into SELECT statement do?

    The INSERT INTO SELECT statement inserts data from one table into another based on a specified condition or criteria.

    Can we use INSERT and SELECT together?

    Yes, INSERT and SELECT can be used together to insert data retrieved from one table into another table.

    What is the difference between INSERT into SELECT and SELECT into INSERT?

    The main difference between INSERT INTO SELECT and SELECT INTO INSERT is that the former inserts data from one table into another, while the latter creates a new table based on the result of a SELECT query.

    What is INSERT into SELECT in SQL?

    INSERT INTO SELECT in SQL is a statement used to insert data from one table into another based on specific criteria defined in the SELECT query.
    Thank You! You should check INSERT INTO statement post next.
    « Previous
    Next »
    RELATED :

    What is SQL

    What is T-SQL

    Difference between DBMS and Data Warehouse

    Download SQL Server

    Install SQL Server

    Download SQL Server Management Studio SSMS

    SQL Server Management Studio

    SQL Database

    Download database

    Restore database

    Backup database

    Attach database

    Detach database

    Create database

    Delete database

    Rename database

    Select database

    Database offline

    Database online

    SQL Commands

    SQL Tables

    Create table

    Truncate table

    Delete table

    Rename table

    Select table

    Alter table

    SQL Data Types

    SQL Comments

    SQL Constraints

    SQL Joins

    SQL inner join

    SQL left join

    SQL right join

    SQL full join

    SQL cross join

    SQL self join

    INSERT INTO statement

    SQL Clauses

    SELECT clause

    FROM clause

    WHERE clause

    GROUP BY clause

    HAVING clause

    ORDER BY clause

    JOIN clause

    UNION clause

    UNION ALL clause

    TOP clause

    DISTINCT clause

    SQL Operators

    SQL Arithmetic operators

    SQL Comparison operators

    SQL Logical operators

    UNION operator

    UNION ALL operator

    INTERSECT operator

    EXCEPT operator

    LIKE operator

    NOT LIKE operator

    IN operator

    NOT IN operator

    IS NULL operator

    IS NOT NULL operator

    EXISTS operator

    NOT EXISTS operator

    BETWEEN operator

    NOT BETWEEN operator

    SQL Functions

    SQL Built-In functions

    CHARINDEX function

    DATEADD function

    CONCAT function

    LEN function

    REPLACE function

    SUBSTRING function

    CASE statement

    GETDATE function

    DATEPART function

    DATEDIFF function

    CAST function

    TRY_CAST function

    CONVERT function

    TRY_CONVERT function

    ISNULL function

    NULLIF function

    COALESCE function

    SQL Window functions

    ROW_NUMBER function

    RANK function

    DENSE_RANK function

    IIF function

    CHOOSE function

    SQL Store Procedure

    Store Procedure vs. Function

    SQL Subquery

    SQL Aliases

    Temp table

    SQL Error Handling

    SQL Variables

    SQL Views

    SQL Merge

    SQL CTE

    SQL WITH TIES

    Define Transaction in DBMS

    ACID properties in DBMS

    Types of Triggers in DBMS


    • Have Some Questions?
    logo

    Elevate your data experience with SQL excellence

    Quick Links
    •  Home
    •  SQL Tutorial
    •  SQL Syntax
    •  Our Services
    Our Services
    • Web Development
    • BI Development
    • Data Warehousing
    • Data Integration ETL


    Follow Us

    GST Registered: XXAXXXXXXXZX
    Mumbai, Maharashtra, India

    © 2024 Copyright | All Rights Reserved | Privacy Policy