Skip to content
Computer ScienceCBSE Class 12 · Chapter 9

Structured Query Language (SQL): CBSE Class 12 Computer Science

Updated 14 October 2026 · Fact-checked

SQL is the language for creating and querying relational databases. Class 12 covers DDL and DML commands, data types char, varchar, int, float and date, constraints, CREATE, ALTER and DROP, INSERT, UPDATE and DELETE, SELECT with WHERE, IN, BETWEEN, LIKE, NULL tests, DISTINCT and ORDER BY, aggregates with GROUP BY and HAVING, and cartesian product, equi-join and natural join.

What Structured Query Language (SQL) is about

Structured Query Language (SQL) is the heart of Unit 3, Database Management (20 of 70 theory marks). In the 2026-27 sample paper it gives 1-mark MCQs, 2-mark "write the output" or "differentiate" items, and the 4-mark Section D question of four queries on two tables. The practical exam adds four SQL queries worth 4 marks. Every query you write is marked line by line, so a wrong keyword or a missing quote costs the mark.

The syllabus list (MySQL):

  • DDL and DML; data types char(n), varchar(n), int, float, date; constraints NOT NULL, UNIQUE, PRIMARY KEY
  • database commands: CREATE DATABASE, USE, SHOW DATABASES, DROP DATABASE
  • table commands: SHOW TABLES, CREATE TABLE, DESCRIBE, ALTER TABLE (add or remove a column, add or remove the primary key), DROP TABLE
  • INSERT, UPDATE, DELETE, SELECT with mathematical, relational and logical operators, aliasing, DISTINCT, WHERE, IN, BETWEEN, ORDER BY, NULL with IS NULL / IS NOT NULL, LIKE
  • aggregate functions MAX, MIN, AVG, SUM, COUNT; GROUP BY and HAVING
  • joins: cartesian product of two tables, equi-join and natural join

Single-row maths, string and date functions and set operations (UNION and others) from NCERT are not in the 2026-27 list. All examples use these two tables:

EMPLOYEE

EmpIDNameDeptIDSalaryCityDOJ
E01Asha RaoD152000Delhi2019-04-12
E02Ravi KumarD238000Mumbai2021-07-01
E03Meena IyerD161000Chennai2017-01-20
E04Joy DasD345000Delhi2022-11-15
E05Kiran ShahD2NULLPune2023-02-10
E06Farah KhanD345000Mumbai2020-09-05

DEPT

DeptIDDNameLocation
D1SalesDelhi
D2FinanceMumbai
D3ITBengaluru
D4HRDelhi

Weightage: This chapter is part of Unit 3, Database Management, which carries 20 of the 70 theory marks together with Database Concepts and Interface of Python with an SQL Database.Source: CBSE Computer Science (083) syllabus 2026-27, distribution of marks, Unit 3 Database Management

Key concepts

DDL, DML, data types and constraints
NCERT section NCERT 9.3, 9.4

DDL (Data Definition Language) commands define or change the structure: CREATE, ALTER, DROP. DML (Data Manipulation Language) commands work on the data: INSERT, UPDATE, DELETE, SELECT. Data types: char(n) fixed length (padded to n characters), varchar(n) variable length up to n (uses only what it needs), int whole numbers, float decimals, date in 'YYYY-MM-DD' format. Constraints are rules on a column: NOT NULL (a value is compulsory), UNIQUE (no two rows share a value; NULLs are allowed), PRIMARY KEY (unique and not NULL; one per table). Text and date values are written in single quotes; numbers are not.

Worked example

Write the command to create EMPLOYEE with EmpID as primary key, Name compulsory and the other columns as in the table.

Try it first, then show the solutionSolution
  1. Pick types: codes of fixed length → char; names and cities → varchar; salary → int; DOJ → date.
  2. CREATE TABLE EMPLOYEE (
      EmpID CHAR(3) PRIMARY KEY,
      Name VARCHAR(20) NOT NULL,
      DeptID CHAR(2),
      Salary INT,
      City VARCHAR(15),
      DOJ DATE
    );

Answer: The CREATE TABLE statement above (it is a DDL command).

Database and table commands; ALTER TABLE
NCERT section NCERT 9.4, 9.5
CREATE DATABASE Office;
SHOW DATABASES;
USE Office;
SHOW TABLES;
DESCRIBE EMPLOYEE;      -- or DESC EMPLOYEE: shows columns, types, keys
DROP TABLE EMPLOYEE;    -- removes the table and its data
DROP DATABASE Office;   -- removes the whole database

ALTER TABLE changes the structure of an existing table:

ALTER TABLE EMPLOYEE ADD Email VARCHAR(30);     -- add a column
ALTER TABLE EMPLOYEE DROP Email;                -- remove a column (DROP COLUMN Email also works)
ALTER TABLE DEPT DROP PRIMARY KEY;              -- remove the primary key
ALTER TABLE DEPT ADD PRIMARY KEY (DeptID);      -- add a primary key

A new column is filled with NULL in existing rows. DROP removes structure and data; DELETE removes only rows.

Worked example

EMPLOYEE has degree 6 and cardinality 6. Write commands to add a column Phone of 10 characters and then remove the column City. What is the degree now, and what does the new column hold for existing rows?

Try it first, then show the solutionSolution
  1. ALTER TABLE EMPLOYEE ADD Phone CHAR(10); raises the degree to 7.
  2. ALTER TABLE EMPLOYEE DROP City; lowers it to 6.
  3. Existing rows get NULL in Phone; cardinality stays 6.

