๐Ÿ“˜ CodingMarble Learn

SQL Single-Row Functions: Maths, Text and Date

A single-row function takes one value from a row and returns one value, so a column of n rows gives n answers. Maths functions: POWER, ROUND and MOD. Text (string) functions: UPPER, LOWER, LENGTH, LEFT, RIGHT, SUBSTR, INSTR and TRIM. Date functions: NOW, DATE, MONTH, MONTHNAME, YEAR, DAY and DAYNAME. They are used inside SELECT, WHERE and ORDER BY.

๐ŸŽฌ Step-by-step story

  1. A single-row function is a machine: one value in, one answer out.
  2. Maths functions: POWER, ROUND and MOD.
  3. Text functions change or measure text: UPPER, SUBSTR, INSTR, TRIM and more.
  4. Date functions pull out parts of a date: MONTHNAME, YEAR, DAYNAME.
  5. Put a function on a column: it runs once for every row.
  6. Your turn: guess a function's output, then press it.

Tip: drag the 3D scene to turn it. Use two fingers to zoom.

๐Ÿค” Common doubts, cleared

What does 'single-row' mean?

The function looks at one row at a time and returns one answer for it. It never mixes values from different rows.

Why is ROUND(15.678, -1) equal to 20?

-1 means round to the nearest ten. 15.678 is closer to 20 than to 10, so the answer is 20.

Does SQL count the first letter as 0 or 1?

As 1. SUBSTR('Informatics', 1, 4) is 'Info'.

What is the difference between SUBSTR and INSTR?

SUBSTR gives you a piece of the text. INSTR tells you the number of the position where a piece is found.

Why does INSTR sometimes give 0?

0 means the piece was not found in the text.

What is the difference between DAY() and DAYNAME()?

DAY gives the date number in the month (1 to 31). DAYNAME gives the weekday word (Monday, Tuesday โ€ฆ).

Why did I get 5 answers from one function?

Because it was used on a column of 5 rows. A single-row function runs once for every row.

What is a single-row function?

A function is a ready-made command that takes values (called arguments) and returns a result. A single-row (scalar) function works on one row at a time and gives one result per row. So if a table has 10 rows, SELECT UPPER(Name) FROM student; gives 10 answers.

(Aggregate functions like SUM work on many rows and give one answer; they are in the next lesson.) You can try any function without a table: SELECT POWER(2, 3);

Maths functions: POWER, ROUND, MOD

FunctionMeaningExample โ†’ output
POWER(x, y) or POWx raised to yPOWER(2, 3) โ†’ 8
ROUND(x, d)round x to d decimal placesROUND(15.678, 2) โ†’ 15.68
ROUND(x)round to a whole numberROUND(15.678) โ†’ 16
ROUND(x, -1)round to nearest 10ROUND(15.678, -1) โ†’ 20
MOD(a, b)remainder of a รท bMOD(17, 5) โ†’ 2

ROUND looks at the next digit: 5 or more rounds up. A negative d rounds to the left of the decimal point (-1 โ†’ tens, -2 โ†’ hundreds).

Text functions: UPPER, LOWER, LENGTH, LEFT, RIGHT, SUBSTR, INSTR, TRIM

FunctionMeaningExample โ†’ output
UPPER(s) / UCASEcapital lettersUPPER('asha') โ†’ ASHA
LOWER(s) / LCASEsmall lettersLOWER('INDIA') โ†’ india
LENGTH(s)number of characters (spaces count)LENGTH('Informatics') โ†’ 11
LEFT(s, n)first n charactersLEFT('Informatics', 4) โ†’ Info
RIGHT(s, n)last n charactersRIGHT('Informatics', 3) โ†’ ics
SUBSTR(s, start, n) / MIDn characters from position startSUBSTR('Informatics', 3, 4) โ†’ form
INSTR(s, sub)position where sub first starts (0 if not found)INSTR('Informatics', 'for') โ†’ 3
TRIM(s)remove spaces at both endsTRIM(' IP ') โ†’ 'IP'
LTRIM(s) / RTRIM(s)remove spaces on left / right onlyLTRIM(' IP ') โ†’ 'IP '

