⬅ Previous Topic
SQL ConstraintsNext Topic ⮕
SQL NULL Handling – IS NULL, COALESCE, IFNULL⬅ Previous Topic
SQL ConstraintsNext Topic ⮕
SQL NULL Handling – IS NULL, COALESCE, IFNULLFunctions in SQL act like tools — each one designed for a specific job. Some format text, others calculate averages or extract dates. Together, they make your queries smarter and your reports richer. In this tutorial, we’ll explore string, numeric, date, and aggregate functions using examples from a school database.
CREATE TABLE students (
roll_no INT,
name VARCHAR(50),
dob DATE,
marks INT,
city VARCHAR(30)
);
INSERT INTO students VALUES
(1, 'Aarav Sharma', '2008-06-12', 85, 'Delhi'),
(2, 'Diya Iyer', '2009-11-23', 92, 'Chennai'),
(3, 'Sneha Patil', '2007-05-17', 78, 'Pune'),
(4, 'Mehul Agarwal', '2008-01-03', 90, 'Delhi');
Combine name and city:
SELECT CONCAT(name, ' - ', city) AS student_info
FROM students;
student_info
-------------------------
Aarav Sharma - Delhi
Diya Iyer - Chennai
...
SELECT UPPER(name) AS upper_name, LOWER(city) AS lower_city
FROM students;
SELECT name, LENGTH(name) AS name_length
FROM students;
SELECT name, ROUND(marks / 3.0, 1) AS average_per_subject
FROM students;
SELECT name, CEIL(marks/3.0) AS ceiling_value, FLOOR(marks/3.0) AS floor_value
FROM students;
Find remainder when marks are divided by 7:
SELECT name, MOD(marks, 7) AS remainder
FROM students;
SELECT name, CURRENT_DATE AS today, NOW() AS current_datetime
FROM students;
SELECT name, YEAR(dob) AS birth_year, MONTH(dob) AS birth_month
FROM students;
Find how many days since birth:
SELECT name, DATEDIFF(CURRENT_DATE, dob) AS age_in_days
FROM students;
These work on a group of rows and return one result.
SELECT COUNT(*) AS total_students
FROM students;
SELECT SUM(marks) AS total_marks, AVG(marks) AS avg_marks
FROM students;
SELECT MIN(marks) AS lowest, MAX(marks) AS highest
FROM students;
SELECT city, AVG(marks) AS avg_city_marks
FROM students
GROUP BY city;
city | avg_city_marks
----------+----------------
Delhi | 87.5
Chennai | 92.0
Pune | 78.0
In a school dashboard, you might use:
SQL functions give you power and flexibility to analyze, format, and calculate data within queries. Whether you’re handling names, marks, dates, or groups — string, numeric, date, and aggregate functions help you turn raw data into actionable insight.
Next, we’ll explore SQL CASE expressions to apply conditional logic directly within your SELECT statements.
SELECT UPPER(SUBSTR(name, 1, 3)) FROM students WHERE roll_no = 101;
⬅ Previous Topic
SQL ConstraintsNext Topic ⮕
SQL NULL Handling – IS NULL, COALESCE, IFNULLYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.