Exam Prep
30 Important SQL Queries for CBSE Class 12 Board Exam 2026-27
Master 30 must-know SQL queries for CBSE Class 12 board exam. SELECT, WHERE, GROUP BY, HAVING, joins, aggregate functions with tables and output.
SQL carries 8 marks in the CBSE Class 12 Computer Science board exam. Every year, 2-3 questions come directly from SQL. This post covers 30 queries you must practice before your exam.
We will use these two tables throughout:
Table: STUDENT
| RollNo | Name | Class | Marks | City |
|---|---|---|---|---|
| 1 | Aman | 12A | 85 | Delhi |
| 2 | Priya | 12B | 92 | Mumbai |
| 3 | Rahul | 12A | 67 | Delhi |
| 4 | Sneha | 12B | 78 | Kolkata |
| 5 | Vikram | 12A | 91 | Chennai |
| 6 | Kavita | 12B | 54 | Mumbai |
| 7 | Arjun | 12A | 73 | Delhi |
| 8 | Meera | 12B | 88 | Chennai |
Table: TEACHER
| TID | TName | Subject | Salary | Department |
|---|---|---|---|---|
| 101 | Mr Sharma | Physics | 55000 | Science |
| 102 | Ms Gupta | Chemistry | 48000 | Science |
| 103 | Mr Verma | English | 42000 | Humanities |
| 104 | Ms Jain | Maths | 60000 | Science |
| 105 | Mr Roy | History | 38000 | Humanities |
Part 1: Basic SELECT Queries
Query 1: Select All Records
SELECT * FROM STUDENT;
This displays every row and column from the STUDENT table.
Query 2: Select Specific Columns
SELECT Name, Marks FROM STUDENT;
Name | Marks
--------|------
Aman | 85
Priya | 92
Rahul | 67
Sneha | 78
Vikram | 91
Kavita | 54
Arjun | 73
Meera | 88
Query 3: DISTINCT, Remove Duplicates
SELECT DISTINCT City FROM STUDENT;
City
--------
Delhi
Mumbai
Kolkata
Chennai
Query 4: WHERE Clause
SELECT Name, Marks FROM STUDENT WHERE Marks > 80;
Name | Marks
-------|------
Aman | 85
Priya | 92
Vikram | 91
Meera | 88
Query 5: AND / OR
SELECT * FROM STUDENT WHERE City = 'Delhi' AND Marks > 70;
RollNo | Name | Class | Marks | City
-------|-------|-------|-------|------
1 | Aman | 12A | 85 | Delhi
7 | Arjun | 12A | 73 | Delhi
Part 2: Operators, BETWEEN, IN, LIKE
Query 6: BETWEEN
SELECT Name, Marks FROM STUDENT WHERE Marks BETWEEN 70 AND 90;
Name | Marks
------|------
Aman | 85
Sneha | 78
Arjun | 73
Meera | 88
Note: BETWEEN is inclusive, it includes both 70 and 90.
Query 7: IN
SELECT * FROM STUDENT WHERE City IN ('Delhi', 'Mumbai');
This returns all students from Delhi or Mumbai. It is a shorter way to write multiple OR conditions.
Query 8: NOT IN
SELECT Name, City FROM STUDENT WHERE City NOT IN ('Delhi');
Returns students from all cities except Delhi.
Query 9: LIKE with % Wildcard
SELECT Name FROM STUDENT WHERE Name LIKE 'A%';
Name
-----
Aman
Arjun
% matches zero or more characters. A% means names starting with 'A'.
Query 10: LIKE with _ Wildcard
SELECT Name FROM STUDENT WHERE Name LIKE '____a';
Name
------
Priya
Sneha
Meera
Each _ matches exactly one character. Five characters total, ending with 'a'.
Part 3: ORDER BY
Query 11: Sort Ascending
SELECT Name, Marks FROM STUDENT ORDER BY Marks;
Name | Marks
-------|------
Kavita | 54
Rahul | 67
Arjun | 73
Sneha | 78
Aman | 85
Meera | 88
Vikram | 91
Priya | 92
Query 12: Sort Descending
SELECT Name, Marks FROM STUDENT ORDER BY Marks DESC;
Returns students sorted from highest to lowest marks.
Query 13: ORDER BY Multiple Columns
SELECT * FROM STUDENT ORDER BY City ASC, Marks DESC;
First sorts by City alphabetically, then by Marks in descending order within each city.
Part 4: Aggregate Functions
These are asked almost every year. Memorize all five: COUNT, SUM, AVG, MAX, MIN.
Query 14: COUNT
SELECT COUNT(*) FROM STUDENT;
COUNT(*)
--------
8
Query 15: SUM
SELECT SUM(Marks) FROM STUDENT;
SUM(Marks)
----------
628
Query 16: AVG
SELECT AVG(Marks) FROM STUDENT;
AVG(Marks)
----------
78.5
Query 17: MAX and MIN
SELECT MAX(Marks), MIN(Marks) FROM STUDENT;
MAX(Marks) | MIN(Marks)
-----------|----------
92 | 54
Query 18: COUNT with Condition
SELECT COUNT(*) FROM STUDENT WHERE City = 'Delhi';
COUNT(*)
--------
3
Part 5: GROUP BY and HAVING
Query 19: GROUP BY
SELECT City, COUNT(*) FROM STUDENT GROUP BY City;
City | COUNT(*)
--------|--------
Delhi | 3
Mumbai | 2
Kolkata | 1
Chennai | 2
Query 20: GROUP BY with Aggregate
SELECT City, AVG(Marks) FROM STUDENT GROUP BY City;
City | AVG(Marks)
--------|----------
Delhi | 75.00
Mumbai | 73.00
Kolkata | 78.00
Chennai | 89.50
Query 21: HAVING Clause
SELECT City, COUNT(*) FROM STUDENT GROUP BY City HAVING COUNT(*) > 1;
City | COUNT(*)
--------|--------
Delhi | 3
Mumbai | 2
Chennai | 2
Important: HAVING is used to filter groups (after GROUP BY). WHERE is used to filter rows (before GROUP BY). This distinction is a favourite board exam question.
Query 22: GROUP BY with HAVING and WHERE
SELECT Class, AVG(Marks) FROM STUDENT WHERE City != 'Kolkata' GROUP BY Class HAVING AVG(Marks) > 75;
This first filters out Kolkata students (WHERE), groups by Class (GROUP BY), and then keeps only groups with average marks above 75 (HAVING).
Part 6: NULL Handling
Query 23: IS NULL
SELECT Name FROM STUDENT WHERE Marks IS NULL;
Use IS NULL, never = NULL. This is a common mistake in exams.
Query 24: IS NOT NULL
SELECT Name FROM STUDENT WHERE Marks IS NOT NULL;
Part 7: DDL and DML Commands
Query 25: CREATE TABLE
CREATE TABLE STUDENT (
RollNo INT PRIMARY KEY,
Name VARCHAR(50) NOT NULL,
Marks INT,
City VARCHAR(30) DEFAULT 'Delhi'
);
Query 26: INSERT INTO
INSERT INTO STUDENT VALUES (9, 'Riya', '12A', 82, 'Pune');
Or insert into specific columns:
INSERT INTO STUDENT (RollNo, Name, Class) VALUES (10, 'Karan', '12B');
Query 27: UPDATE
UPDATE STUDENT SET Marks = 90 WHERE RollNo = 3;
Always use WHERE with UPDATE, or it changes every row.
Query 28: DELETE
DELETE FROM STUDENT WHERE Marks < 60;
This removes Kavita (Marks = 54) from the table.
Query 29: ALTER TABLE, Add Column
ALTER TABLE STUDENT ADD Grade CHAR(2);
Query 30: ALTER TABLE, Drop Column
ALTER TABLE STUDENT DROP COLUMN Grade;
Board Exam Pattern Tips
- SQL questions carry 2-3 marks each. Write the complete query, not just fragments.
- Write table output neatly. Draw proper column headers when the question says "write the output."
- GROUP BY + HAVING is a guaranteed question. Know the difference between WHERE and HAVING.
- Aggregate functions ignore NULL values. COUNT(*) counts all rows, but COUNT(column) skips NULLs.
- ORDER BY default is ASC. You do not need to write ASC explicitly.
- String values need single quotes -
WHERE Name = 'Aman', not double quotes. - Practice writing output tables. The examiner checks if you draw the correct rows AND correct columns.
Quick Reference: SQL Clause Order
Always write SQL clauses in this order:
SELECT column(s)
FROM table
WHERE condition
GROUP BY column
HAVING condition
ORDER BY column;
Remember the order: S-F-W-G-H-O ("Some Friends Work on Group Homework Online").
Master these 30 queries and you will confidently handle any SQL question in the CBSE Class 12 board exam. Practice writing them by hand, that is how the exam works.
Want to learn more?
Explore free chapter-wise notes with quizzes and code playground
Prefer watching over reading?
Subscribe for free.