By WebNest Studio Engineering Team
Database and SQL Tutorial
SQL CRUD Operations
CRUD stands for Create, Read, Update, and Delete — the four fundamental operations every application performs on stored data. In SQL, these map directly to four core statements: INSERT, SELECT, UPDATE, and DELETE. Mastering these four statements covers the majority of day-to-day database work.
Why it matters
Create, read, update and delete are the four things every application does with data: registering, viewing a profile, editing it, closing the account. These four statements are the SQL you will write most, and a forgotten WHERE clause on an update or delete is one of the classic ways to damage a production database.
INSERT — Creating Rows
INSERT INTO table (columns...) VALUES (values...) adds one or more new rows. Listing column names explicitly (rather than relying on column order) makes statements resilient to future schema changes and easier to read.
SELECT — Reading Rows
SELECT columns FROM table WHERE condition retrieves rows. SELECT * retrieves every column, but naming specific columns is generally preferred in real applications since it avoids pulling unnecessary data and breaking if columns are later added.
UPDATE and DELETE — Modifying and Removing Rows
UPDATE table SET column = value WHERE condition modifies existing rows that match the condition. DELETE FROM table WHERE condition removes matching rows entirely. In both statements, the WHERE clause is what limits the effect to specific rows — omitting it applies the change to every row in the table, which is one of the most damaging mistakes possible in SQL.
Examples
A full CRUD cycle against an employees table
INSERT INTO employees (id, name, department, salary)
VALUES (1, 'Rohit Sharma', 'Sales', 55000);
SELECT id, name, salary FROM employees WHERE department = 'Sales';
UPDATE employees SET salary = 60000 WHERE id = 1;
DELETE FROM employees WHERE id = 1;
INSERT adds 1 row. SELECT returns 1 row: (1, 'Rohit Sharma', 55000). UPDATE changes 1 row. DELETE removes 1 row.
Inserting multiple rows in a single statement
INSERT INTO products (id, name, price) VALUES
(1, 'Notebook', 4.50),
(2, 'Pen', 1.20),
(3, 'Stapler', 6.75);
Inserts 3 rows in one statement.
Common Mistakes
- Running UPDATE or DELETE without a WHERE clause, which affects every single row in the table — always test the matching condition with a SELECT first.
- Relying on positional VALUES without naming columns, which silently inserts data into the wrong columns if the table structure changes.
- Forgetting that UPDATE and DELETE are not automatically reversible outside a transaction — without a surrounding transaction and a chance to ROLLBACK, the change is permanent once committed.
- Using SELECT * in application code, which can break the application when new columns are added to the table later.
Exercise
Insert three employees: Asha earning 50000, Ravi earning 40000 and Zoya earning 60000. Raise Ravi's salary to 45000. Delete Zoya. Then list the names and salaries of the remaining employees, ordered by name.
Expected output: name | salary, Asha | 50000, Ravi | 45000 (one per line)
CREATE TABLE employees (id INTEGER PRIMARY KEY, name TEXT, salary INTEGER);
-- TODO: insert Asha (1), Ravi (2) and Zoya (3)
-- TODO: set Ravi's salary to 45000
-- TODO: delete Zoya
-- TODO: name and salary of everyone, ordered by name
Show hints
- Several rows can be inserted in one statement, separated by commas after
VALUES. - Both
UPDATEandDELETEneed aWHEREclause, or they affect every row.
Show solution
CREATE TABLE employees (id INTEGER PRIMARY KEY, name TEXT, salary INTEGER);
INSERT INTO employees (id, name, salary) VALUES
(1, 'Asha', 50000),
(2, 'Ravi', 40000),
(3, 'Zoya', 60000);
UPDATE employees SET salary = 45000 WHERE name = 'Ravi';
DELETE FROM employees WHERE name = 'Zoya';
SELECT name, salary FROM employees ORDER BY name;
Quick quiz
1. What does DELETE FROM orders; do?
- A. Deletes the orders table
- B. Deletes the first row
- C. Deletes every row in the table
- D. Nothing, without a WHERE clause
Show answer
C. The table itself remains. DROP TABLE removes the table.
2. Which statement changes existing rows?
- A. UPDATE
- B. INSERT
- C. ALTER
- D. SELECT
Show answer
A. ALTER changes the structure of a table, not its rows.
3. Why is SELECT * discouraged in application code?
- A. It is invalid SQL
- B. It returns only one row
- C. It cannot be used with WHERE
- D. It fetches columns that are not needed and breaks when the table's columns change
Show answer
D. Naming the columns keeps the query stable and transfers less data.
Interview questions
What is the difference between DELETE, TRUNCATE and DROP?
Show answer
DELETE removes the rows that match a WHERE clause, one by one; it can be rolled back and fires triggers. TRUNCATE removes all rows of a table at once, much faster, and typically resets identity counters; it cannot filter rows. DROP TABLE removes the table itself, its structure as well as its data.
How do you protect yourself from a wrong UPDATE or DELETE?
Show answer
Run the same WHERE clause in a SELECT first to see exactly which rows will be affected. Execute the change inside a transaction, check the number of rows reported, and commit only if it is what was expected; otherwise roll back. On important systems, take a backup first and use an account whose permissions are limited to what the task needs.
Key Points to Remember
- CRUD maps directly to INSERT (create), SELECT (read), UPDATE (update), and DELETE (delete).
- Explicitly naming columns in INSERT and SELECT makes statements safer and more readable.
- The WHERE clause in UPDATE and DELETE is what limits changes to specific rows — omitting it affects the entire table.
Practice in the playground
Write and run your own code for this topic, or start from the exercise or an example. Runs your SQL on SQLite in your browser. Standard SQL works; a few features that exist only in PostgreSQL or MySQL do not. Your code for each topic is kept in this browser.
Run the examples
Change an input, predict the result, then compare it with the output. Explain why the result changes.
These examples need a project environment on your computer, so they can be copied but not run here. Codelab runs Python, FastAPI backends with SQLite, plain Java 17 and HTML/CSS/JavaScript.
Written by
WebNest Studio Engineering Team
Practical lessons from developers working with Java, Python, React, databases and production systems.
Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering