Chapter 15: Advanced Data Manipulation

This is where we go beyond just reading and writing data and start transforming it like real data ninjas!

In real life, raw data from the database is often ugly or incomplete:

  • Prices without currency symbols
  • Names split into first/last
  • Dates in wrong format
  • Categories that need to be grouped as “Cheap / Medium / Expensive”

Today we’re going to learn the most useful advanced manipulation tools:

  • CASE statement — like if-else logic inside SQL
  • String functions — CONCAT, SUBSTRING, REPLACE, UPPER/LOWER, TRIM, etc.
  • Date functions — DATE_FORMAT, DATEDIFF, DATE_ADD, NOW(), etc.
  • Mathematical functions — ROUND, CEIL, FLOOR, ABS, POWER, etc.

We’ll use our bookshop database with the books and authors tables. Let’s first add a few useful columns to make examples more realistic:

SQL

(You can imagine we’ve filled these with data.)

1. CASE Statement – If-Then-Else Logic Inside SQL

CASE is SQL’s way of doing conditional logic — exactly like if-else in programming.

Basic Syntax:

SQL

Example 1 – Categorize books by price

SQL

Result example:

title price price_category
The Blue Umbrella 180.00 Budget Friendly
Wise and Otherwise 199.00 Budget Friendly
The Room on the Roof 250.00 Mid-Range
The Immortals of Meluha 349.00 Mid-Range
The Palace of Illusions 450.00 Premium

Example 2 – Give discount labels based on stock

SQL

Pro Tip: You can also use searched CASE (no expression before CASE):

SQL

2. String Functions – Manipulating Text Like Magic

These are super common when cleaning or formatting names, descriptions, etc.

Function What it does Example
CONCAT() Join strings CONCAT(first_name, ‘ ‘, last_name)
CONCAT_WS() Join with separator (ignores NULLs) CONCAT_WS(‘ ‘, first_name, last_name)
UPPER() / LOWER() Change case UPPER(title)
TRIM() / LTRIM() / RTRIM() Remove spaces TRIM(‘ Hello ‘) → ‘Hello’
SUBSTRING() / SUBSTR() Extract part of string SUBSTRING(title, 1, 10)
LENGTH() / CHAR_LENGTH() Length of string LENGTH(title)
REPLACE() Replace substring REPLACE(title, ‘The’, ‘A’)
LEFT() / RIGHT() Take left/right characters LEFT(title, 5)
LOCATE() / POSITION() Find position of substring LOCATE(‘The’, title)

Example 1 – Create full name and clean title

SQL

Example 2 – Check if title starts with ‘The’

SQL

3. Date Functions – Working with Dates and Times

Dates are tricky — but these functions make them easy.

Function What it does Example
NOW() / CURRENT_TIMESTAMP Current date + time NOW()
CURDATE() / CURRENT_DATE Current date only CURDATE()
DATE_FORMAT() Format date as string DATE_FORMAT(published_date, ‘%d-%m-%Y’)
DATEDIFF() Days between two dates DATEDIFF(CURDATE(), published_date)
DATE_ADD() / DATE_SUB() Add/subtract days/months/years DATE_ADD(published_date, INTERVAL 1 YEAR)
YEAR() / MONTH() / DAY() Extract part of date YEAR(published_date)
DAYNAME() / MONTHNAME() Day or month name DAYNAME(published_date)

Example 1 – Show how old each book is

SQL

Example 2 – Format dates nicely + add one year

SQL

4. Mathematical Functions – Number Crunching

Function What it does Example
ROUND() Round to n decimals ROUND(price * 1.18, 2)
CEIL() / FLOOR() Round up / down CEIL(99.1) → 100, FLOOR(99.9) → 99
ABS() Absolute value ABS(-50) → 50
POWER() / POW() Raise to power POWER(2, 3) → 8
SQRT() Square root SQRT(16) → 4
MOD() Remainder MOD(10, 3) → 1
RAND() Random number (0–1) RAND()

Example – Apply GST (18%) and round prices

SQL

Full Realistic Report – Combining Everything

SQL

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *