How to Write SELECT and INSERT in Single Query

Table inserts are very common when you are a SQL developer or Data analysis specialist. In this post, I have explained some ideas on inserting values using SELECT.


To insert data you need three key statements:

  1. INSERT
  2. INTO
  3. VALUES

Sample SQL Query

INSERT INTO my_table (name, id, salary)
VALUES ('Srinimf', 1001, 150000);

While inserting, if you know the values are in sequence to columns then not needed to give column names else you need to give.

The remaining columns when you not inserted the values will have their default values – Like NULL.

All NOT NULL columns you must supply values.

In the above example, I have not used SELECT.



How to INSERT values using SELECT

This concept also called cross checking. That means after INSERT you can get the data inserted.

The Key Statements you need

  1. SELECT
  2. NEW TABLE
  3. INSERT
  4. INTO
  5. VALUES

Example SQL Query

SELECT * FROM NEW_TABLE (
INSERT INTO my_table (name, id, salary)
VALUES ('Srinimf', 1001, 150000)
);

From the above Query you can get the data after inserting values. This example would work well in DB2.

Related Posts

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.