Positions start at 1. SUBSTR without n takes everything to the end: SUBSTR('Informatics', 6) โ†’ 'matics'. A negative start counts from the end: SUBSTR('Informatics', -3) โ†’ 'ics'.

Date functions: NOW, DATE, MONTH, MONTHNAME, YEAR, DAY, DAYNAME

MySQL writes dates as 'YYYY-MM-DD'.

FunctionExample โ†’ output
NOW()current date and time, e.g. 2026-09-30 10:15:00
DATE(dt)DATE('2026-09-30 10:15:00') โ†’ 2026-09-30
MONTH(d)MONTH('2026-09-30') โ†’ 9
MONTHNAME(d)MONTHNAME('2026-09-30') โ†’ September
YEAR(d)YEAR('2026-09-30') โ†’ 2026
DAY(d)DAY('2026-09-30') โ†’ 30
DAYNAME(d)DAYNAME('2026-09-30') โ†’ Wednesday

NOW() changes every time you run it, so board questions usually give a fixed date.

Using functions on a table

SELECT Name, UPPER(Name), LENGTH(Name) FROM student;
SELECT Name, ROUND(Fee * 1.18, 2) AS FeeWithGST FROM student;
SELECT * FROM student WHERE MONTHNAME(DOB) = 'August';
SELECT * FROM student WHERE MOD(Roll, 2) = 0;      -- even roll numbers
SELECT Name FROM student ORDER BY LENGTH(Name);

AS gives the output column a nicer name (an alias).

Try it: your name and birthday

On paper, write your full name and date of birth. Predict: LENGTH(name), LEFT(name, 3), INSTR(name, 'a'), DAYNAME(dob), MONTHNAME(dob). Then run each in MySQL or an online SQL playground, e.g. SELECT DAYNAME('2009-08-15');. Count how many predictions were right. Then test more in step 6 of the 3D.

Key formulas and definitions

Worked examples

1. Find the output: SELECT POWER(5, 2), MOD(47, 10), ROUND(7.456, 1);

25, 7, 7.5.

2. Find the output: SELECT ROUND(2468.5), ROUND(2468.5, -2), ROUND(2468.537, 2);

2469 (0.5 rounds up), 2500 (nearest hundred), 2468.54.

3. Find the output: SELECT SUBSTR('CodingMarble', 7, 6), INSTR('CodingMarble', 'Mar'), LENGTH('CodingMarble');

'Marble' (starts at position 7, takes 6), 7, 12.

4. Find the output: SELECT LEFT('Informatics Practices', 11), RIGHT('Informatics Practices', 9), UPPER(LEFT('practices', 1));

'Informatics', 'Practices', 'P'.

5. For the date '2026-01-26', find MONTH, MONTHNAME, DAY, YEAR and DAYNAME.

1, January, 26, 2026, Monday.

6. Write a query to show names of students whose name has 5 or more letters, in capital letters.

SELECT UPPER(Name) FROM student WHERE LENGTH(Name) >= 5;

7. What is the output of SELECT LENGTH(TRIM(' Hi IP '));?

TRIM gives 'Hi IP' (inside space stays). LENGTH = 5.

Common mistakes

Practice quiz

1. MOD(20, 6) returns:
2. SUBSTR('Python', 2, 3) returns:
3. INSTR('Delhi', 'z') returns:
4. Which gives the name of the day, like 'Monday'?
5. ROUND(349.5, -2) returns:

Practice: answer these yourself

Type or choose your answer, then press Check. Use a hint if you are stuck; the full solution appears after you answer.

Frequently asked questions

What are single-row functions in SQL?

Functions that work on one row at a time and return one value per row, such as ROUND, UPPER and MONTHNAME.

What is the difference between SUBSTR and INSTR in MySQL?

SUBSTR(s, start, n) returns n characters from position start; INSTR(s, sub) returns the position where sub first appears, or 0.

What does ROUND with a negative number do?

It rounds to the left of the decimal point: -1 to the nearest ten, -2 to the nearest hundred.

Where this is taught

CBSE (India)Class 12Database Query using SQL

Learn first

Learn next

Related lessons

All Informatics Practices lessons