Answer: Degree 6 again (7 then 6); Phone is NULL in all existing rows.

INSERT, UPDATE and DELETE
NCERT section NCERT 9.6, 9.7
INSERT INTO DEPT VALUES ('D5', 'Admin', 'Pune');             -- values in column order
INSERT INTO EMPLOYEE (EmpID, Name, DeptID) VALUES ('E07', 'Neha Jain', 'D4');   -- named columns; the rest become NULL
UPDATE EMPLOYEE SET Salary = Salary + 5000 WHERE DeptID = 'D3';
DELETE FROM EMPLOYEE WHERE City = 'Mumbai';

Without WHERE, UPDATE changes every row and DELETE removes every row (the empty table remains). UPDATE can set several columns: SET Salary = 50000, City = 'Pune'.

Worked example

On the original EMPLOYEE table, run UPDATE EMPLOYEE SET Salary = Salary + 5000 WHERE DeptID = 'D3'; then DELETE FROM EMPLOYEE WHERE City = 'Mumbai';. Which employees remain, and what are the D3 salaries?

Try it first, then show the solutionSolution
  1. D3 employees are Joy (45000) and Farah (45000); both become 50000.
  2. Mumbai rows are Ravi (E02) and Farah (E06); both are deleted.
  3. Remaining: E01 Asha, E03 Meena, E04 Joy (50000), E05 Kiran.

Answer: E01, E03, E04, E05 remain (cardinality 4); Joy's salary is 50000 (Farah's row is deleted).

SELECT with WHERE, operators and aliases
NCERT section NCERT 9.7.1-9.7.4

SELECT column_list FROM table WHERE condition; * selects all columns. Operators: mathematical + - * / %; relational = <> (or !=) < > <= >=; logical AND OR NOT. An alias renames a column or table in the output: SELECT Name, Salary * 12 AS AnnualSalary FROM EMPLOYEE; (AS is optional; use quotes if the alias has a space). DISTINCT removes duplicate rows from the result: SELECT DISTINCT City FROM EMPLOYEE;.

Worked example

Write the output of SELECT Name, Salary * 12 AS AnnualSalary FROM EMPLOYEE WHERE DeptID = 'D1';

Try it first, then show the solutionSolution
  1. D1 rows: Asha Rao (52000) and Meena Iyer (61000).
  2. 52000 × 12 = 624000; 61000 × 12 = 732000; the column is headed AnnualSalary.

Answer:

NameAnnualSalary
Asha Rao624000
Meena Iyer732000

IN, BETWEEN, LIKE and NULL
NCERT section NCERT 9.7.5-9.7.8

IN (list) matches any value in the list: City IN ('Delhi', 'Pune'). BETWEEN a AND b is inclusive at both ends: Salary BETWEEN 40000 AND 52000 includes 52000. LIKE matches patterns: % stands for any number of characters (including none) and _ for exactly one character; '_a%' means the second letter is a. NULL means unknown: it is never equal to anything, not even NULL, so Salary = NULL finds nothing; use IS NULL or IS NOT NULL. Any arithmetic with NULL gives NULL.

Worked example

Give the outputs of (a) SELECT Name FROM EMPLOYEE WHERE Name LIKE '_a%'; (b) SELECT Name, Salary FROM EMPLOYEE WHERE Salary BETWEEN 40000 AND 52000; (c) SELECT Name FROM EMPLOYEE WHERE Salary IS NULL;

Try it first, then show the solutionSolution
  1. (a) Second letter 'a': Ravi Kumar, Farah Khan (Asha has 's' second; Kiran has 'i').
  2. (b) 40000 to 52000 inclusive: Asha 52000, Joy 45000, Farah 45000. Kiran's NULL is not between anything.
  3. (c) Only Kiran Shah has NULL salary.

Answer: (a) Ravi Kumar, Farah Khan (b) Asha Rao 52000, Joy Das 45000, Farah Khan 45000 (c) Kiran Shah

ORDER BY
NCERT section NCERT 9.7.6

ORDER BY column [ASC | DESC] sorts the result; ASC (ascending) is the default. Several columns break ties: ORDER BY Salary DESC, Name sorts by salary high to low and, for equal salaries, by name A to Z. In MySQL, NULL comes first in ascending order and last in descending order. ORDER BY comes after WHERE (and after GROUP BY / HAVING) in the statement.

Worked example

Write the output of SELECT Name, Salary FROM EMPLOYEE WHERE Salary >= 45000 ORDER BY Salary DESC, Name;

