![]() |
SQLite is a lightweight, serverless RDBMS that is used to store and manipulate the data in the database in relational or tabular form. It can run on various operating systems like Windows, macOS, Linux and mobile operating systems like Android and iOS. SQLite is designed for single-user access. Multiple processes can read from an SQLite database simultaneously but only one process can write at a time. In this article, we are going to see how we can update table rows in SQLite using subquery. Introduction to Update Statement in SQLiteThe UPDATE statement is a SQLite command that is used to update or change the already existing record from the table. Update Statement allows to make changes to one or more columns within a specified table based on specific conditions such as conditions defined in the WHERE clause. Basic Syntax: UPDATE table_name Explanation:
Setting Up EnvironmentLet us start by creating a table and adding some sample data to the table. The following query creates two sample tables and inserts records in them: Query: //first table creation After Inserting data the table test1 looks like: ![]() test1 initial data After Inserting data the table test2 looks like: ![]() test2 initial data Update Rows Using SubqueryWe can make use of UPDATE statement with a subquery to update tables based on some complex logic. The subquery allows us to interact with other tables among other during the update statement. The following query updates the test1 table records which are present in test2 table with the values of the test2 table: Query: UPDATE test1 Output: ![]() Updated data in test1 Explanation: As we can see the records with ids 1 and 9 were updated in the table. Example of How to Update Table Rows in SQLite Using SubqueryLet’s create some tables and insert some data in it. The following query creates a department table and inserts some records in it. Query: -- Create departments table Output: ![]() Department table Let’s creates a another table called employee and inserts some records in it. Query: -- Create employees table Output: ![]() employees table Let’s creates a another table called salaries and inserts some records in it. Query: -- Create salaries table Output: ![]() salaries table We will write query to update the salary of all the employees in the ‘Engineering‘ department by increasing the salary by 10%. We can make use of UPDATE statement with subquery to achieve this. We will first filter the employee id of all the employees in the ‘Engineering’ department using subquery and later use that information in the UPDATE statement to modify the salary. The following query updates the salary of all the employees in ‘Engineering‘ department by 10%: Query: UPDATE salaries Output: ![]() updated salary data Explanation: As we can see the salary of employee 1, i.e. John Doe, increased from 50000 to 55000 and that of employee 3, i.e. Michael Johnson, increased from 55000 to 60500. ConclusionAfter reading whole article now we have good understanding of how to update data in SQLite using Subquery. In this article we have seen a example and perform queries to get good understanding. Now you can easily update the data into the table using the subquery. |
Reffered: https://www.geeksforgeeks.org
Databases |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |