Aral Code

Functions & Grouping Lesson 13 of 21

SQL Aliases, CASE and Useful Functions

Rename columns and tables with AS, label data with CASE WHEN (like DepEd grade descriptors), and use text, number and date functions such as UPPER, LENGTH, SUBSTR and ROUND.

This lesson collects tools that make query results clearer: aliases for readable names, CASE for if-then logic inside a query, and handy functions for text, numbers and dates.

Column aliases

SQL Friendly column names
SELECT name AS "Product",
       price AS "Price (₱)",
       stock AS "On hand"
FROM products
LIMIT 5;

Use double quotes around an alias that contains spaces or symbols. For simple names, quotes aren't needed: price AS old_price.

Table aliases

A table can get a short nickname too. This saves a lot of typing when you join tables:

SQL Short table names
SELECT p.name, p.price
FROM products AS p
WHERE p.category = 'Canned Goods';

The word AS is optional for tables: FROM products p works the same.

CASE: if-then inside SQL

CASE checks conditions in order and returns the value for the first one that's true, like if/else if/else:

SQL Stock status labels
SELECT name, stock,
  CASE
    WHEN stock = 0 THEN 'Out of stock'
    WHEN stock < 30 THEN 'Low - restock soon'
    ELSE 'OK'
  END AS status
FROM products
ORDER BY stock;
SQL DepEd grade descriptors
SELECT student_id, subject, score,
  CASE
    WHEN score >= 90 THEN 'Outstanding'
    WHEN score >= 85 THEN 'Very Satisfactory'
    WHEN score >= 80 THEN 'Satisfactory'
    WHEN score >= 75 THEN 'Fairly Satisfactory'
    ELSE 'Did Not Meet Expectations'
  END AS descriptor
FROM grades
WHERE subject = 'Math'
ORDER BY score DESC;

CASE with GROUP BY

You can group by a CASE expression to make your own categories:

SQL How many products in each price range?
SELECT
  CASE
    WHEN price < 10 THEN 'Below ₱10'
    WHEN price < 30 THEN '₱10–₱29'
    ELSE '₱30 and up'
  END AS price_range,
  COUNT(*) AS products
FROM products
GROUP BY price_range;

Text functions

FunctionExampleResult
UPPER(x) / LOWER(x)UPPER('kape')KAPE
LENGTH(x)LENGTH('Rizal')5
SUBSTR(x, start, len)SUBSTR('Pilipinas', 1, 4)Pili
TRIM(x)TRIM(' hi ')hi
REPLACE(x, a, b)REPLACE('09-17', '-', '')0917
SQL Text functions in action
SELECT UPPER(last_name) || ', ' || first_name AS formal_name,
       SUBSTR(first_name, 1, 1) || SUBSTR(last_name, 1, 1) AS initials,
       LENGTH(first_name) AS name_length
FROM students;

Note: SQL counts text positions from 1, not 0 like Python and JavaScript.

Number functions

SQL Rounding and absolute values
SELECT ROUND(88.456, 1) AS r1,
       ROUND(88.5) AS r0,
       ABS(-12) AS absolute,
       17 % 5 AS remainder;

Date functions (SQLite)

SQL Working with dates
SELECT id, order_date,
       strftime('%d', order_date) AS day,
       julianday('2026-12-01') - julianday(order_date) AS days_ago
FROM orders
LIMIT 5;

Date functions differ the most between databases. In MySQL, you'd use DAY(order_date) and DATEDIFF('2026-12-01', order_date). The MySQL course covers those.

Computing age

SQL Student ages on December 1, 2026
SELECT first_name, birthday,
  CAST((julianday('2026-12-01') - julianday(birthday)) / 365.25 AS INTEGER) AS age
FROM students;

CAST(... AS INTEGER) cuts off the decimal part, giving whole years.

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. Which keyword ends a CASE expression?
2. What does FROM products p do?
3. In SQL, what is SUBSTR('Mabini', 1, 3)?
4. When are double quotes needed for a column alias?

By Sir Reyel · Last updated 2026-12-04