Facebook Pixel

Advanced Data Manipulation

In addition to basic retrieval, SQL provides powerful Data Manipulation Language (DML) commands to insert new records, modify existing rows, and delete data safely.

Modifying Records: INSERT, UPDATE, DELETE

Let's start with a sample Customers table to see how DML commands manipulate its state:

Customers (Initial State)
IDCustomerNameCityCountry
1AlfredsBerlinGermany
2Ana TrujilloMéxico D.F.Mexico

1. INSERT INTO

The INSERT INTO statement adds brand new rows to a table. You specify the columns you are filling, followed by the values in the exact same order.

sql
INSERT INTO Customers (ID, CustomerName, City, Country)
VALUES ('3', 'Cardinal', 'Stavanger', 'Norway');
Customers (After INSERT)
IDCustomerNameCityCountry
1AlfredsBerlinGermany
2Ana TrujilloMéxico D.F.Mexico
3CardinalStavangerNorway

2. UPDATE and DELETE

The UPDATE statement modifies existing records, while DELETE completely removes them. CRITICAL RULE: You must *always* use a WHERE clause! If you omit the WHERE clause, the database will update or delete EVERY single row in your table!

sql
-- Update Ana's city
UPDATE Customers 
SET City = 'Cancun' 
WHERE ID = '2';

-- Delete Alfreds from the system
DELETE FROM Customers 
WHERE ID = '1';
Customers (After UPDATE & DELETE)
IDCustomerNameCityCountry
2Ana TrujilloCancunMexico
3CardinalStavangerNorway

Handling Missing Data (NULLs)

NULL values represent missing, unknown, or undefined data. Because NULL is mathematically "unknown", a database engine cannot evaluate it using standard comparison operators like = or !=. Asking the database WHERE City = NULL will silently fail or return zero rows.

The IS NULL Operator
To check if a column is missing data, you MUST use the special IS NULL or IS NOT NULL operators.
sql
SELECT CustomerName 
FROM Customers 
WHERE City IS NULL;

Pattern Matching (LIKE, IN, BETWEEN)

Often, you don't know the exact string you are looking for. SQL provides the LIKE operator alongside wildcards to search for patterns within text.

  • The Percent Sign (%): Represents zero, one, or multiple characters. Example: `LIKE 'a%'` finds anything starting with 'a' (apple, ant, a).
  • The Underscore (_): Represents exactly ONE single character. Example: `LIKE '_r%'` finds any string where the SECOND letter is 'r' (bread, tree, orbit).
sql
-- Find values starting with 'a' and at least 3 characters long
SELECT * 
FROM Customers 
WHERE CustomerName LIKE 'a__%';

-- Find values within a specific list
SELECT * 
FROM Customers 
WHERE Country IN ('Germany', 'France', 'UK');

-- Find values inside an inclusive numeric range
SELECT * 
FROM Products 
WHERE Price BETWEEN 10 AND 20;

Limiting Results

When dealing with millions of rows, you rarely want to return all of them. However, SQL implementations differ wildly on how to limit returned rows.

sql
/* Microsoft SQL Server Syntax */
SELECT TOP 3 * 
FROM Customers;

/* MySQL and PostgreSQL Syntax */
SELECT * 
FROM Customers 
LIMIT 3;

/* Oracle ANSI Standard Syntax */
SELECT * 
FROM Customers 
FETCH FIRST 3 ROWS ONLY;

Aggregate Functions

Aggregate functions perform mathematical calculations on an entire column of values and crush them down into a single summary value.

Products
ProductIDProductNamePrice
1Laptop1000
2Mouse50
3Keyboard150
sql
SELECT 
  COUNT(ProductID) AS TotalItems,
  MIN(Price) AS CheapestItem,
  MAX(Price) AS ExpensiveItem,
  SUM(Price) AS TotalInventoryValue,
  AVG(Price) AS AveragePrice
FROM Products;
Aggregation Result (Collapsed into 1 row)
TotalItemsCheapestItemExpensiveItemTotalInventoryValueAveragePrice
35010001200400
Placement Trap: COUNT(*) vs COUNT(column)
Interviewers love asking the difference between COUNT(*) and COUNT(column_name). COUNT(*) simply counts the total number of physical rows in the table. However, COUNT(column_name) counts the number of values in that column while IGNORING all NULL values! If a column has 10 rows but 2 are NULL, COUNT(col) returns 8.

Spot The Bug!

An intern is trying to update the city for a specific customer with ID 5, but the senior developer stopped them before they ran this query. Why?

sql
UPDATE Customers 
SET City = 'London';

Fill in the Blank

To check if a user's phone number is missing from the database, you cannot use the equals sign (=). You must use the operator.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.