Try it first, then show the solutionSolution
  1. Rows with Salary ≥ 45000: Asha 52000, Meena 61000, Joy 45000, Farah 45000 (Kiran's NULL fails the test).
  2. Descending salary: 61000, 52000, then the two 45000s.
  3. Tie broken by name: Farah Khan before Joy Das.

Answer:

NameSalary
Meena Iyer61000
Asha Rao52000
Farah Khan45000
Joy Das45000

Aggregate functions
NCERT section NCERT 9.8 (aggregate functions only)

Aggregate functions work on a whole column (or group) and return one value: MAX(), MIN(), SUM(), AVG(), COUNT(). All of them ignore NULL, except COUNT(*), which counts rows. COUNT(column) counts non-NULL values, and COUNT(DISTINCT column) counts different non-NULL values. MySQL shows AVG with decimals, e.g. 48200.0000. An aggregate cannot be used in WHERE; use HAVING for conditions on groups.

Worked example

Give the output of SELECT COUNT(*), COUNT(Salary), SUM(Salary), AVG(Salary), MAX(Salary), MIN(Salary) FROM EMPLOYEE;

Try it first, then show the solutionSolution
  1. COUNT(*) counts all 6 rows; COUNT(Salary) skips Kiran's NULL: 5.
  2. SUM = 52000 + 38000 + 61000 + 45000 + 45000 = 241000.
  3. AVG = 241000 / 5 = 48200 (the NULL is not counted as 0).
  4. MAX 61000, MIN 38000.

Answer: 6 | 5 | 241000 | 48200 (MySQL shows 48200.0000) | 61000 | 38000

GROUP BY and HAVING
NCERT section NCERT 9.9

GROUP BY column forms one group per distinct value and lets aggregates work per group: SELECT DeptID, COUNT(*) FROM EMPLOYEE GROUP BY DeptID;. HAVING filters groups after they are formed and may use aggregates: HAVING SUM(Salary) > 90000. WHERE filters rows before grouping and cannot use aggregates. Order of clauses: SELECT ... FROM ... WHERE ... GROUP BY ... HAVING ... ORDER BY .... Columns in SELECT should be either grouped columns or aggregates.

Worked example

Give the outputs of (a) SELECT DeptID, SUM(Salary) FROM EMPLOYEE GROUP BY DeptID HAVING SUM(Salary) > 90000; (b) SELECT City, COUNT(*) FROM EMPLOYEE GROUP BY City HAVING COUNT(*) > 1;

Try it first, then show the solutionSolution
  1. (a) Sums: D1 52000 + 61000 = 113000; D2 38000 (NULL ignored); D3 90000. Only D1 exceeds 90000 (90000 is not > 90000).
  2. (b) Counts: Delhi 2, Mumbai 2, Chennai 1, Pune 1. Delhi and Mumbai have more than one.

Answer: (a)

DeptIDSUM(Salary)
D1113000
(b)
CityCOUNT(*)
Delhi2
Mumbai2

Joins: cartesian product, equi-join and natural join
NCERT section NCERT 9.10 (joins), 9.11

The cartesian product SELECT * FROM EMPLOYEE, DEPT; pairs every row of the first table with every row of the second: degree = 6 + 3 = 9, cardinality = 6 × 4 = 24. Most of those pairs are meaningless. An equi-join keeps only pairs where the common column is equal: WHERE EMPLOYEE.DeptID = DEPT.DeptID; the common column appears twice in SELECT *, and a column name present in both tables must be qualified (EMPLOYEE.DeptID) or given via table aliases (E.DeptID). A natural join SELECT * FROM EMPLOYEE NATURAL JOIN DEPT; joins on all columns with the same name automatically and shows the common column only once.

Worked example

(a) Degree and cardinality of EMPLOYEE × DEPT? (b) Output of SELECT E.Name, D.DName FROM EMPLOYEE E, DEPT D WHERE E.DeptID = D.DeptID AND D.DName = 'IT'; (c) How many columns does SELECT * FROM EMPLOYEE NATURAL JOIN DEPT; show?

Try it first, then show the solutionSolution
  1. (a) Degree 6 + 3 = 9; cardinality 6 × 4 = 24.
  2. (b) IT is D3; D3 employees are Joy Das and Farah Khan.
  3. (c) 6 + 3 - 1 = 8, because DeptID appears once.

Answer: (a) 9 and 24 (b) Joy Das IT; Farah Khan IT (c) 8 columns

Structured Query Language (SQL) in real life

Each concept, met in an everyday situation. Read the situation, answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.

GROUP BY with HAVING

Suppose a supermarket chain stores every bill in a table SALES(BillNo, Store, Amount) and the manager wants to know which stores sold more than 5 lakh rupees in total.

Write the query.

Try it first, then show the solutionSolution
  1. Total per store needs GROUP BY Store with SUM(Amount).
  2. A condition on that total needs HAVING, not WHERE.
  3. SELECT Store, SUM(Amount) AS Total
    FROM SALES
    GROUP BY Store
    HAVING SUM(Amount) > 500000;

Answer: The query above lists each store whose total exceeds 500000.

Memory hook: WHERE picks the bills; HAVING picks the stores.

LIKE patterns

Imagine a school helpdesk where a parent remembers only that the teacher's name starts with 'Pr' and has 'sh' somewhere later.

Write a query on TEACHER(TName, Subject) to find possible teachers.

Try it first, then show the solutionSolution
  1. Starts with Pr: 'Pr%'. Contains sh later: '%sh%'.
  2. SELECT TName, Subject FROM TEACHER
    WHERE TName LIKE 'Pr%' AND TName LIKE '%sh%';

Answer: The query above (e.g. it would match Prakash and Pritish).

Memory hook: % is a stretchy blank; _ is one empty box.

UPDATE with WHERE

Suppose a company gives a 10% raise only to the IT department, and a new clerk runs the UPDATE without a WHERE clause.

What goes wrong, and what is the correct command for EMPLOYEE (IT is D3)?

Try it first, then show the solutionSolution
  1. Without WHERE, every employee's salary increases by 10%.
  2. UPDATE EMPLOYEE SET Salary = Salary * 1.1 WHERE DeptID = 'D3';

Answer: Everyone gets the raise by mistake; the WHERE clause limits it to D3.

Memory hook: No WHERE, no mercy: UPDATE and DELETE hit every row.

Topics in this chapter

Structured Query Language (SQL): syntax and key facts

Clause order
SELECT [DISTINCT] cols FROM tables WHERE row-condition GROUP BY cols HAVING group-condition ORDER BY cols [ASC|DESC];
WHERE before grouping (no aggregates); HAVING after grouping (aggregates allowed).
DDL vs DML
DDL: CREATE, ALTER, DROP. DML: INSERT, UPDATE, DELETE, SELECT.
DROP TABLE removes the table; DELETE FROM removes rows only.
ALTER TABLE
ADD col type; DROP col; ADD PRIMARY KEY (col); DROP PRIMARY KEY
New columns are NULL in existing rows.
LIKE wildcards
% = any number of characters (even none); _ = exactly one character
'A%' starts with A; '%a' ends with a; '_a%' has a as the second letter; '%an%' contains an.
NULL rules
Test with IS NULL / IS NOT NULL; aggregates ignore NULL except COUNT(*); NULL in arithmetic gives NULL
AVG divides by the count of non-NULL values.
Cartesian product
degree = d1 + d2; cardinality = c1 × c2
Equi-join adds WHERE t1.col = t2.col; natural join shows the common column once.
Constraints
NOT NULL: value required. UNIQUE: no duplicates (NULLs allowed). PRIMARY KEY: unique + not NULL, one per table.
char(n) fixed length; varchar(n) variable length.

How the board asks Structured Query Language (SQL)

The question patterns to expect, and the method that works for each.

Question typeHow it is askedMethod
Write queries on two tables (4 marks)Two related tables are given; write four queries using WHERE, ORDER BY, GROUP BY/HAVING, aggregates, UPDATE/DELETE/ALTER or a join (Section D).Underline the columns asked for and the condition; check quotes around text and dates; for 'each department' use GROUP BY; for a condition on a count or sum use HAVING; for data from both tables join on the common column.
Write the output (2-3 marks)Given queries on a table, write their results.Filter rows first (WHERE), then group and aggregate, then HAVING, then sort; write the column headings exactly as in the SELECT, including aliases and expressions.
Differentiatechar vs varchar; WHERE vs HAVING; DELETE vs DROP; COUNT(*) vs COUNT(col); primary key vs unique; equi-join vs natural join (2 marks).Two crisp points with a one-line SQL example each.
Degree and cardinality after operationsAfter a cartesian product, an ALTER or INSERT/DELETE (1 mark).Columns add, rows multiply in a cartesian product; ALTER changes degree; INSERT/DELETE change cardinality.
MCQ and assertion-reasonWhich command is DDL? Which clause filters groups? Which wildcard matches one character? Does AVG count NULL? (1 mark).Recall the key-facts list; read every word of the options (e.g. COUNT(*) vs COUNT(col)).

Tricks to remember Structured Query Language (SQL)

Short memory aids. Say each one aloud once, then use it on a question the same day.

  • "Some Friendly Wizards Go Home Often"

    SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY.

    Use it when: Clause order in every query.

  • CAD for DDL

    Create, Alter, Drop change the Design.

    Use it when: Classifying DDL and DML.

  • COUNT star counts stars too

    COUNT(*) counts every row, even those full of NULLs; every other aggregate skips NULL.

    Use it when: COUNT and AVG output questions.

  • Cartesian: add the columns, multiply the rows

    Degree adds, cardinality multiplies.

    Use it when: Cartesian product questions.

  • BETWEEN hugs both ends

    BETWEEN 40000 AND 52000 includes 40000 and 52000.

    Use it when: Range queries.

  • NULL is not a value

    Nothing equals NULL, so ask 'IS it NULL?', never '= NULL'.

    Use it when: NULL conditions.

Structured Query Language (SQL): board-level solved questions

Ordered from board level to HOTS. Cover the solution, write your own, then compare step by step.

Example 1: DISTINCT and ORDER BY · 1 mark · MCQBoard level

How many rows does SELECT DISTINCT City FROM EMPLOYEE; return? (a) 6 (b) 5 (c) 4 (d) 3

Try it first, then show the solutionSolution
  1. Cities: Delhi, Mumbai, Chennai, Delhi, Pune, Mumbai.
  2. Distinct values: Delhi, Mumbai, Chennai, Pune.

Answer: (c) 4

Example 2: Group output with a WHERE · 2 marks · Short answerHarder board level

Write the output of SELECT DeptID, MAX(Salary) FROM EMPLOYEE WHERE City <> 'Delhi' GROUP BY DeptID;

Try it first, then show the solutionSolution
  1. WHERE removes Delhi rows (Asha, Joy) first. Left: Ravi D2 38000, Meena D1 61000, Kiran D2 NULL, Farah D3 45000.
  2. Group: D1 {61000}, D2 {38000, NULL}, D3 {45000}. MAX ignores NULL.

Answer:

DeptIDMAX(Salary)
D161000
D238000
D345000
(The order of groups may differ in MySQL.)

Example 3: Assertion-reason on COUNT · 1 mark · Assertion-reasonHOTS / competency

Assertion (A): For EMPLOYEE, SELECT COUNT(*) and SELECT COUNT(Salary) return different values.
Reason (R): COUNT(column) does not count NULL values.
(a) Both true, R explains A (b) Both true, R does not explain A (c) A true, R false (d) A false, R true

Try it first, then show the solutionSolution
  1. COUNT(*) = 6; COUNT(Salary) = 5 because Kiran's salary is NULL.
  2. R gives the reason.

Answer: (a) Both A and R are true and R is the correct explanation of A.

Example 4: Four queries on two tables · 4 marks · Long answerHarder board level

Write queries to: (i) display the department names and the number of employees in each, using a join (ii) display names of employees who joined before 1 January 2020, oldest first (iii) display the cities from which more than one employee comes, with the count (iv) delete the department HR from DEPT. Also give the output of (ii).

Try it first, then show the solutionSolution
  1. (i)
    SELECT DName, COUNT(*) FROM EMPLOYEE E, DEPT D
    WHERE E.DeptID = D.DeptID GROUP BY DName;
  2. (ii)
    SELECT Name, DOJ FROM EMPLOYEE WHERE DOJ < '2020-01-01' ORDER BY DOJ;
  3. (iii)
    SELECT City, COUNT(*) FROM EMPLOYEE GROUP BY City HAVING COUNT(*) > 1;
  4. (iv)
    DELETE FROM DEPT WHERE DName = 'HR';
  5. Output of (ii): Meena Iyer (2017-01-20), then Asha Rao (2019-04-12).

Answer: Queries as above; (ii) gives

NameDOJ
Meena Iyer2017-01-20
Asha Rao2019-04-12

Example 5: After UPDATE and DELETE · 2 marks · Short answerHOTS / competency

Starting from the original EMPLOYEE table, these run in order: UPDATE EMPLOYEE SET Salary = 40000 WHERE Salary IS NULL; then SELECT AVG(Salary) FROM EMPLOYEE;. What is printed, and why is it different from 48200?

Try it first, then show the solutionSolution
  1. Kiran's NULL becomes 40000, so all 6 rows now have salaries.
  2. Total = 241000 + 40000 = 281000; AVG = 281000 / 6 = 46833.33...

Answer: 46833.3333 (MySQL); it differs because the NULL row now has a value and is counted.

Example 6: Cartesian product counting · 1 mark · Very short answerHarder board level

Table A has degree 4 and cardinality 5; table B has degree 3 and cardinality 6. Find the degree and cardinality of (i) the cartesian product A × B (ii) the natural join of A and B if they share exactly one column name (give the degree only).

Try it first, then show the solutionSolution
  1. (i) Degree 4 + 3 = 7; cardinality 5 × 6 = 30.
  2. (ii) The common column appears once: 4 + 3 - 1 = 6 (the cardinality depends on the data).

Answer: (i) 7 and 30 (ii) degree 6

Example 7: Find the error · 2 marks · Short answerHarder board level

Each query has an error. Correct it: (a) SELECT Name FROM EMPLOYEE WHERE Salary = NULL; (b) SELECT DeptID, AVG(Salary) FROM EMPLOYEE WHERE AVG(Salary) > 40000 GROUP BY DeptID; (c) SELECT * FROM EMPLOYEE ORDER Salary; (d) DELETE * FROM EMPLOYEE WHERE City = 'Pune';

Try it first, then show the solutionSolution
  1. (a) NULL needs IS: WHERE Salary IS NULL.
  2. (b) Aggregate condition goes in HAVING: ... GROUP BY DeptID HAVING AVG(Salary) > 40000;
  3. (c) The keyword is ORDER BY: ORDER BY Salary.
  4. (d) DELETE takes no column list: DELETE FROM EMPLOYEE WHERE City = 'Pune';

Answer: Corrections as shown in the four steps.

Example 8: LIKE pattern output · 1 mark · MCQHOTS / competency

Which names does SELECT Name FROM EMPLOYEE WHERE Name LIKE '%r%'; return in MySQL (case-insensitive by default)? (a) Ravi Kumar, Farah Khan (b) Asha Rao, Ravi Kumar, Meena Iyer, Kiran Shah, Farah Khan (c) all six (d) Meena Iyer, Kiran Shah, Farah Khan

Try it first, then show the solutionSolution
  1. Look for r or R anywhere: Asha Rao (R), Ravi Kumar (R, r), Meena Iyer (r), Kiran Shah (r), Farah Khan (r).
  2. Joy Das has no r.

Answer: (b) Asha Rao, Ravi Kumar, Meena Iyer, Kiran Shah, Farah Khan

Case-study practice (Section E style)

Read the situation, then answer every part. In the board paper this is a 4-mark question split into 1 + 1 + 2 marks.

Library database

Suppose the school library uses two tables:

BOOK

BookIDTitleAuthorPriceCopies
B1Python BasicsSharma4505
B2SQL Made EasyRao3002
B3NetworksSharma5203
B4Data StoriesIyerNULL4

ISSUE

IssueNoBookIDMember
1B1Asha
2B3Ravi
3B1Joy
  1. (i) · 1 mark

    Write the output of SELECT Author, COUNT(*) FROM BOOK GROUP BY Author HAVING COUNT(*) > 1;

    Answer

    Answer: Sharma | 2

    Sharma wrote B1 and B3; Rao and Iyer one book each.

  2. (ii) · 1 mark

    Write the output of SELECT SUM(Price * Copies), AVG(Price) FROM BOOK;

    Answer

    Answer: 4410 | 423.3333

    450 × 5 + 300 × 2 + 520 × 3 = 2250 + 600 + 1560 = 4410 (B4's NULL price gives NULL and is ignored). AVG = (450 + 300 + 520) / 3 = 423.3333.

  3. (iii) · 2 marks

    Write a query to display the Title and Member for every issue (equi-join), and state the degree and cardinality of BOOK × ISSUE.

    Answer

    Answer:

    SELECT Title, Member FROM BOOK B, ISSUE I WHERE B.BookID = I.BookID;
    Output (row order may differ): Python Basics Asha; Networks Ravi; Python Basics Joy. BOOK × ISSUE: degree 5 + 3 = 8, cardinality 4 × 3 = 12.

    Join on the common BookID; the cartesian product adds columns and multiplies rows.

Where the marks are

How a typical answer is split into step marks. Show every step: a right final answer alone rarely earns full marks.

Differentiate between WHERE and HAVING with an example. [2 marks] (2 marks)

StepMarks
WHERE filters rows before grouping; cannot use aggregates, e.g. WHERE City = 'Delhi'1
HAVING filters groups after GROUP BY; can use aggregates, e.g. GROUP BY DeptID HAVING COUNT(*) > 11

Write the outputs of three queries on EMPLOYEE: (a) SELECT COUNT(DISTINCT City) FROM EMPLOYEE; (b) SELECT MAX(Salary) - MIN(Salary) FROM EMPLOYEE; (c) SELECT Name FROM EMPLOYEE WHERE Name LIKE '%a'; [3 marks] (3 marks)

StepMarks
(a) 4 (Delhi, Mumbai, Chennai, Pune)1
(b) 61000 - 38000 = 230001
(c) Empty set: no name ends with a1

Write queries: (i) display names of employees of the Sales department with their city (ii) display each DeptID with the number of employees (iii) increase the salary of employee E02 by 2000 (iv) add a column Email of up to 30 characters. [4 marks] (4 marks)

StepMarks
(i) SELECT Name, City FROM EMPLOYEE E, DEPT D WHERE E.DeptID = D.DeptID AND DName = 'Sales';1
(ii) SELECT DeptID, COUNT(*) FROM EMPLOYEE GROUP BY DeptID;1
(iii) UPDATE EMPLOYEE SET Salary = Salary + 2000 WHERE EmpID = 'E02';1
(iv) ALTER TABLE EMPLOYEE ADD Email VARCHAR(30);1

(a) Write the command to create DEPT with DeptID as primary key and DName not null. (b) Find the degree and cardinality of EMPLOYEE × DEPT. (c) Write a natural-join query showing Name and Location for employees earning above 50000, and its output. [1 + 1 + 3 = 5 marks] (5 marks)

StepMarks
(a) CREATE TABLE DEPT (DeptID CHAR(2) PRIMARY KEY, DName VARCHAR(15) NOT NULL, Location VARCHAR(15));1
(b) Degree 9, cardinality 241
(c) SELECT Name, Location FROM EMPLOYEE NATURAL JOIN DEPT WHERE Salary > 50000;1.5
(c) Output: Asha Rao Delhi; Meena Iyer Delhi1.5

Where students lose marks in Structured Query Language (SQL)

The slips examiners see most often, and what to write instead.

  • Mistake: Using an aggregate in WHERE

    WHERE runs before rows are grouped, so aggregates are not available there.

    Wrong working

    SELECT DeptID FROM EMPLOYEE WHERE COUNT(*) > 1 GROUP BY DeptID;

    Right working

    SELECT DeptID FROM EMPLOYEE GROUP BY DeptID HAVING COUNT(*) > 1;

    Do this: Conditions on SUM, COUNT, AVG, MAX or MIN go in HAVING.

    Usually costs: 1 mark

  • Mistake: Writing = NULL instead of IS NULL

    NULL is unknown, so a comparison with = is never true.

    Wrong working

    SELECT Name FROM EMPLOYEE WHERE Salary = NULL;

    Right working

    SELECT Name FROM EMPLOYEE WHERE Salary IS NULL;

    Do this: Always IS NULL / IS NOT NULL.

    Usually costs: 1 mark

  • Mistake: Counting NULL in AVG or COUNT(column)

    Aggregates skip NULL; only COUNT(*) counts every row.

    Wrong working

    AVG(Salary) = 241000 / 6 = 40166.67

    Right working

    AVG(Salary) = 241000 / 5 = 48200

    Do this: Cross out NULL rows before computing any aggregate except COUNT(*).

    Usually costs: 1 mark in an output question

  • Mistake: Leaving out quotes around text or dates, or quoting numbers carelessly

    City = Delhi is read as a column name; dates need 'YYYY-MM-DD' in quotes.

    Wrong working

    WHERE City = Delhi AND DOJ > 2020-01-01

    Right working

    WHERE City = 'Delhi' AND DOJ > '2020-01-01'

    Do this: Quote every text and date literal; leave numbers unquoted.

    Usually costs: Half to 1 mark

  • Mistake: Ambiguous column in a join

    DeptID exists in both tables, so SELECT DeptID ... FROM EMPLOYEE, DEPT is ambiguous.

    Wrong working

    SELECT Name, DeptID, DName FROM EMPLOYEE, DEPT WHERE DeptID = DeptID;

    Right working

    SELECT Name, E.DeptID, DName FROM EMPLOYEE E, DEPT D WHERE E.DeptID = D.DeptID;

    Do this: Qualify common columns with the table name or an alias.

    Usually costs: 1 mark

  • Mistake: Confusing DELETE and DROP

    DELETE removes rows (DML); DROP TABLE removes the table itself (DDL).

    Do this: To empty a table keep its structure: DELETE FROM T; to remove the table: DROP TABLE T; to remove a column: ALTER TABLE T DROP col.

    Usually costs: 1 mark

NCERT exercise guide: Structured Query Language (SQL)

What each exercise in the NCERT chapter tests, how to approach it, and a question of the same kind solved step by step. Questions here are written in our own words.

NCERT Chapter 9 exercises: creating and altering tables

What it tests: CREATE DATABASE, CREATE TABLE with constraints, DESCRIBE, ALTER TABLE, INSERT.

How to approach it: Choose a type and constraint for each column first; then write one statement per task.

Same kind as the CREATE/ALTER exercises

Create a database School, open it, create STUDENT(RollNo int primary key, Name varchar(20) not null, Phone char(10) unique), and later remove the primary key.

Show the solutionSolution
  1. CREATE DATABASE School;
    USE School;
    CREATE TABLE STUDENT (
      RollNo INT PRIMARY KEY,
      Name VARCHAR(20) NOT NULL,
      Phone CHAR(10) UNIQUE
    );
    ALTER TABLE STUDENT DROP PRIMARY KEY;

Answer: The four statements above (CREATE DATABASE, USE, CREATE TABLE, ALTER TABLE).

NCERT Chapter 9 exercises: queries with SELECT

What it tests: WHERE with operators, IN, BETWEEN, LIKE, NULL, DISTINCT, ORDER BY, aggregates and GROUP BY.

How to approach it: Translate each English phrase: 'each' → GROUP BY; 'more than n' on a count → HAVING; 'in order' → ORDER BY.

Same kind as the GROUP BY exercises

Display the average salary of each department and the number of employees in it.

Show the solutionSolution
  1. SELECT DeptID, COUNT(*), AVG(Salary) FROM EMPLOYEE GROUP BY DeptID;
  2. D1: 2 employees, (52000 + 61000) / 2 = 56500. D2: 2 employees, AVG over the one non-NULL salary = 38000. D3: 2 employees, 45000.

Answer:

DeptIDCOUNT(*)AVG(Salary)
D1256500
D2238000
D3245000
(MySQL shows AVG as 56500.0000 and so on.)

NCERT Chapter 9 exercises: queries on two tables

What it tests: Cartesian product, equi-join and natural join (set operations in NCERT are not in the 2026-27 syllabus).

How to approach it: Join on the common column; qualify names that appear in both tables.

Same kind as the join exercises

Display the names, department names and locations of employees who work in a department located in their own city.

Show the solutionSolution
  1. SELECT Name, DName, Location FROM EMPLOYEE, DEPT
    WHERE EMPLOYEE.DeptID = DEPT.DeptID AND City = Location;
  2. Asha (D1 Sales, Delhi; lives in Delhi) and Ravi (D2 Finance, Mumbai; lives in Mumbai) qualify; Meena (Chennai vs Delhi), Joy (Delhi vs Bengaluru), Kiran (Pune vs Mumbai) and Farah (Mumbai vs Bengaluru) do not.

Answer:

NameDNameLocation
Asha RaoSalesDelhi
Ravi KumarFinanceMumbai

Practice questions on Structured Query Language (SQL)

Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.

  1. Q1 · 1 mark · MCQBoard level

    Which of these is a DML command? (a) CREATE (b) ALTER (c) UPDATE (d) DROP

    Hint

    It changes data, not structure.

    Answer

    (c) UPDATE

  2. Q2 · 1 mark · Very short answerBoard level

    Write the output of SELECT Name FROM EMPLOYEE WHERE City IN ('Delhi', 'Pune') AND Salary IS NOT NULL;

    Hint

    Kiran is in Pune but has NULL salary.

    Answer

    Asha Rao, Joy Das

  3. Q3 · 1 mark · Very short answerBoard level

    Write the command to add a primary key on EmpID if EMPLOYEE was created without one, and the command to show the table's structure.

    Hint

    ALTER and DESCRIBE.

    Answer

    ALTER TABLE EMPLOYEE ADD PRIMARY KEY (EmpID); DESCRIBE EMPLOYEE;

  4. Q4 · 2 marks · Short answerHarder board level

    Write the output of SELECT DName, COUNT(*) FROM EMPLOYEE E, DEPT D WHERE E.DeptID = D.DeptID GROUP BY DName ORDER BY DName;

    Hint

    HR has no employees, so it does not appear.

    Answer
    DNameCOUNT(*)
    Finance2
    IT2
    Sales2
  5. Q5 · 1 mark · Assertion-reasonHOTS / competency

    Assertion (A): SELECT * FROM EMPLOYEE NATURAL JOIN DEPT; displays DeptID only once.
    Reason (R): A natural join joins on columns with the same name and removes the duplicate column.

    Hint

    Compare with an equi-join.

    Answer

    (a) Both A and R are true and R is the correct explanation of A.

  6. Q6 · 2 marks · Short answerHarder board level

    Write queries to (i) display names and annual salary (Salary × 12) as 'Annual' of employees of D2 and D3 (ii) display the total salary of each city in descending order of total.

    Hint

    Alias; GROUP BY with ORDER BY on the aggregate.

    Answer

    (i) SELECT Name, Salary * 12 AS Annual FROM EMPLOYEE WHERE DeptID IN ('D2', 'D3');
    (ii) SELECT City, SUM(Salary) FROM EMPLOYEE GROUP BY City ORDER BY SUM(Salary) DESC;

  7. Q7 · 2 marks · Short answerHOTS / competency

    Write the output of SELECT Name, Salary FROM EMPLOYEE ORDER BY Salary; and explain where Kiran Shah appears.

    Hint

    MySQL puts NULL first in ascending order.

    Answer

    Kiran Shah NULL; Ravi Kumar 38000; Joy Das 45000; Farah Khan 45000; Asha Rao 52000; Meena Iyer 61000. NULL sorts first in ascending order in MySQL (the two 45000 rows may appear in either order).

  8. Q8 · 2 marks · Short answerBoard level

    Differentiate between char(n) and varchar(n) with an example of when to use each.

    Hint

    Fixed vs variable.

    Answer

    char(n) stores exactly n characters (padding shorter values), good for fixed-length codes such as EmpID 'E01' or PIN codes. varchar(n) stores only the characters entered up to n, good for names and addresses whose lengths vary.

  9. Q9 · 2 marks · Short answerHarder board level

    The department D1 moves to Gurugram, and all its employees get a 5% raise. Write the two statements.

    Hint

    Two tables are updated.

    Answer

    UPDATE DEPT SET Location = 'Gurugram' WHERE DeptID = 'D1';
    UPDATE EMPLOYEE SET Salary = Salary * 1.05 WHERE DeptID = 'D1';

  10. Q10 · 3 marks · Short answerHOTS / competency

    Write a query to display, for each department name, the highest salary, only where that highest salary is at least 45000, using an equi-join. Give its output.

    Hint

    GROUP BY DName with HAVING MAX(Salary) >= 45000.

    Answer

    SELECT DName, MAX(Salary) FROM EMPLOYEE E, DEPT D WHERE E.DeptID = D.DeptID GROUP BY DName HAVING MAX(Salary) >= 45000;
    Output: IT 45000; Sales 61000 (Finance's maximum is 38000, so it is left out; group order may differ).

Test your recall

Answer in your head or on paper first, then open the card. Pulling an answer out of memory makes it stick far better than re-reading.

  1. 1.Is ALTER a DDL or DML command?Show answer

    DDL.

  2. 2.What is the difference between char(10) and varchar(10)?Show answer

    char(10) always uses 10 characters (padded); varchar(10) uses only as many as stored, up to 10.

  3. 3.Which wildcard in LIKE matches exactly one character?Show answer

    _ (underscore).

  4. 4.What does COUNT(Salary) return for EMPLOYEE?Show answer

    5 (Kiran's NULL is skipped).

  5. 5.Which clause filters groups?Show answer

    HAVING.

  6. 6.What is the cardinality of the cartesian product of a 6-row and a 4-row table?Show answer

    24.

  7. 7.Write the command to remove the primary key of DEPT.Show answer

    ALTER TABLE DEPT DROP PRIMARY KEY;

  8. 8.How does a natural join differ from an equi-join in its output?Show answer

    A natural join shows the common column once; an equi-join with SELECT * shows it twice.

  9. 9.Which command shows the structure of a table?Show answer

    DESCRIBE table_name; (or DESC).

Best order to study it

  1. DDL vs DML, data types and constraints; CREATE TABLE.
  2. Database and table commands; ALTER TABLE variants; DROP vs DELETE.
  3. INSERT, UPDATE, DELETE with and without WHERE.
  4. SELECT with operators, aliases, DISTINCT; then IN, BETWEEN, LIKE, NULL.
  5. ORDER BY with ties and NULL.
  6. Aggregates and NULL; GROUP BY and HAVING.
  7. Cartesian product, equi-join, natural join; practise four-query sets on two tables.

Quick revision checklist

The night before the exam: can you do each of these without looking?

  • DDL: CREATE, ALTER, DROP. DML: INSERT, UPDATE, DELETE, SELECT.
  • char(n) fixed; varchar(n) variable; int; float; date 'YYYY-MM-DD'.
  • NOT NULL, UNIQUE (NULL allowed), PRIMARY KEY (unique + not NULL).
  • ALTER TABLE: ADD col; DROP col; ADD PRIMARY KEY (col); DROP PRIMARY KEY.
  • UPDATE/DELETE without WHERE affect all rows.
  • IN for lists; BETWEEN inclusive; LIKE with % and _; IS NULL.
  • ORDER BY default ASC; extra columns break ties.
  • Aggregates ignore NULL except COUNT(*); MySQL shows AVG with 4 decimals.
  • WHERE before GROUP BY; HAVING after; ORDER BY last.
  • Cartesian: degree adds, cardinality multiplies; equi-join on =; natural join shows the common column once.

Structured Query Language (SQL): frequently asked questions

Which SQL functions are in the CBSE Class 12 syllabus for 2026-27?

Only the aggregate functions MAX, MIN, AVG, SUM and COUNT. Single-row maths, string and date functions described in NCERT are not in the 2026-27 list.

Which joins must I know?

Cartesian product on two tables, equi-join and natural join. Other join types are not in the syllabus.

Does the board use MySQL syntax?

Yes. The syllabus and sample papers use MySQL, so use its commands such as DESCRIBE, SHOW TABLES and ALTER TABLE ... DROP PRIMARY KEY.

What is the difference between WHERE and HAVING?

WHERE filters individual rows before grouping and cannot use aggregate functions. HAVING filters groups after GROUP BY and can use aggregates such as COUNT(*) > 1.

Why does AVG ignore NULL?

NULL means the value is unknown, so it is left out of both the total and the count. Use COUNT(*) if you want to count rows including NULLs.

How many marks does SQL carry?

SQL is part of Unit 3 (20 of 70 theory marks). In the 2026-27 sample paper it appears in MCQs, 2-mark questions and the 4-mark query question of Section D, and four SQL queries carry 4 marks in the practical exam.

Based on: CBSE Computer Science (083) syllabus 2026-27; NCERT Computer Science, Textbook for Class XII (Reprint 2026-27)