21 questions from CBSE board, sample and compartment papers (2024–2027), 1 to 5 marks, each with a model answer. Questions on aggregate functions (SUM, AVG, MIN, MAX, COUNT), GROUP BY and HAVING, ORDER BY, and queries on one or two tables.
Which of the following is not an aggregate function in SQL?
(A)COUNT(*)
(B)MIN()
(C)LEFT()
(D)AVG()
Model answer
LEFT()
Explanation: LEFT() is a string/text function. COUNT, MIN, and AVG are aggregate functions.
Querying Using SQL· 3 Marks· Q32· 2025 Board Paper
(a) Given the following tables:
Table: STUDENTS
S_ID | NAME | AGE | CITY
1 | Rahul | 20 | Delhi
2 | Priya | 22 | Mumbai
3 | David | 21 | Delhi
4 | Neha | 23 | Bengaluru
5 | Khurshid | 22 | Delhi
Table: GRADES
S_ID | SUBJECT | GRADE
1 | Math | A
2 | English | B
3 | Math | C
4 | English | A
5 | Math | B
Write SQL queries for the following:
(i) To display the number of students from each city.
(ii) To find the average age of all students.
(iii) To list the names of students and their grades.
OR
(b) Consider the following tables:
Table 1: PRODUCTS
This table stores the basic details of the products available in a shop.
PID | PName | Category
201 | Laptop | Electronics
202 | Chair | Furniture
203 | Desk | Furniture
204 | Smartphone | NULL
205 | Tablet | Electronics
Table 2: SALES
This table records the number of units sold for each product.
SaleID | PID | UnitsSold
301 | 201 | 50
302 | 202 | 100
303 | 203 | 60
304 | 204 | 80
305 | 205 | 70
Write SQL queries for the following:
(i) To delete those records from table SALES whose UnitsSold is less than 80.
(ii) To display names of all products whose category is not known.
(iii) To display the product names along with their corresponding units sold.
Model answer
(i) SELECT CITY, COUNT(*) FROM STUDENTS GROUP BY CITY; (ii) SELECT AVG(AGE) FROM STUDENTS; (iii) SELECT NAME, GRADE FROM STUDENTS, GRADES WHERE STUDENTS.S_ID = GRADES.S_ID;
Explanation: Uses Group By, Aggregate Functions, and Equi-Join.
Querying Using SQL· 4 Marks· Q34· 2025 Board Paper
(a) An educational institution is maintaining a database for storing the details of courses being offered. The database includes a table COURSE with the following attributes:
C_ID: Stores the unique ID for each course.
C_NAME: Stores the course's name.
INSTRUCTOR: Stores the name of the course instructor.
DURATION: Stores the duration of the course in hours.
Table: COURSE
C_ID | C_NAME | INSTRUCTOR | DURATION
C101 | Data Structures | Dr. Alok | 40
C102 | Machine Learning | Prof. Sunita | 60
C103 | Web Development | Ms. Sakshi | 45
C104 | Database Management | Mr. Suresh | 50
C105 | Python Programming | Dr. Pawan | 35
Write SQL queries for the following:
(i) To add a new record with following specifications:
C_ID: C106, C_NAME: Introduction to AI, INSTRUCTOR: Ms. Preeti, DURATION: 55
(ii) To display the longest duration among all courses.
(iii) To count total number of courses run by the institution.
(iv) To display the instructors' name in lower case.
OR
(b) Ashutosh, who is a manager, has created a database to manage employee records. The database includes a table named EMPLOYEE whose attribute names are mentioned below:
EID: Stores the unique ID for each employee.
EMP_NAME: Stores the name of the employee.
DEPT: Stores the department of the employee.
SALARY: Stores the salary of the employee.
JOIN_DATE: Stores the employee's joining date.
Table: EMPLOYEE
EID | EMP_NAME | DEPT | SALARY | JOIN_DATE
E01 | ARJUN SINGH | SALES | 75000 | 2019-11-01
E02 | PRIYA JAIN | ENGINEERING | 85000 | 2020-05-20
E03 | RAVI SHARMA | MARKETING | 60000 | 2018-08-14
E04 | AYESHA | NULL | 50000 | 2021-01-10
E05 | RAHUL VERMA | FINANCE | 40000 | 2017-06-25
Write the output of the following SQL Queries:
(i) Select SUBSTRING(EMP_NAME, 1, 5) from EMPLOYEE where DEPT = 'ENGINEERING';
(ii) Select EMP_NAME from EMPLOYEE where month(JOIN_DATE) = 8;
(iii) Select EMP_NAME from EMPLOYEE where SALARY > 60000;
(iv) Select count(DEPT) from EMPLOYEE;
INSERT INTO COURSE VALUES('C106', 'Introduction to AI', 'Ms. Preeti', 55);
SELECT MAX(DURATION) FROM COURSE;
SELECT COUNT(*) FROM COURSE;
SELECT LOWER(INSTRUCTOR) FROM COURSE;
Assertion (A): The output of the SQL query SELECT COUNT(name) FROM students; will differ from the output of SELECT COUNT(*) FROM students; if there are some NULL values in the name column.
Reason (R): COUNT(column name) returns the count of NON NULL values in that column, whereas COUNT(*) returns the number of records in the table.
Model answer
Both (A) and (R) are True, and (R) correctly explains (A).
Explanation: COUNT(*) counts all rows including NULLs, while COUNT(column) counts only the non-null entries in that specific column.
Querying Using SQL· 1 Mark· Q11· 2025 Compartment Paper
What does the following SQL query do?
SELECT name, COUNT(*) FROM students GROUP BY name HAVING COUNT(*) > 1;
(A)Selects students with unique name.
(B)Selects students who have the same name and counts them.
(C)Displays the names and the number of occurrences for those names that appear more than once in the table.
(D)Orders students by name and counts them.
Model answer
Displays the names and the number of occurrences for those names that appear more than once in the table.
Explanation: GROUP BY name aggregates identical names. HAVING COUNT(*) > 1 filters out names that only appear once.
Querying Using SQL· 2 Marks· Q26· 2025 Compartment Paper
What are aggregate functions in SQL? Name any two aggregate functions.
Model answer
Aggregate functions perform operations on a set of values (a column) and return a single value. Examples: SUM(), AVG(), MAX(), MIN(), COUNT().
Explanation: They group multiple rows together to form a single summary value.
Querying Using SQL· 3 Marks· Q32· 2025 Compartment Paper
(a) Given the following tables:
Table: EMPLOYEES
EMP_ID | EMP_NAME | DEPARTMENT | JOIN_DATE
Table: PROJECTS
Project_ID | EMP_ID | PROJECT_NAME | HOURS
Write SQL queries to:
(i) For each department, show the department names and the total count of employees working there.
(ii) Display the total hours worked on the project named 'Budgeting'.
(iii) Display employee names along with the names of the projects they are working on.
OR
(b) Given the following tables:
Table: STUDENTS
StudentID | StudentName | Class
Table: EXAMS
ExamID | StudentID | Subject | Marks
Write SQL queries to:
(i) Calculate the average marks obtained by students in Mathematics.
(ii) For each class, show the class and the total count of students studying there.
(iii) Display the student names and their corresponding subjects.
Model answer
(a) Queries:
(i) SELECT DEPARTMENT, COUNT(*) FROM EMPLOYEES GROUP BY DEPARTMENT;
(ii) SELECT SUM(HOURS) FROM PROJECTS WHERE PROJECT_NAME = 'Budgeting';
(iii) SELECT EMP_NAME, PROJECT_NAME FROM EMPLOYEES E, PROJECTS P WHERE E.EMP_ID = P.EMP_ID;
OR
(b) Queries:
(i) SELECT AVG(Marks) FROM EXAMS WHERE Subject = 'Mathematics';
(ii) SELECT Class, COUNT(*) FROM STUDENTS GROUP BY Class;
(iii) SELECT StudentName, Subject FROM STUDENTS S, EXAMS E WHERE S.StudentID = E.StudentID;
/* Part A Queries */
SELECT DEPARTMENT, COUNT(*) FROM EMPLOYEES GROUP BY DEPARTMENT;
SELECT SUM(HOURS) FROM PROJECTS WHERE PROJECT_NAME = 'Budgeting';
SELECT E.EMP_NAME, P.PROJECT_NAME FROM EMPLOYEES E, PROJECTS P WHERE E.EMP_ID = P.EMP_ID;
/* Part B Queries */
SELECT AVG(Marks) FROM EXAMS WHERE Subject = 'Mathematics';
SELECT Class, COUNT(*) FROM STUDENTS GROUP BY Class;
SELECT S.StudentName, E.Subject FROM STUDENTS S, EXAMS E WHERE S.StudentID = E.StudentID;
Raj, a Database Administrator, needs to display the average pay of workers from those departments which have more than five employees. He is experiencing a problem while running the following query:
SELECT DEPT, AVG(SAL) FROM EMP WHERE COUNT(*) > 5 GROUP BY DEPT;
Which of the following is a correct query to perform the given task?
(A)i. SELECT DEPT, AVG(SAL) FROM EMP WHERE COUNT(*) > 5 GROUP BY DEPT;
(B)ii. SELECT DEPT, AVG(SAL) FROM EMP HAVING COUNT(*) > 5 GROUP BY DEPT;
(C)iii. SELECT DEPT, AVG(SAL) FROM EMP GROUP BY DEPT WHERE COUNT(*) > 5;
(D)iv. SELECT DEPT, AVG(SAL) FROM EMP GROUP BY DEPT HAVING COUNT(*) > 5;
Model answer
iv. SELECT DEPT, AVG(SAL) FROM EMP GROUP BY DEPT HAVING COUNT(*) > 5;
Explanation: Aggregate conditions like COUNT(*) > 5 must be filtered using the HAVING clause after the GROUP BY clause, not the WHERE clause.
Querying Using SQL· 2 Marks· Q25· 2024 Sample Paper
What are aggregate functions in SQL? Name any two.
Model answer
Aggregate functions: These are also called multiple row functions. These functions work on a set of records as a whole, and return a single value for each column of the records on which the function is applied.
Max(), Min(), Avg(), Sum(), Count() and Count(*) are few examples of multiple row functions.
Querying Using SQL· 3 Marks· Q26· 2024 Sample Paper
Based on the SQL table CAR_SALES, write suitable queries for the following:
+--------+-------------------+--------+-------+-------+
| NUMBER | SEGMENT | FUEL | QT1 | QT2 |
+--------+-------------------+--------+-------+-------+
| 1 | Compact HatchBack | Petrol | 56000 | 70000 |
| 2 | Compact HatchBack | Diesel | 34000 | 40000 |
| 3 | MUV | Petrol | 33000 | 35000 |
| 4 | MUV | Diesel | 14000 | 15000 |
| 5 | SUV | Petrol | 27000 | 54000 |
| 6 | SUV | Diesel | 18000 | 30000 |
| 7 | Sedan | Petrol | 8000 | 10000 |
| 8 | Sedan | Diesel | 1000 | 5000 |
+--------+-------------------+--------+-------+-------+
i. Display fuel wise average sales in the first quarter.
ii. Display segment wise highest sales in the second quarter.
iii. Display the records in the descending order of sales in the second quarter.
OR
Predict the output of the following queries based on the table CAR_SALES given above:
i. SELECT LEFT(SEGMENT,2) FROM CAR_SALES WHERE FUEL= "PETROL";
ii. SELECT (QT2-QT1)/2 "AVG SALE" FROM CAR_SALES WHERE SEGMENT= "SUV";
iii. SELECT SUM(QT1) "TOT SALE" FROM CAR_SALES WHERE FUEL= "DIESEL";
Model answer
i. SELECT FUEL, AVG(QT1) FROM CAR_SALES GROUP BY FUEL;
ii. SELECT SEGMENT, MAX(QT2) FROM CAR_SALES GROUP BY SEGMENT;
iii. SELECT * FROM CAR_SALES ORDER BY QT2 DESC;
OR
i.
+-----------------+
| LEFT(SEGMENT,2) |
+-----------------+
| Co |
| MU |
| SU |
| Se |
+-----------------+
ii.
+------------+
| AVG SALE |
+------------+
| 13500.0000 |
| 6000.0000 |
+------------+
iii.
+----------+
| TOT SALE |
+----------+
| 67000 |
+----------+
State whether the following statement is True or False:
In SQL, the HAVING clause is used to apply filter on groups formed by the GROUP BY clause.
Model answer
True
Explanation: HAVING is used to filter groups, whereas WHERE is used to filter individual rows before grouping.
Querying Using SQL· 1 Mark· Q11· 2025 Sample Paper
Fill in the Blank The COUNT(*) function provides the total number of __________ within a relation (table) in a relational database.
(A)Columns
(B)Unique values
(C)Not-null values
(D)Rows
Model answer
(D) Rows
Explanation: COUNT(*) counts all rows in the result set, including those with NULL values.
Querying Using SQL· 3 Marks· Q32· 2025 Sample Paper
(A) Consider the following tables:
Table 1: EMPLOYEE, which stores Employee ID (EMP_ID), Employee Name (EMP_NAME), Employee City (EMP_CITY)
Table 2: PAYROLL, which stores Employee ID (EMP_ID), Department (DEPARTMENT), Designation (DESIGNATION), and Salary (SALARY) for various employees.
Note: Attribute names are written within brackets.
Table: EMPLOYEE
EMP_ID | EMP_NAME | EMP_CITY
1 | ABHINAV | AGRA
2 | KABIR | FARIDABAD
3 | ESHA | NOIDA
4 | PAUL | SEOUL
5 | VICTORIA | LONDON
Table: PAYROLL
EMP_ID | DEPARTMENT | DESIGNATION | SALARY
1 | SALES | MANAGER | 75000
2 | SALES | ASSOCIATE | 50000
3 | ENGINEERING | MANAGER | 95000
4 | ENGINEERING | ENGINEER | 70000
5 | MARKETING | MANAGER | 65000
Write appropriate SQL queries for the following:
I. Display department-wise average Salary.
II. List all designations in the decreasing order of Salary.
III. Display employee name along with their corresponding departments.
OR
(B) Consider the following tables:
Table 1: ATHLETE, which stores AthleteID, Name, Country. The table displays basic information of the athletes.
Table 2: MEDALS, which stores AthleteID, Sport, and Medals. The table displays the number of medals won by each athlete in their respective sports.
Table: ATHLETE
AthleteID | Name | COUNTRY
101 | Arjun | INDIA
102 | Priya | INDIA
103 | Asif | UAE
104 | Rozy | USA
105 | David | DENMARK
Table: MEDALS
AthleteID | Sport | Medals
101 | Swimming | 8
102 | Track | 3
103 | Gymnastics | 5
104 | Swimming | 2
105 | Track | 6
Write appropriate SQL queries for the following:
I. Display the sports-wise total number of medals won.
II. Display the names of all the Indian athletes in uppercase.
III. Display the athlete name along with their corresponding sports.
/* Part A */
SELECT DEPARTMENT, AVG(SALARY) FROM PAYROLL GROUP BY DEPARTMENT;
SELECT DESIGNATION FROM PAYROLL ORDER BY SALARY DESC;
SELECT EMP_NAME, DEPARTMENT FROM EMPLOYEE E, PAYROLL P WHERE E.EMP_ID = P.EMP_ID;
/* Part B (OR) */
SELECT SPORT, SUM(Medals) FROM MEDALS GROUP BY SPORT;
SELECT UPPER(Name) FROM ATHLETE WHERE COUNTRY='INDIA';
Querying Using SQL· 5 Marks· Q37· 2025 Sample Paper
(A) Write suitable SQL query for the following:
I. To display the average score from the test_results column (attribute) in the Exams table.
II. To display the last three characters of the registration_number column (attribute) in the Vehicles table. (Note: The registration numbers are stored in the format DL-01-AV-1234)
III. To display the data from the column (attribute) username in the Users table, after eliminating any leading and trailing spaces.
IV. To display the maximum value in the salary column (attribute) of the Employees table.
V. To determine the count of rows in the Suppliers table.
OR
(B) Write suitable SQL query for the following:
I. Round the value of pi (3.14159) to two decimal places.
II. Calculate the remainder when 125 is divided by 8.
III. Display the number of characters in the word 'NewDelhi'.
IV. Display the first 5 characters from the word 'Informatics Practices'.
V. Display details from 'email' column (attribute), in the 'Students' table, after removing any leading and trailing spaces.
/* Part A */
SELECT AVG(test_results) FROM Exams;
SELECT RIGHT(registration_number, 3) FROM Vehicles;
SELECT TRIM(username) FROM Users;
SELECT MAX(salary) FROM Employees;
SELECT COUNT(*) FROM Suppliers;
/* Part B */
SELECT ROUND(3.14159, 2);
SELECT MOD(125, 8);
SELECT LENGTH('NewDelhi');
SELECT LEFT('Informatics Practices', 5);
SELECT TRIM(email) FROM Students;
Querying Using SQL· 1 Mark· Q11· 2026 Sample Paper
Which of the following SQL function is used to count the non-NULL values in a column named column_name?
(A)COUNT(*)
(B)COUNT(column_name)
(C)SUM(column_name)
(D)AVG(column_name)
Model answer
COUNT(column_name)
Explanation: COUNT(column_name) counts non-null values. COUNT(*) counts all rows including nulls.
Querying Using SQL· 1 Mark· Q14· 2026 Sample Paper
Which SQL command is used to sort rows in either ascending or descending order of values in a specific column?
(A)ORDER BY
(B)SORT BY
(C)GROUP BY
(D)SORT ON
Model answer
ORDER BY
Explanation: ORDER BY is the clause used to sort the result set.
Querying Using SQL· 1 Mark· Q19· 2026 Sample Paper
Which of the following is NOT an aggregate function in SQL?
(A)MIN()
(B)SUM()
(C)UPPER()
(D)AVG()
Model answer
UPPER()
Explanation: UPPER() is a string/text function. The others calculate values across multiple rows.