ЁЯТ╗
тМия╕П
ЁЯЦ▒я╕П
ЁЯЦея╕П
ЁЯТ╛
тЖР Back to Dashboard
Font Size:

1. Introduction

MySQL ships with a large library of built-in functions that operate on values while a query is being executed. These functions allow a single SELECT statement to compute, transform, and format data without any additional programming. For the Class 12 examination the syllabus groups them into four families: mathematical (numeric) functions, string functions, date and time functions, and aggregate functions. Understanding what each function does, and being able to predict the output of expressions that combine them, is a reliable source of marks in both theory and practicals.

Functions are used inside a SELECT clause, a WHERE clause, or even inside other functions. Most functions accept one or more arguments and return a single value per call. Single-row functions transform each value independently, so they can be applied column-wise to produce one result per row. Aggregate functions, by contrast, collapse many rows into one summary value. Mixing the two families carelessly, such as combining a single-row column with an aggregate without grouping, is a classic source of errors.

This chapter covers each function family in turn. For every function we give its exact syntax, a working example, and the expected output, because output-prediction questions are the most common way these functions are tested. The aggregate functions were introduced in the queries chapter; here they are summarised and extended so that the full toolkit is available in one place.

2. Numeric (Mathematical) Functions

Numeric functions accept numeric values and return numeric results.

SELECT ROUND(15.678, 2);
SELECT TRUNCATE(15.678, 2);
SELECT POWER(2, 3);
SELECT SQRT(81);
SELECT MOD(17, 5);
SELECT ABS(-45);
SELECT CEIL(4.2), FLOOR(4.8);
SELECT GREATEST(10, 25, 18), LEAST(10, 25, 18);

3. String Functions

String functions manipulate character data.

3.1 Case Conversion

SELECT UCASE('hello'), UPPER('hello');
SELECT LCASE('HELLO'), LOWER('HELLO');

UCASE (also UPPER) converts a string to uppercase, and LCASE (also LOWER) converts it to lowercase. UCASE('hello') returns HELLO.

3.2 Character Count and Substrings

SELECT LENGTH('hello');
SELECT MID('Informatic', 3, 4);
SELECT SUBSTR('Informatic', 3, 4);
SELECT LEFT('Informatic', 3);
SELECT RIGHT('Informatic', 3);
SELECT CONCAT('Info', 'matics');
SELECT INSTR('Informatic', 'mat');

3.4 Trimming and Padding

SELECT TRIM('  hello  ');
SELECT LTRIM('  hello');
SELECT RTRIM('hello  ');
SELECT LPAD('123', 5, '0');
SELECT RPAD('123', 5, '*');

4. Date and Time Functions

Date functions work on DATE and DATETIME values.

SELECT NOW();
SELECT CURDATE();
SELECT DATE('2024-08-15 10:30:00');
SELECT YEAR('2024-08-15');
SELECT MONTH('2024-08-15');
SELECT DAY('2024-08-15');
SELECT DAYNAME('2024-08-15');
SELECT MONTHNAME('2024-08-15');
SELECT DATEDIFF('2024-08-20', '2024-08-15');

5. Aggregate Functions

Aggregate functions summarise an entire column into one value.

SELECT COUNT(*) FROM student;
SELECT SUM(marks), AVG(marks) FROM student;
SELECT MAX(marks), MIN(marks) FROM student;

Aggregates combine naturally with GROUP BY:

SELECT city, AVG(marks) FROM student GROUP BY city;
SELECT city, COUNT(*) FROM student GROUP BY city HAVING COUNT(*) > 2;

6. Nested Functions

Functions can be nested inside one another, and the inner function is evaluated first:

SELECT LENGTH(UCASE('hello'));
SELECT ROUND(SQRT(50), 1);
SELECT DAY(LAST_DAY('2024-02-10'));

LENGTH(UCASE('hello')) first converts 'hello' to 'HELLO' and then counts 5 characters, returning 5. Nested expressions of this type are a favourite in output-prediction questions.

Quick Revision Tables

Table 1: Numeric and String Functions

Function Example Result
ROUND(15.678, 2) Round to 2 places 15.68
TRUNCATE(15.678, 2) Cut to 2 places 15.67
MOD(17, 5) Remainder 2
POWER(2, 3) 2 raised to 3 8
UCASE('abc') Uppercase ABC
LCASE('ABC') Lowercase abc
MID('Informatic', 3, 4) Substring form
CONCAT('Info', 'matics') Join strings Informatics
INSTR('Informatic', 'mat') First position 7

Table 2: Date and Aggregate Functions

