Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Databases
  • SQL
  • MySQL
  • PostgreSQL
  • PL/SQL
  • MongoDB
  • SQL Cheat Sheet
  • SQL Interview Questions
  • MySQL Interview Questions
  • PL/SQL Interview Questions
  • Learn SQL and Database
Open In App
Next Article:
How to Update Table Rows in PL/SQL Using Subquery?
Next article icon

How to Update Table Rows in SQLite Using Subquery

Last Updated : 08 Feb, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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 SQLite

The 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
SET column1 = value1, column2 = value2, ...
WHERE condition;

Explanation:

  • table_name: It is a name of the table from which records will updated.
  • column1 and column2: It represent the columns within the table that will updated.
  • value1 and value2: They are the new values to be assigned to the respective columns.

Setting Up Environment

Let 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
CREATE TABLE test1
(
id INT,
name VARCHAR(20),
age INT
);

INSERT INTO test1 VALUES(1, 'Alex', 20);
INSERT INTO test1 VALUES(4, 'Jane', 34);
INSERT INTO test1 VALUES(9, 'Tyler', 11);

//Second table creation
CREATE TABLE test2
(
id INT,
name VARCHAR(20),
age INT
);

INSERT INTO test2 VALUES(1, 'Austin', 25);
INSERT INTO test2 VALUES(5, 'Jesse', 34);
INSERT INTO test2 VALUES(9, 'Tyler', 23);
INSERT INTO test2 VALUES(3, 'Smith', 11);

After Inserting data the table test1 looks like:

test1
test1 initial data

After Inserting data the table test2 looks like:

test2
test2 initial data

Update Rows Using Subquery

We 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
SET
name=t.name,
age=t.age
FROM (
SELECT * FROM test2
) t
WHERE test1.id=t.id;

Output:

usingSubquery
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 Subquery

Let'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
CREATE TABLE departments
(
dept_id SERIAL PRIMARY KEY,
dept_name VARCHAR(100) NOT NULL
);

-- Insert sample data into departments table
INSERT INTO departments (dept_name) VALUES
('Engineering'),
('Sales'),
('Marketing');


Output:

department
Department table

Let's creates a another table called employee and inserts some records in it.

Query:

-- Create employees table
CREATE TABLE employees
(
emp_id SERIAL PRIMARY KEY,
emp_name VARCHAR(100) NOT NULL,
dept_id INT NOT NULL,
FOREIGN KEY (dept_id) REFERENCES departments(dept_id)
);

-- Insert sample data into employees table
INSERT INTO employees (emp_name, dept_id) VALUES
('John Doe', 1), -- Engineering department
('Jane Smith', 2), -- Sales department
('Michael Johnson', 1), -- Engineering department
('Emily Davis', 3); -- Marketing department

Output:

employee
employees table

Let's creates a another table called salaries and inserts some records in it.

Query:

-- Create salaries table
CREATE TABLE salaries (
salary_id SERIAL PRIMARY KEY,
emp_id INT NOT NULL,
salary_amount NUMERIC(10, 2) NOT NULL,
FOREIGN KEY (emp_id) REFERENCES employees(emp_id)
);

-- Insert sample data into salaries table
INSERT INTO salaries (emp_id, salary_amount) VALUES
(1, 50000.00), -- John Doe's salary
(2, 60000.00), -- Jane Smith's salary
(3, 55000.00), -- Michael Johnson's salary
(4, 52000.00); -- Emily Davis's salary

Output:

salaries
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
SET salary_amount = salary_amount * 1.1
WHERE emp_id IN (
SELECT emp_id
FROM employees
WHERE dept_id = (
SELECT dept_id
FROM departments
WHERE dept_name = 'Engineering'
)
);

Output:

Updatedata
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.

Conclusion

After 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.


Next Article
How to Update Table Rows in PL/SQL Using Subquery?

D

dvsingla28
Improve
Article Tags :
  • SQLite
  • Databases
  • SQLite Query

Similar Reads

  • How to Update Table Rows in PL/SQL Using Subquery?
    Updating table rows in PL/SQL via subqueries allows precise data modifications based on specific conditions. By integrating subqueries within the UPDATE statement, rows can be selectively targeted for updates, enhancing data management efficiency. This article explores the concept of updating rows i
    4 min read
  • How to Update Table Rows in SQL Server using Subquery ?
    Updating table rows in SQL Server using a subquery is a common operation that allows us to modify records based on values derived from another table or query. In this article, we will explain how to update table rows in SQL Server using subquery with the help of examples. Updating Table Rows Using a
    3 min read
  • How to Update Table Rows Using Subquery in MySQL
    Updating table rows using subqueries in MySQL enables precise modifications based on specific conditions or values from other tables. This technique leverages subqueries within the SET or WHERE clauses of the UPDATE statement, allowing dynamic and context-specific updates. This guide covers the synt
    5 min read
  • How to Update Table Rows in PostgreSQL Using Subquery?
    PostgreSQL is a general-purpose object-relational database management system and is open-source. It ensures data integrity features like constraints, transactions and foreign key support. In this article, We will understand How to update table rows in PostgreSQL using subquery using various methods,
    5 min read
  • How to Use the IN Operator with a SubQuery in SQL?
    In this article, we will see the use of IN Operator with a SubQuery in SQL. IN operator is used to compare the column values with the list of values. The list of values is defined after IN query in SQL. If we don't know the exact list of values to be compared, we can generate the list of values usin
    2 min read
  • How to list tables using SQLite3 in Python ?
    In this article, we will discuss how to list all the tables in the SQLite database using Python. Here, we will use the already created database table from SQLite. We will also learn exception handling during connecting to our database. Database Used: Steps to Fetch all tables using SQLite3 in Python
    2 min read
  • How to Update Data in MySQL Database Table Using PHP?
    Updating data in a MySQL database table using PHP is a fundamental aspect of web development, particularly in applications where user interactions involve modifying existing records. This guide delves into the process of updating data in a MySQL database table using PHP, covering database connection
    3 min read
  • SQL Query to Update All Rows in a Table
    Updating all rows in a SQL table is a common task when we need to make changes to every record without specifying individual rows. This operation can be performed using the UPDATE statement combined with the SET clause, which allows for modifying values across all rows of a specified table. Understa
    5 min read
  • How to Update All Rows in SQL?
    Updating records in an SQL database is a fundamental operation used to modify existing data. The UPDATE command is the go-to method for making such modifications, whether for all rows in a table or a subset based on specific conditions. In this article, we will explain how to update all rows in SQL
    4 min read
  • How to Update Multiple Rows at Once Using PL/SQL?
    Updating multiple rows simultaneously is a common requirement in database management, especially when handling large datasets. PL/SQL, the procedural extension of SQL in Oracle databases, provides various techniques to accomplish this task efficiently. In this article, we will explore three powerful
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences