In the world of databases, data manipulation is a crucial skill to possess. SQLite, a lightweight and versatile database engine, offers a powerful way to insert data into tables using the INSERT INTO SELECT
statement. This article will guide you through the intricacies of this technique, empowering you to efficiently populate your SQLite tables with ease.
By using the SQLite INSERT INTO SELECT
statement, you can efficiently populate tables with data from other tables or queries, streamlining your data manipulation tasks and enhancing your database management capabilities.
Creating a Sample Table
Before we dive into the INSERT INTO SELECT
statement, let’s create a sample table to work with. Imagine you have a business that tracks sales data. Let’s create a table called sales
to store this information:
CREATE TABLE sales (
id INTEGER PRIMARY KEY,
product TEXT,
quantity INTEGER,
price REAL,
date TEXT
);
Now, let’s populate the sales
table with some sample data:
INSERT INTO sales (product, quantity, price, date)
VALUES
('Widget A', 10, 9.99, '2023-05-01'),
('Gadget B', 5, 19.99, '2023-05-02'),
('Widget A', 8, 9.99, '2023-05-03'),
('Gizmo C', 3, 14.99, '2023-05-04');
Understanding INSERT INTO SELECT
The INSERT INTO SELECT
statement allows you to insert data into a table by selecting data from another table or query. It combines the INSERT INTO
and SELECT
statements, enabling you to populate a table with the results of a SELECT
query.
The basic syntax of INSERT INTO SELECT
is as follows:
INSERT INTO table_name (column1, column2, ...)
SELECT column1, column2, ...
FROM source_table
WHERE condition;
Let’s break down the components of this statement:
table_name
: The name of the table into which you want to insert data.column1, column2, ...
: The columns in the target table that will receive the inserted data.SELECT column1, column2, ...
: TheSELECT
query that retrieves the data to be inserted.source_table
: The table from which the data is selected.WHERE condition
: An optional condition to filter the selected data.
Inserting Data from Another Table
Suppose you have another table called inventory
that contains information about your product inventory:
CREATE TABLE inventory (
id INTEGER PRIMARY KEY,
product TEXT,
quantity INTEGER
);
INSERT INTO inventory (product, quantity)
VALUES
('Widget A', 100),
('Gadget B', 50),
('Gizmo C', 75);
Now, let’s say you want to insert the inventory data into the sales
table. You can use the INSERT INTO SELECT
statement as follows:
INSERT INTO sales (product, quantity, price, date)
SELECT product, quantity, 0, '2023-05-05'
FROM inventory;
In this example, we select the product
and quantity
columns from the inventory
table and insert them into the corresponding columns of the sales
table. We set the price
to 0 and the date
to ‘2023-05-05’ for all inserted rows.
After executing this statement, the sales
table will contain the following data:
id | product | quantity | price | date
---+----------+----------+-------+------------
1 | Widget A | 10 | 9.99 | 2023-05-01
2 | Gadget B | 5 | 19.99 | 2023-05-02
3 | Widget A | 8 | 9.99 | 2023-05-03
4 | Gizmo C | 3 | 14.99 | 2023-05-04
5 | Widget A | 100 | 0.00 | 2023-05-05
6 | Gadget B | 50 | 0.00 | 2023-05-05
7 | Gizmo C | 75 | 0.00 | 2023-05-05
As you can see, the inventory data has been inserted into the sales
table, with the specified price
and date
values.
Inserting Data with Calculated Values
The INSERT INTO SELECT
statement also allows you to perform calculations or transformations on the selected data before inserting it into the target table.
For example, let’s say you want to insert sales data with a 10% discount applied to the price:
INSERT INTO sales (product, quantity, price, date)
SELECT product, quantity, price * 0.9, '2023-05-06'
FROM sales
WHERE date = '2023-05-03';
In this case, we select the product
, quantity
, and price
columns from the sales
table where the date
is ‘2023-05-03’. We multiply the price
by 0.9 to apply a 10% discount and set the date
to ‘2023-05-06’ for the inserted rows.
After executing this statement, the sales
table will contain the following data:
id | product | quantity | price | date
---+----------+----------+--------+------------
1 | Widget A | 10 | 9.99 | 2023-05-01
2 | Gadget B | 5 | 19.99 | 2023-05-02
3 | Widget A | 8 | 9.99 | 2023-05-03
4 | Gizmo C | 3 | 14.99 | 2023-05-04
5 | Widget A | 100 | 0.00 | 2023-05-05
6 | Gadget B | 50 | 0.00 | 2023-05-05
7 | Gizmo C | 75 | 0.00 | 2023-05-05
8 | Widget A | 8 | 8.99 | 2023-05-06
The new row with id
8 represents the discounted sale of “Widget A” on ‘2023-05-06’.
Inserting Data with Aggregated Values
You can also use the INSERT INTO SELECT
statement with aggregate functions to insert summarized data into a table.
For instance, let’s create a table called daily_sales
to store the total sales quantity and revenue for each date:
CREATE TABLE daily_sales (
date TEXT,
total_quantity INTEGER,
total_revenue REAL
);
Now, you can use INSERT INTO SELECT
with aggregate functions to populate the daily_sales
table:
INSERT INTO daily_sales (date, total_quantity, total_revenue)
SELECT date, SUM(quantity), SUM(quantity * price)
FROM sales
GROUP BY date;
In this example, we select the date
column and calculate the total quantity and total revenue using the SUM
aggregate function. We group the results by the date
column to get the aggregated values for each unique date.
After executing this statement, the daily_sales
table will contain the following data:
date | total_quantity | total_revenue
-----------+----------------+--------------
2023-05-01 | 10 | 99.90
2023-05-02 | 5 | 99.95
2023-05-03 | 8 | 79.92
2023-05-04 | 3 | 44.97
2023-05-05 | 225 | 0.00
2023-05-06 | 8 | 71.92
Each row represents the total quantity and revenue for a specific date, providing a summarized view of the sales data.
Conclusion
The SQLite INSERT INTO SELECT
statement is a powerful tool for inserting data into tables based on the results of a SELECT
query. Whether you want to insert data from another table, perform calculations, or aggregate values, INSERT INTO SELECT
offers flexibility and efficiency in data manipulation.
By understanding and utilizing the INSERT INTO SELECT
statement, you can streamline your data insertion tasks, populate tables with calculated or aggregated values, and efficiently manage your SQLite databases.
Remember to experiment with different scenarios, explore the potential of combining INSERT INTO SELECT
with other SQL clauses, and adapt this technique to suit your specific business needs. With practice and creativity, you’ll unlock the full potential of SQLite’s data insertion capabilities.