Chapter 1 – Querying and SQL Functions (Class 12 IP)
Structured Query Language (SQL) is used to communicate with and manage relational databases. In CBSE Class 12
IP, the focus is on writing SQL queries to retrieve, filter, and process data from tables.
1. Important SQL Clauses
Clause Description Example
SELECT Retrieves data from one or more columns. SELECT name, age FROM Students;
FROM Specifies the table to retrieve data from. SELECT * FROM Employees;
WHERE Filters rows based on a condition. SELECT * FROM Students WHERE age > 18;
ORDER BY Sorts the result in ascending (ASC) or descending SELECT
(DESC) order.
name FROM Students ORDER BY age DESC;
GROUP BY Groups rows sharing a property for aggregate functions.
SELECT dept, COUNT(*) FROM Employees GROUP BY dept;
HAVING Filters groups after aggregation. SELECT dept, COUNT(*) FROM Employees GROUP BY dept H
DISTINCT Removes duplicate values from the result. SELECT DISTINCT city FROM Customers;
BETWEEN Selects values within a given range (inclusive). SELECT * FROM Orders WHERE amount BETWEEN 1000 AN
IN Matches any value in a list. SELECT * FROM Students WHERE class IN ('XI', 'XII');
LIKE Pattern matching with wildcards (% for multiple, _ for
SELECT
single).name FROM Students WHERE name LIKE 'A%';
2. SQL Aggregate Functions
Function Description Example
COUNT() Returns number of rows. SELECT COUNT(*) FROM Students;
SUM() Returns the total sum of a numeric column. SELECT SUM(salary) FROM Employees;
AVG() Returns the average value. SELECT AVG(marks) FROM Students;
MAX() Returns the largest value. SELECT MAX(marks) FROM Students;
MIN() Returns the smallest value. SELECT MIN(marks) FROM Students;
3. SQL String Functions
Function Description Example
LCASE() Converts text to lowercase. SELECT LCASE(name) FROM Students;
UCASE() Converts text to uppercase. SELECT UCASE(city) FROM Customers;
LENGTH() Returns length of a string. SELECT LENGTH(name) FROM Students;
LEFT(str, n) Returns first n characters. SELECT LEFT(name, 3) FROM Students;
RIGHT(str, n) Returns last n characters. SELECT RIGHT(name, 4) FROM Students;
MID(str, start, len) Extracts substring. SELECT MID(name, 2, 3) FROM Students;
CONCAT(str1, str2) Joins two strings. SELECT CONCAT(fname, ' ', lname) FROM Students;
4. SQL Date Functions
Function Description Example
NOW() Returns current date and time. SELECT NOW();
CURDATE() Returns current date. SELECT CURDATE();
CURTIME() Returns current time. SELECT CURTIME();
Function Description Example
YEAR(date) Extracts year. SELECT YEAR(dob) FROM Students;
MONTH(date) Extracts month. SELECT MONTH(dob) FROM Students;
DAY(date) Extracts day of month. SELECT DAY(dob) FROM Students;