Function Example Result
YEAR('2024-08-15') Year number 2024
MONTH('2024-08-15') Month number 8
DAY('2024-08-15') Day of month 15
DAYNAME('2024-08-15') Weekday name Thursday
DATEDIFF(d1, d2) Days between d1 minus d2
COUNT(*) Row count Total rows
SUM(col) Total Sum of values
AVG(col) Mean Average value
MAX(col) / MIN(col) Extremes Largest / smallest

Mind Map

graph TD A["MySQL Functions"] --> B["Numeric Functions"] A --> C["String Functions"] A --> D["Date Functions"] A --> E["Aggregate Functions"] B --> B1["ROUND, TRUNCATE, MOD"] B --> B2["POWER, SQRT, ABS"] B --> B3["CEIL, FLOOR, GREATEST, LEAST"] C --> C1["UCASE, LCASE, LENGTH"] C --> C2["MID, SUBSTR, LEFT, RIGHT"] C --> C3["CONCAT, INSTR"] C --> C4["TRIM, LTRIM, RTRIM, LPAD, RPAD"] D --> D1["NOW, CURDATE, DATE"] D --> D2["YEAR, MONTH, DAY"] D --> D3["DAYNAME, MONTHNAME, DATEDIFF"] E --> E1["COUNT, SUM, AVG"] E --> E2["MAX, MIN"]

Important Diagrams (SVG)

Diagram 1: Classification of MySQL Functions

Classification of MySQL Functions MySQL Functions Single-row and aggregate Numeric ROUND, TRUNCATE POWER, SQRT, MOD ABS, CEIL, FLOOR GREATEST, LEAST String UCASE, LCASE, LENGTH MID, SUBSTR, LEFT CONCAT, INSTR, TRIM LPAD, RPAD Date NOW, CURDATE, DATE YEAR, MONTH, DAY DAYNAME, MONTHNAME DATEDIFF Aggregate COUNT, SUM AVG, MAX, MIN Grouped results Key Difference Single-row functions return one result per row Aggregate functions return one result for the whole column or group Golden Rule Count single-row functions row by row, but count aggregates column by column

Diagram 2: Substring Extraction with MID

How MID(str, pos, len) Works I n f o r m a t i c string: Informatic pos 1 pos 3 MID('Informatic', 3, 4) start at pos 3, take 4 chars Result form Golden Rule String positions in MySQL are 1-based: MID and SUBSTR count characters from position 1

Common Mistakes

  1. Using TRUNCATE when rounding is intended: TRUNCATE(15.678, 2) gives 15.67 while ROUND(15.678, 2) gives 15.68.
  2. Confusing MID with LEFT; MID starts at a given position, while LEFT always starts at the first character.
  3. Forgetting that MySQL string positions are 1-based, so MID('abc', 1, 2) returns 'ab' not 'bc'.
  4. Calling LENGTH on numeric data and forgetting that it first converts the number to a string.
  5. Using MONTH where the month name is wanted; MONTH(date) gives a number and MONTHNAME(date) gives the name.
  6. Mixing single-row functions with aggregates in a SELECT without a GROUP BY, causing an "Invalid use of group function" error.
  7. Expecting DATEDIFF(d1, d2) to be symmetric; it returns d1 minus d2 and can be negative if d1 is earlier.
  8. Writing CONCAT('Info', 'matics') with a + instead of a comma, since MySQL does not use + to join strings.
  9. Applying aggregate functions to text columns; functions like SUM and AVG require numeric data.

Exam Tips

  1. Memorise the exact output of the most common functions, since output-prediction is a guaranteed question type.
  2. Learn the pairs: ROUND/TRUNCATE, CEIL/FLOOR, UCASE/LCASE, LPAD/RPAD, LEFT/RIGHT, MID/SUBSTR.
  3. Remember that MOD(a, b) and a % b are equivalent in MySQL.
  4. For date questions, remember that YEAR, MONTH, and DAY take a date as argument and return a number.
  5. Practise nested function questions, evaluating from the innermost function outwards.
  6. Know that all aggregates except COUNT(*) ignore NULL values, which affects outputs when data is incomplete.
  7. In practical exams, test each function with a simple SELECT before embedding it into a larger query.

Conclusion

MySQL functions give a single SELECT statement the ability to compute, transform, format, and summarise data. Numeric functions such as ROUND, TRUNCATE, MOD, POWER, and SQRT handle mathematical work; string functions like UCASE, LCASE, MID, CONCAT, INSTR, and the padding family manipulate text; and the date functions YEAR, MONTH, DAY, DAYNAME, MONTHNAME, and DATEDIFF interpret and compare dates. Aggregate functions, including COUNT, SUM, AVG, MAX, and MIN, collapse whole columns into summary values and work with GROUP BY to produce per-group statistics. Because functions can be nested and combined freely, they make MySQL a complete data-processing language, and they are examined heavily through output-prediction and query-writing questions throughout the Class 12 curriculum.