A Quick Guide to SQL — Chapter 3: Manipulating Data

Sajjad Hadi
3 min readJun 10, 2023

--

In the previous chapter we learned about filtering and sorting data. In this lesson, we will explore how to manipulate data in SQL. We will cover the essential operations of inserting new records, updating existing records, and deleting unwanted records. By the end of this lesson, you will be able to confidently modify the data in your SQL tables. Let’s get started!

Chapters of This Series

  1. Chapter 1: Introduction and Basic Syntax
  2. Chapter 2: Filtering and Sorting Data
  3. Chapter 3: Manipulating Data
  4. Chapter 4: Querying Multiple Tables with JOIN
  5. Chapter 5: Aggregating Data
  6. Chapter 6: Modifying Data
  7. Chapter 7: Advanced SQL Concepts
  8. Chapter 8: Modifying Table Structure

1. Review of INSERT Statement

The INSERT statement is used to add new records to a table. Let’s assume we have a table called “employees” with columns “id,” “name,” “age,” and “salary.” To insert a new employee into the table, use the following syntax:

INSERT INTO employees (id, name, age, salary)
VALUES (1, 'John Doe', 30, 50000.00);

This statement inserts a new record with values 1 for “id,” ‘John Doe’ for “name,” 30 for “age,” and 50000.00 for “salary.”

2. Updating Existing Records using UPDATE Statement

The UPDATE statement allows us to modify existing records in a table. Let’s say we want to update the salary of an employee with “id” 1. Use the following syntax:

UPDATE employees
SET salary = 55000.00
WHERE id = 1;

This statement updates the “salary” column to 55000.00 for the employee with “id” 1.

3. Deleting Records using DELETE Statement

The DELETE statement enables us to remove unwanted records from a table. For instance, to delete an employee with “id” 1, execute the following statement:

DELETE FROM employees
WHERE id = 1;

This statement deletes the record(s) that match the specified condition.

4. Using WHERE Clause with UPDATE and DELETE Statements

Both the UPDATE and DELETE statements can utilize the WHERE clause to modify or delete specific records based on conditions. For example, let’s update the salaries of employees older than 40:

UPDATE employees
SET salary = salary * 1.1
WHERE age > 40;

This statement increases the salary of employees over 40 years old by 10%.

Similarly, let’s delete all employees with a salary less than 30000.00:

DELETE FROM employees
WHERE salary < 30000.00;

This statement removes all records where the salary is below 30000.00.

5. Conclusion

In this lesson, we covered the fundamental data manipulation operations in SQL. We reviewed the INSERT statement to add new records to a table, the UPDATE statement to modify existing records, and the DELETE statement to remove unwanted records. We also explored using the WHERE clause with UPDATE and DELETE statements to apply conditions for selective modifications or deletions. With these powerful techniques, you can confidently manipulate data in your SQL tables, ensuring your data remains accurate and up to date.

If you found this course helpful and would like to explore more free courses, I invite you to follow my account on Medium and connect with me on LinkedIn. I regularly share valuable content on these platforms.

--

--