1. Introduction to SQL Functions

SQL functions are built-in operations that take input value(s) and return a result. They are classified as:

Category Operates on Returns Examples
Single-Row Functions One row at a time One result per row UCASE(), LENGTH(), MID(), ROUND()
Aggregate Functions Group of rows One result per group MAX(), MIN(), AVG(), SUM(), COUNT()

Single-row functions are further divided into: String (Text) functions, Math (Numeric) functions, and Date functions. This note covers all String Functions from the CBSE IP syllabus.

We will use this sample table throughout all examples:

Table: STUDENT

RollNo Name City Marks
1AryanDelhi85
2PriyaMumbai92
3RohanDelhi78
4SnehaPune95

2. Case Conversion Functions — UCASE() / UPPER() and LCASE() / LOWER()

Function Syntax Description
UCASE() / UPPER() UCASE(string) Converts all characters to UPPERCASE
LCASE() / LOWER() LCASE(string) Converts all characters to lowercase
SELECT UCASE(Name), LCASE(City) FROM STUDENT;
UCASE(Name)LCASE(City)
ARYANdelhi
PRIYAmumbai
ROHANdelhi
SNEHApune
-- Direct value (no table needed)
SELECT UCASE('hello world');   -- HELLO WORLD
SELECT LCASE('PYTHON SQL');    -- python sql

Note: UCASE() and UPPER() are synonyms — they do the same thing. Same for LCASE() and LOWER(). Use either in board answers.

3. LENGTH() — String Length

LENGTH(string) returns the number of characters in a string, including spaces.

SELECT Name, LENGTH(Name) FROM STUDENT;
NameLENGTH(Name)
Aryan5
Priya5
Rohan5
Sneha5
SELECT LENGTH('Database');    -- 8
SELECT LENGTH('SQL');          -- 3
SELECT LENGTH('  hi  ');       -- 6 (spaces are counted)

4. MID() / SUBSTRING() / SUBSTR() — Extract Substring

These three functions are synonyms — they extract a portion of a string.

MID(string, position, length)

  • position: Starting position — 1-based (first character = position 1, not 0).
  • length: Number of characters to extract.
SELECT MID('Informatics', 1, 6);    -- Inform
SELECT MID('Informatics', 5, 3);    -- rma
SELECT SUBSTR('Database', 2, 4);     -- atab
SELECT SUBSTRING('Computer', 3, 5);  -- mpute

Verification — MID('Informatics', pos, len):

I  n  f  o  r  m  a  t  i  c  s
1  2  3  4  5  6  7  8  9  10 11
MID('Informatics', 1, 6) → positions 1–6 → I,n,f,o,r,m → Inform
MID('Informatics', 5, 3) → positions 5–7 → r,m,a → rma
-- On table column
SELECT Name, MID(Name, 1, 3) AS ShortName FROM STUDENT;
NameShortName
AryanAry
PriyaPri
RohanRoh
SnehaSne

5. LEFT() and RIGHT() — Extract from Ends

Function Syntax Returns Example Output
LEFT() LEFT(str, n) First n characters from the left LEFT('Computer', 3) Com
RIGHT() RIGHT(str, n) Last n characters from the right RIGHT('Computer', 4) uter
SELECT LEFT('Informatics', 4);    -- Info
SELECT RIGHT('Informatics', 5);   -- atics
SELECT LEFT(City, 3) FROM STUDENT;
-- Del, Mum, Del, Pun

Relationship: LEFT(str, n) = MID(str, 1, n) and RIGHT(str, n) = MID(str, LENGTH(str)-n+1, n) — they are shortcuts.

6. INSTR() — Find Position of a Substring

INSTR(string, substring) returns the 1-based position of the first occurrence of substring within string. Returns 0 if not found.

SELECT INSTR('Computer Science', 'Science');  -- 10
SELECT INSTR('hello', 'world');            -- 0 (not found)
SELECT INSTR('Informatics', 'mat');       -- 6

Verification — INSTR('Computer Science', 'Science'):

C  o  m  p  u  t  e  r     S  c  i  e  n  c  e
1  2  3  4  5  6  7  8  9  10 11 12 13 14 15 16
'Science' starts at position 10 → INSTR = 10

7. LTRIM(), RTRIM() and TRIM() — Remove Whitespace

Function Removes Example Output
LTRIM() Leading spaces (left side) LTRIM('  hello  ') 'hello  '
RTRIM() Trailing spaces (right side) RTRIM('  hello  ') '  hello'
TRIM() Both leading and trailing spaces TRIM('  hello  ') 'hello'
-- LENGTH before and after trim
SELECT LENGTH('  hi  ');             -- 6
SELECT LENGTH(TRIM('  hi  '));       -- 2
SELECT LENGTH(LTRIM('  hi  '));      -- 4 (only left spaces removed)
SELECT LENGTH(RTRIM('  hi  '));      -- 4 (only right spaces removed)

Important use case: TRIM functions are commonly used to clean up data — removing accidental leading/trailing spaces in names, emails, or other text fields before comparison or display.

8. Complete String Functions Summary

Function Syntax Purpose Quick Example → Output
UCASE() / UPPER()UCASE(str)UppercaseUCASE('hello')HELLO
LCASE() / LOWER()LCASE(str)LowercaseLCASE('HELLO')hello
LENGTH()LENGTH(str)Count charactersLENGTH('SQL')3
MID() / SUBSTR() / SUBSTRING()MID(str, pos, len)Extract substring (1-based)MID('Python', 2, 4)ytho
LEFT()LEFT(str, n)First n charactersLEFT('Python', 3)Pyt
RIGHT()RIGHT(str, n)Last n charactersRIGHT('Python', 3)hon
INSTR()INSTR(str, sub)Position of substring (1-based, 0 if absent)INSTR('SQL', 'Q')2
LTRIM()LTRIM(str)Remove leading spacesLTRIM(' hi')'hi'
RTRIM()RTRIM(str)Remove trailing spacesRTRIM('hi ')'hi'
TRIM()TRIM(str)Remove both leading and trailing spacesTRIM(' hi ')'hi'