SQLite INSERT INTO SELECT
Last Updated :
13 Feb, 2024
SQLite is a lightweight and server-less relational database management system. It requires very minimal configuration which has proven to be very helpful for developers to integrate it into any applications with ease. Due to its server-less architecture, we can use SQLite in various mobile applications as well as in small desktop applications. In this article, we are going to explore the "INSERT INTO SELECT" statement in SQLite. We are going to cover all its use cases with clear and concise examples along with their respective explanations.
INSERT INTO SELECT statement
In SQLite, an INSERT INTO SELECT statement is used to copy one or more tables data into another table. We can insert or copy complete data from one table to another as a whole or with some modifications. Simply it is a statement that allows us to perform data retrieval and data insertion tasks in a single statement.
Syntax:
INSERT INTO table_1 (column_1, column_2,…………………)
SELECT (column_1, column_2,…………………)
FROM table_2;
Examples of INSERT INTO SELECT Statements in SQLite
We already have two tables present in our database. The tables are courses and new_course_info. The data of these two mentioned tables are shown below
Table: courses
Table - coursesTable : new_course_info
Table - new_course_infoWe need to create one more table in our database in order to perform operations on that table.
Query:
CREATE TABLE geeksforgeeks
(
id INTEGER,
name TEXT,
courses INTEGER,
total_score INTEGER
);
As we have create a new table "geeksforgeeks", Now we are good to go.
Example 1: Inserting Complete Table's Data Into Another Table
In this example, we are going to copy one table data completely to another table. For this example, we are going to copy the courses table data completely to our geeksforgeeks table. We are using only table to copy our data without any filtering.
Query:
INSERT INTO geeksforgeeks(id, name, courses, total_score)
SELECT *
FROM courses;
Output:
copying data from single tableExplanation : We can clearly observe that the table above is completely identical to courses table. This is because we have copied the complete data of courses table into our geeksforgeeks table without any filtering.
Example 2: Inserting Data From Multiple Table
In this example, we will combine data of courses and new_course_info tables and insert it into our table geeksforgeeks. Unlike in the previous example, we will use our two tables to fecth data and insert it into our source table.
Query:
INSERT INTO geeksforgeeks(id, name, courses, total_score)
SELECT c.user_id, c.user_name, n. courses, c.total_score
FROM courses c, new_course_info n
where n.id = c.user_id;
Output:
Copying data from multiple tablesExplanation: We can clearly observe that above table has all the fields similar to our courses table except courses column. For this column, we have taken data from from our new_course_info table. Therefore the above image has all the field data similar to courses table except one.
Example 3: INSERT INTO SELECT Statement With WHERE Clause.
In this example, we will use INSERT INTO SELECT Statement with WHERE Clause. We will insert one table data into another table with filtering. Unlike in other examples we have seen above, are not copy the complete data from one table to another table, but we will filter them and then insert them into our table. For this example, we will copy all the data from the courses table to the geeksforgeeks table where user id is less than or equal to 4.
Query:
INSERT INTO geeksforgeeks(id,name,courses,total_score)
SELECT *
FROM courses where user_id <= 4;
Output:
Insert into select statement with where clause Explanation : We can clearly observe that the above output displayed has all the data copied from courses table, but the last row has been not added. This is because last row has id = 5 , that exceeds our minimum id criteria. Therefore, the last row has not been added.
Conclusion
Overall, INSERT INTO SELECT statement is used to copy data from one or more table to another table with or without filtering. We can completely copy one table data to another or add some modifications to it. We have covered it various examples such as inserting complete table's data into another table, inserting data from multiple table and INSERT INTO SELECT Statement with WHERE Clause. Now you have a good understanding on how to write INSERT INTO SELECT statement with different types of scenarios. Now you can easily perform queries related to it and can get the desired output.
Similar Reads
PL/SQL INSERT INTO SELECT
In PL/SQL, the INSERT INTO SELECT statement is used to insert data into a table by selecting data from one or more tables. This is a powerful feature for populating tables with data from existing tables or views, making it useful for data migration, reporting, and backup processes.In this guide, we
5 min read
SQL INSERT INTO SELECT Statement
In SQL, the INSERT INTO statement is used to add or insert records into the specified table. We use this statement to insert data directly into a table by specifying column names in a specific order. The SELECT statement is used to retrieve data from the table, and it can be used in conjunction with
5 min read
PL/SQL INSERT INTO
PL/SQL (Procedural Language/Structured Query Language) is Oracle's procedural extension to SQL. It allows us to write complex queries and scripts that include procedural logic, control structures, and error handling. The INSERT INTO statement in PL/SQL is essential for adding new rows of data to tab
6 min read
MySQL INSERT INTO SELECT Statement
MySQL is an open-source relational database management system that uses Structured Query Language (SQL) to manipulate databases. It stores data in a table format. It provides various statements to perform Create, Read, Update, and Delete operations on a database table. INSERT INTO SELECT statement i
5 min read
SQL | INSERT INTO Query
In SQL, managing data effectively involves the ability to insert new rows into a table. TheINSERT INTO statement is used to add data to a database table. This statement provides two methods for inserting rows: inserting only values and inserting values with column names. Each method has its own synt
4 min read
SQLite SELECT Query
SQLite is a serverless, popular, easy-to-use, relational database system that is written in c programming language. SQLite is a database engine that is built into all the popular devices that we use every day of our lives including TV, Mobile, and Computer. As we know that we create the tables in th
6 min read
SQL INSERT INTO Statement
The SQL INSERT INTO statement is one of the most commonly used commands for adding new data into a table in a database. Whether you're working with customer data, products, or user details, mastering this command is crucial for efficient database management. Letâs break down how this command works,
6 min read
PL/SQL SELECT INTO Existing Table
PL/SQL is a programming language that is used alongside SQL for writing procedural code such as stored procedures, functions, triggers, and packages within the Oracle Database. It was developed by Oracle Corporation and is widely used in database programming.PL/SQL is a programming language that has
5 min read
MySQL INSERT INTO Statement
In MySQL, the INSERT INTO statement is essential for adding new data rows to a table in a database. This is important for setting up initial data in tables and for adding new records as needed when working with the database. Understanding how to use the INSERT INTO statement is key for managing and
6 min read
SQL SELECT INTO Statement
The SELECT INTO statement in SQL is a powerful and efficient command that allow users to create a new table and populate it with data from an existing table or query result in a single step. This feature is especially useful for creating backups, extracting specific subsets of data, or preparing new
5 min read