DEV Community

Cover image for ๐Ÿง  Database for Software Developers โ€“ Data Transformation, Grouping & Aggregation (Part 1)
Tahsin Abrar
Tahsin Abrar

Posted on • Edited on

๐Ÿง  Database for Software Developers โ€“ Data Transformation, Grouping & Aggregation (Part 1)

Welcome back to the Database for Software Developers series. In this post, weโ€™re diving into data transformation, grouping, and aggregationโ€”some of the most powerful tools you can use to manipulate and make sense of your data.

Whether you're building dashboards, cleaning up records, or generating insightful reports, these SQL functions will be your best friends. Let's go step-by

step and see how they work in real-world scenarios.


๐Ÿ“Œ Real-World Scenarios

Here are a few examples of what weโ€™ll achieve in this guide:

  • โœ… Concatenate multiple fields to show them together
  • ๐Ÿ—‘๏ธ Delete abandoned carts older than 3 days
  • ๐Ÿ“ง Filter users by email domain (e.g., @mycompany.com)
  • ๐Ÿ•’ Calculate how long applications took to get approved
  • ๐Ÿ”ข Format large numbers with commas for readability

Letโ€™s explore how to implement these using built-in SQL functions.


๐Ÿงฐ What Built-In SQL Functions Can Do

SQL is more than just SELECT and WHERE! Here's what built-in functions are capable of:

  • ๐ŸŽจ Data formatting
  • โœ‚๏ธ String manipulation
  • ๐Ÿงฎ Numeric calculations
  • ๐Ÿ“† Date and time processing
  • ๐Ÿ”’ Encryption/decryption
  • ๐Ÿ” Comparisons and control flows
  • ๐Ÿ”„ Type conversions
  • ๐Ÿ“ฆ Aggregations

๐Ÿ”ค String Manipulation Functions

These are super handy when cleaning or formatting text data:

Function Purpose
CONCAT() Combines multiple strings
SUBSTRING() / SUBSTR() Extracts part of a string
INSTR() Finds the position of a substring
LENGTH() / CHAR_LENGTH() Finds string length
REPLACE() Replaces part of a string
LOWER() / UPPER() Changes case
LEFT() / RIGHT() Gets a substring from the left or right
LPAD() / RPAD() Pads strings
TRIM(), LTRIM(), RTRIM() Removes whitespace

โœจ Examples:

1. Extract email domain:

SELECT id, name, SUBSTRING(email, INSTR(email, '@') + 1) AS email_domain 
FROM users;
Enter fullscreen mode Exit fullscreen mode

2. Generate SEO-friendly product slugs:

SELECT title, 
       LEFT(description, 30) AS excerpt, 
       LOWER(REPLACE(title, ' ', '-')) AS url_slug 
FROM products;
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”ข Numeric Calculations

These functions are useful for rounding numbers, calculating totals, and more.

Function Purpose
ROUND(), FLOOR(), CEIL() Rounding strategies
MOD() Returns remainder
COUNT(), SUM(), AVG() Aggregates
MAX(), MIN() Finds limits
POWER(), SQRT() Math ops

โœจ Examples:

1. Count total admins:

SELECT COUNT(*) FROM users WHERE role = 'admin';
Enter fullscreen mode Exit fullscreen mode

2. Update every 3rd user to 'teacher':

UPDATE users
SET role = 'teacher'
WHERE MOD(id, 3) = 0;
Enter fullscreen mode Exit fullscreen mode

๐Ÿ—“๏ธ Date and Time Functions

Perfect for filtering or calculating time differences.

Function Description
CURDATE() / NOW() Current date/time
DATEDIFF() Days between two dates
DATE_ADD() / DATE_SUB() Add/subtract time
DATE_FORMAT() Format date strings

โœจ Examples:

1. How many days since a given date?

SELECT DATEDIFF(CURDATE(), '2014-12-16');
Enter fullscreen mode Exit fullscreen mode

2. Find upcoming payments:

SELECT check_number, amount 
FROM payments 
WHERE payment_date > CURDATE();
Enter fullscreen mode Exit fullscreen mode

๐Ÿ” Control Flow Functions

Dynamic logic based on conditionsโ€”great for labels or categorizations.

Function Description
IF() Simple if/else logic
CASE Multiple conditions
COALESCE() First non-null value
IFNULL() Fallback value if NULL

โœจ Examples:

1. Categorize customers:

SELECT id, name,
  CASE
    WHEN credit_limit > 1000 THEN 'Premium'
    WHEN credit_limit > 500 THEN 'Standard'
    ELSE 'New'
  END AS customer_type
FROM customers;
Enter fullscreen mode Exit fullscreen mode

2. Discount eligibility:

SELECT product_id, 
       IF(stock_qty > 50, 'Eligible', 'Not Eligible') AS discount
FROM products;
Enter fullscreen mode Exit fullscreen mode

๐Ÿ” Cryptography & Hashing

Security is key when handling sensitive data.

Function Use
MD5(), SHA1(), SHA2() Hashing
AES_ENCRYPT(), AES_DECRYPT() Encryption

โœจ Examples:

1. Storing hashed password:

INSERT INTO users(username, password)
VALUES('goodboy', MD5('the-secret-password'));
Enter fullscreen mode Exit fullscreen mode

2. Signing document version:

UPDATE documents
SET content_hash = SHA1(CONCAT(content, username, salt));
Enter fullscreen mode Exit fullscreen mode

3. Encrypt/decrypt card info:

UPDATE customers 
SET credit_card_info = AES_ENCRYPT('the-card-number', 'encryptionKey');

SELECT customer_id, AES_DECRYPT(credit_card_info, 'encryptionKey') AS card_info 
FROM customers;
Enter fullscreen mode Exit fullscreen mode

Top comments (0)