These Notes for Class 12 Computer Science Chapter 9 Structured Query Language (SQL) give you a fast, concept-first revision of the whole chapter, built on the latest 2026-27 CBSE syllabus. They explain DDL, DML and DQL statements, data types and constraints, the SELECT query with all its clauses, and the single-row and aggregate functions that the board paper tests every year on MySQL.
- Every SQL statement in one place with its job, plain syntax, a worked example, and the exact output MySQL prints, so you can revise the chapter in one sitting.
- Full coverage of CREATE, ALTER, DROP, INSERT, UPDATE, DELETE, SELECT, GROUP BY, HAVING, JOIN and the functions the CBSE board paper asks directly.
- Notes aligned with the 2026-27 CBSE Class 12 Computer Science syllabus and useful for CUET and JEE-level programming questions; no NEET references because Computer Science is not a medical subject.

These Collegedunia revision notes are curated by Computer Science subject experts, mapped to the 2026-27 NCERT textbook, and refined against the last five years of CBSE Class 12 Computer Science board papers.
Student Feedback: What 12,400 students told us about this chapter
71% of Class 12 students said SQL felt easy in class but cost them marks in output and query questions during the exam. 3 out of 5 students told us a single table that lined up each clause with one short example helped them lock the chapter the night before the board paper.
Toppers found that typing 8 to 10 queries by hand fixed the syntax faster than re-reading the theory, and the average student spent 2 to 3 hours on these notes across the first read and the final revision.
Source: 2026-27 Class 12 Computer Science student poll. Sample of 12,400 students from CBSE schools across 14 states, conducted before the 2026 boards.
What the Notes for Class 12 Computer Science Chapter 9 Structured Query Language (SQL) Cover
This chapter answers one question: how do you store data in a relational database and then pull out exactly the rows you need? These notes keep the NCERT order but compress it into revision-ready blocks, so you can read the whole chapter in one sitting and recall every statement fast in the exam.
- What SQL is: the query language for relational databases like MySQL, split into DDL, DML and DQL statements by the job each one does.
- Data types and constraints: CHAR, VARCHAR, INT, FLOAT, DATE, plus NOT NULL, UNIQUE, DEFAULT, PRIMARY KEY and FOREIGN KEY.
- The full set of statements: CREATE, ALTER, DROP, INSERT, UPDATE, DELETE and the SELECT query with all its clauses, each with a worked example and output.
- Functions and joins: single-row and aggregate functions, GROUP BY and HAVING, plus the Cartesian product and JOIN across two tables.
Source: Magnet Brains on YouTube

SQL and the Relational Database in Class 12 Computer Science
Before the statements, fix the idea. SQL (Structured Query Language) is the language a relational database understands. In a Relational Database Management System (RDBMS) like MySQL, data sits in tables called relations, and SQL is what you type at the mysql> prompt to create those tables, fill them, change them and read them back.
SQL is popular because its statements read like plain English, so they are easy to learn. You tell SQL what you want, not how to fetch it, and the database works out the steps. Two small rules matter from the start: SQL is not case sensitive, so select and SELECT are the same, and every statement ends with a semicolon.
| Group | Full form | What it does | Statements |
|---|---|---|---|
| DDL | Data Definition Language | Defines or changes the structure of tables | CREATE, ALTER, DROP, DESCRIBE |
| DML | Data Manipulation Language | Works on the data inside the rows | INSERT, UPDATE, DELETE |
| DQL | Data Query Language | Reads data from the tables | SELECT |
Knowing the group of a statement tells you what it touches. A DDL statement changes the structure, a DML statement changes the rows, and DQL only reads. This single split organises the whole chapter, so learn it first.
SELECT query runs quietly behind the scenes to pull your data out of a huge table. The same SQL you learn here powers those systems at scale.
Data Types and Constraints in MySQL
A relation is made of attributes, which are its columns, and every attribute must be given a data type that says what kind of value it can hold. You can also attach constraints, which are rules that limit the values an attribute may take. Choosing the right type and constraint when you create the table keeps the data correct for the life of the database.
| Data type | Meaning |
|---|---|
CHAR(n) | Fixed-length text of length n. Short data is padded with spaces, so it always uses n characters of space. |
VARCHAR(n) | Variable-length text up to n characters. It uses only as much space as the actual string needs. |
INT | A whole number, 4 bytes per value. Use BIGINT for very large numbers. |
FLOAT | A number with a decimal point. |
DATE | A date in 'YYYY-MM-DD' format. |
Constraints are restrictions on the values an attribute can hold. They are not compulsory for every column, but they keep the data correct. The most tested ones are NOT NULL (no empty value), UNIQUE (every value different), DEFAULT (a value used when none is supplied), PRIMARY KEY and FOREIGN KEY.
CHAR(10) always reserves space for 10 characters even if the value is just 'city', filling the rest with spaces, while VARCHAR(10) stores only the 4 needed. Use CHAR for fixed-length codes and VARCHAR for names of varying length.
A PRIMARY KEY is the combination of two simpler ideas: it is both NOT NULL and UNIQUE. So a primary key column can never be empty and can never repeat a value, which is why it can uniquely identify each row. A FOREIGN KEY is an attribute in one table that points to the primary key of another table, and it is how related data in separate tables stays connected.
DDL Statements in Class 12 Computer Science: CREATE, ALTER, DROP
Before storing any data you must first define the schema, which means creating the database, creating its tables, choosing data types and constraints, and sometimes changing this structure later. All of these jobs use Data Definition Language statements. A database is a container for tables, so you make the database first, select it with USE, then create tables inside it.
CREATE DATABASE StudentAttendance;
USE StudentAttendance;
CREATE TABLE STUDENT(
RollNumber INT,
SName VARCHAR(20),
SDateofBirth DATE,
GUID CHAR(12),
PRIMARY KEY (RollNumber));
Query OK, 1 row affected (0.02 sec)
Database changed
Query OK, 0 rows affected (0.91 sec)
The number of columns in a relation is called its degree, and the number of rows is its cardinality. Inside CREATE TABLE each attribute definition is separated by a comma, and the whole statement ends with one semicolon. A missing comma between two columns is the most frequent reason a CREATE fails.
DESCRIBE and ALTER TABLE
After making a table you often want to confirm its structure. The DESCRIBE statement (you may shorten it to DESC) shows each field, its type, whether it allows NULL, and its key role. The ALTER TABLE statement then handles every later schema change: adding a column, changing a data type, adding a constraint, or dropping something.
DESCRIBE STUDENT;
ALTER TABLE GUARDIAN ADD PRIMARY KEY (GUID);
ALTER TABLE STUDENT ADD FOREIGN KEY(GUID) REFERENCES GUARDIAN(GUID);
ALTER TABLE GUARDIAN ADD income INT;
ALTER TABLE STUDENT MODIFY SName VARCHAR(20) NOT NULL;
ALTER TABLE GUARDIAN DROP income;
When you use MODIFY to add a NOT NULL or a DEFAULT, you must repeat the data type, as in MODIFY SName VARCHAR(20) NOT NULL. Leaving out VARCHAR(20) causes an error. Each form of ALTER changes structure only; it never touches the data inside the existing rows.
ALTER changes the structure, DROP TABLE or DROP DATABASE destroys the whole object permanently, and DELETE (a DML statement) removes only rows. A DROP cannot be undone, so use it with care.
DML Statements: INSERT, UPDATE and DELETE
A freshly created table has structure but no data. To put records in we use INSERT, to change existing records we use UPDATE, and to remove records we use DELETE. These three statements form the Data Manipulation Language, and they all work on the rows, never the structure.
- INSERT INTO adds new rows. Give values for every column in order, or name the columns when you fill only some of them.
- UPDATE changes values in existing rows, using
SETto name the columns andWHEREto pick the rows. - DELETE removes whole rows, with
WHEREchoosing which rows go, while keeping the table itself.
INSERT INTO GUARDIAN
VALUES (444444444444, 'Amit Ahuja', 5711492685, 'G-35, Delhi');
UPDATE GUARDIAN
SET GAddress = 'WZ-68, Azad Avenue, MP', GPhone = 9010810547
WHERE GUID = 466444444666;
DELETE FROM STUDENT WHERE RollNumber = 2;
Query OK, 1 row affected (0.06 sec)
Rows matched: 1 Changed: 1 Warnings: 0
Query OK, 1 row affected (0.06 sec)
If a table has a foreign key, the row it points to must already exist in the referenced table, so you insert into the referenced table before the referencing one. Text and date values are always wrapped in single quotes.
WHERE clause in UPDATE or DELETE. Writing UPDATE STUDENT SET GUID = 101010101010; with no WHERE gives every single student the same guardian, and DELETE FROM STUDENT; empties the whole table. The WHERE clause is what limits the change to the rows you mean.
Querying Data with SELECT in Class 12 Computer Science
The whole point of storing data is to fetch it back in any shape you need. The SELECT statement, the only DQL statement, retrieves data and shows it as a table. SELECT is the most used and most tested statement in the chapter. A SELECT names the columns to show, the FROM clause names the table, and the optional WHERE clause filters the rows. Use * to show all columns.
SELECT SName, SDateofBirth FROM STUDENT WHERE RollNumber = 1;
+--------------+--------------+
| SName | SDateofBirth |
+--------------+--------------+
| Atharv Ahuja | 2003-05-15 |
+--------------+--------------+
1 row in set (0.03 sec)
A SELECT must always have a FROM clause to say which table to read, while the WHERE clause is optional; without it, every row is returned. The WHERE clause can use relational operators and the logical operators AND, OR, NOT, plus two special operators: BETWEEN for a range and IN for a set of values.
| Clause / Operator | What it does |
|---|---|
BETWEEN a AND b | True when the value is in the range a to b, both ends included. |
IN (list) | True when the value matches any value in the list. |
IS NULL | True when the value is missing (NULL). |
LIKE pattern | True when the text matches a wild-card pattern (% for many characters, _ for one). |
DISTINCT | Removes repeated values so each appears once. |
ORDER BY | Sorts the output, ascending by default, descending with DESC. |
The DISTINCT clause removes repeats, the AS keyword renames a column in the output (an alias), and you can put arithmetic right in the SELECT list to make a calculated column. The ORDER BY clause sorts the output, and you can sort by a second column to break ties.
SELECT DISTINCT DeptId FROM EMPLOYEE;
SELECT EName AS Name, Salary*12 AS 'Annual Income' FROM EMPLOYEE;
SELECT * FROM EMPLOYEE WHERE Salary BETWEEN 20000 AND 50000;
SELECT * FROM EMPLOYEE WHERE Ename LIKE 'K%';
SELECT PCode, PName, UPrice FROM Product
ORDER BY PName DESC, UPrice ASC;
= cannot test it. You can never write WHERE Bonus = NULL; you must use IS NULL or IS NOT NULL. Also remember that BETWEEN 20000 AND 50000 includes both 20000 and 50000.
SQL Functions and GROUP BY in Class 12 Computer Science
A function performs a task and returns a value. SQL functions split into two families: single-row functions, which work on one value at a time and give one result per row, and aggregate functions, which work on a whole group of rows and return a single summary value. Combined with the GROUP BY clause, these let you summarise large tables in one query.

Single-row functions come in three categories: numeric, string, and date and time. The math functions are short but high-scoring, so learn the exact output of each one. The table below lists the most asked single-row functions.
| Function | Returns | Example to output |
|---|---|---|
POWER(X,Y) | X raised to the power Y (also POW) | POW(2,3) to 8 |
ROUND(N,D) | N rounded to D decimal places | ROUND(342.9234,-1) to 340 |
LENGTH(S) | Number of characters in the string | LENGTH('Informatics Practices') to 21 |
LEFT / RIGHT / MID | Characters from the start, end, or a position | LEFT('INDIA',3) to IND |
MONTHNAME(D) | The name of the month from a date | MONTHNAME('2003-11-28') to November |
SELECT POW(2,3);
SELECT ROUND(342.9234,-1);
SELECT LEFT('INDIA',3), RIGHT('Computer Science',4),
MID('Informatics',3,4), SUBSTR('Practices',3);
POW(2,3) -> 8
ROUND(342.9234,-1) -> 340
LEFT('INDIA',3) -> IND
RIGHT('Computer Science',4) -> ence
MID('Informatics',3,4) -> form
SUBSTR('Practices',3) -> actices
ROUND rounds to the left of the decimal point, so ROUND(342.9234,-1) gives 340 because it rounds to the nearest 10. Examiners love this trick in output questions.
Aggregate functions and GROUP BY
Aggregate functions, also called multiple-row functions, work on a set of rows and return one summary value. The five you must know are MAX, MIN, AVG, SUM and COUNT. The GROUP BY clause collects rows that share the same value in a column into groups, then applies an aggregate to each group, and the HAVING clause adds a condition on those groups.
SELECT CustID, COUNT(*) AS 'Number of Cars'
FROM SALE GROUP BY CustID;
SELECT CustID, COUNT(*) FROM SALE
GROUP BY CustID HAVING COUNT(*) > 1;
+--------+----------+
| CustID | COUNT(*) |
+--------+----------+
| C0001 | 2 |
| C0002 | 2 |
+--------+----------+
2 rows in set (0.30 sec)
WHERE filters individual rows before grouping, while HAVING filters whole groups after grouping. So a condition on an aggregate like COUNT(*) > 1 can only go in HAVING, never in WHERE. Also note COUNT(*) counts every row but COUNT(column) skips NULL values.
Working with Two Relations: Cartesian Product and JOIN
So far every query read a single table. Real databases spread data across many related tables, so you must learn to combine two of them in one query. SQL gives you set operations, the Cartesian product, and the JOIN. These appear in board papers as both query-writing and concept questions, so understand the difference clearly.
The Cartesian product pairs every row of the first table with every row of the second, whether or not they are related. In SQL you get it by listing two tables in the FROM clause with a comma. The result has a degree equal to the sum of the two degrees, and a cardinality equal to the product of the two cardinalities.
| Operation | What it keeps |
|---|---|
| UNION | All rows from both tables; rows in both are shown once. |
| INTERSECT | Only the rows common to both tables. |
| MINUS | Rows in the first table that are not in the second. |
| Cartesian product | Every row of A paired with every row of B. |
| JOIN | Only the rows that satisfy a matching condition. |
A JOIN combines rows from two tables on a matching condition, usually the primary key of one table equal to the foreign key of another. Unlike the Cartesian product, a JOIN keeps only the rows that satisfy the condition. There are three common ways to write the same join.
-- condition in WHERE
SELECT * FROM UNIFORM U, COST C WHERE U.UCode = C.UCode;
-- explicit JOIN ... ON
SELECT * FROM UNIFORM U JOIN COST C ON U.UCode = C.UCode;
-- NATURAL JOIN removes the repeated column
SELECT * FROM UNIFORM NATURAL JOIN COST;
The first two give the same output with the common column UCode shown twice. The NATURAL JOIN gives the same rows but shows the common column only once, because the repeated column adds no new information.
How to Use the SQL Notes PDF for Board Revision
The SQL chapter is syntax-heavy and query-heavy, so it rewards practice over reading. The best approach is two passes: one for the statements and clauses, one for writing queries by hand on the sample tables.
First pass: statements and clauses
Read these notes and fix the DDL, DML, DQL split, the data types and constraints, and the order of clauses in a full SELECT. Say one line of meaning aloud for CREATE, INSERT, SELECT, UPDATE and DROP so the jobs stick before you start writing queries.
Second pass: write queries by hand
Take the MOVIE and Product tables from the NCERT exercise and write the queries on paper: a calculated column with AS, a filter with BETWEEN, a group count with GROUP BY, and an output question on ROUND and the string functions. That single exercise covers the most common 3-mark and 4-mark board questions on this chapter.
JEE and CUET angle
For students preparing competitive exams, SQL appears in CUET Computer Science and in database rounds of programming tests. The clause order in a SELECT, the WHERE-versus-HAVING rule, and the difference between a Cartesian product and a JOIN are exactly the kind of concept questions those papers reuse, so this revision doubles as competitive prep.
Previous Year Question Trends from the SQL Chapter
The SQL chapter is one of the most heavily tested in CBSE board papers, usually as a query-writing block on a given table plus one or two output questions on the functions. The table below maps the asked question types across recent board papers, so your revision can target the high-frequency areas.
| Year | Question type asked | Marks |
|---|---|---|
| 2025 | Write SQL queries on a given table using GROUP BY and ORDER BY; predict function output | 4 + 1 |
| 2024 | Create a table with constraints; write SELECT with WHERE and BETWEEN | 2 + 2 |
| 2023 | Differentiate ALTER and UPDATE, DELETE and DROP; output of ROUND and string functions | 2 + 2 |
| 2022 | Define Cartesian product; write a JOIN query across two tables | 1 + 3 |
| 2021 | Aggregate functions with GROUP BY and HAVING; difference from single-row functions | 2 + 2 |
Also Check: The full set of CBSE board paper questions for this chapter, with step-by-step answers, is included in the downloadable PDF above, updated for the 2026-27 cycle.
Other Resources for Class 12 Computer Science Chapter 9 Structured Query Language (SQL)
Pair these revision notes with the matching NCERT Solutions, handwritten notes and the official NCERT book chapter. All resources for Class 12 Computer Science Chapter 9 Structured Query Language (SQL) are linked below.
| Resource | What it covers | Open |
|---|---|---|
| Notes | Concept-first revision notes on DDL, DML, DQL, the SELECT query, functions and joins, with worked queries and output. | You are here |
| NCERT Solutions | Step-by-step answers to all 8 exercise questions, with an Expert Solution for each. | Class 12 Computer Science Chapter 9 NCERT Solutions |
| Handwritten Notes | Scanned-style handwritten pages for last-minute board revision. | Class 12 Computer Science Chapter 9 Handwritten Notes |
| NCERT Book PDF | Official NCERT Computer Science Chapter 9 Structured Query Language (SQL) textbook in PDF form. | Class 12 Computer Science Chapter 9 NCERT Book PDF |
Notes for Class 12 Computer Science: All Chapters
Related Links: Use the table below to open the revision notes for the other chapters of Class 12 Computer Science. Every chapter ships with the same concept-first notes style, full PDF download, and revision FAQ.
| Chapter | Notes link |
|---|---|
| Chapter 1 | Exception Handling in Python Notes |
| Chapter 2 | File Handling in Python Notes |
| Chapter 3 | Stack Notes |
| Chapter 4 | Queue Notes |
| Chapter 5 | Sorting Notes |
| Chapter 6 | Searching Notes |
| Chapter 7 | Understanding Data Notes |
| Chapter 8 | Database Concepts Notes |
| Chapter 9 | Structured Query Language (SQL) Notes (You are here) |
| Chapter 10 | Computer Networks Notes |
| Chapter 11 | Data Communication Notes |
| Chapter 12 | Security Aspects Notes |
Notes Class 12 Computer Science Chapter 9 Structured Query Language (SQL) FAQs
Ques. What does Chapter 9 Structured Query Language (SQL) cover in Class 12 Computer Science?
Ans. Chapter 9 covers how to create, fill, change and query a relational database using SQL on MySQL. The notes explain the three groups of statements, which are DDL for structure, DML for data and DQL for reading, the common data types like CHAR, VARCHAR, INT, FLOAT and DATE, and the constraints NOT NULL, UNIQUE, DEFAULT, PRIMARY KEY and FOREIGN KEY. They then work through CREATE, ALTER, DROP, INSERT, UPDATE, DELETE and the SELECT query with WHERE, DISTINCT, ORDER BY, BETWEEN, IN, LIKE and IS NULL. The chapter ends with single-row and aggregate functions, GROUP BY and HAVING, the Cartesian product and the JOIN, all aligned with the 2026-27 CBSE syllabus.
Ques. What is the difference between DDL, DML and DQL in SQL?
Ans. SQL statements are sorted into three groups by the job they do. DDL, the Data Definition Language, defines or changes the structure of tables and includes CREATE, ALTER, DROP and DESCRIBE. DML, the Data Manipulation Language, works on the data inside the rows and includes INSERT, UPDATE and DELETE. DQL, the Data Query Language, reads data, and its one statement is SELECT. The simple test is this: if a statement changes the structure it is DDL, if it changes the rows it is DML, and if it only reads it is DQL. Knowing the group of a statement tells you exactly what it touches.
Ques. What is the difference between a primary key and a foreign key in MySQL?
Ans. A primary key is a column whose value uniquely identifies each row in a table. It is the combination of two constraints, NOT NULL and UNIQUE, so a primary key column can never be empty and can never repeat a value. A foreign key is an attribute in one table that points to the primary key of another table, and it is how related data in two separate tables stays connected. For a foreign key to work, the referenced table must already exist, the referenced attribute must be its primary key, and the data type and size of the two attributes must match. The primary key keeps a table's own rows unique, while the foreign key links one table to another.
Ques. What is the difference between WHERE and HAVING in a SELECT statement?
Ans. Both WHERE and HAVING add conditions, but at different stages. WHERE filters individual rows before any grouping happens, so it works on the values in the columns of each row. HAVING filters whole groups after GROUP BY has formed them, so it works on the result of an aggregate function. This is why a condition like COUNT(*) greater than 1 can only go in HAVING and never in WHERE, because the count does not exist until the rows are grouped. A useful rule is WHERE before grouping, HAVING after grouping. The legal clause order in a full SELECT is SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY.
Ques. What is the difference between a Cartesian product and a JOIN in SQL?
Ans. A Cartesian product pairs every row of the first table with every row of the second, whether or not they are related, so its result has a degree equal to the sum of the two degrees and a cardinality equal to the product of the two cardinalities. You get it by listing two tables in the FROM clause with a comma. A JOIN combines rows from two tables on a matching condition, usually the primary key of one equal to the foreign key of the other, so it keeps only the rows that satisfy the condition. A JOIN is really a Cartesian product with a filter applied. To join N tables on equality you need N minus 1 joins, and a NATURAL JOIN shows the common column only once.
Ques. What is the difference between single-row functions and aggregate functions?
Ans. Single-row functions, also called scalar functions, work on one value at a time and return one result per row. They come in three categories: numeric functions like POWER, ROUND and MOD, string functions like UCASE, LENGTH, LEFT, RIGHT and MID, and date functions like MONTHNAME, YEAR and DAYNAME. Aggregate functions, also called multiple-row functions, work on a whole group of rows and return a single summary value. The five aggregate functions are MAX, MIN, AVG, SUM and COUNT. The key difference is the number of rows the function reads and returns: a single-row function gives one output per input row, while an aggregate function reads many rows and gives one summary value, often used with GROUP BY.
Ques. How many pages is the Class 12th Computer Science SQL Notes PDF?
Ans. The Structured Query Language (SQL) Notes PDF runs about 22 pages and covers the full chapter in concept-first revision blocks, with worked MySQL queries, sample output, labelled diagrams, function tables, common traps and a one-glance revision strip. The PDF is free to download for the 2026-27 session, and a green Handwritten Notes button on this page opens the scanned-style version for last-minute revision.
Ques. Are these Notes for Class 12 Computer Science Chapter 9 aligned with the 2026-27 syllabus?
Ans. Yes. This page reflects the current 2026-27 CBSE syllabus for Class 12 Computer Science. The Structured Query Language (SQL) chapter is unchanged for the current cycle, and these notes follow the NCERT textbook, covering SQL and the relational database, data types and constraints, the DDL, DML and DQL statements, the SELECT query with all its clauses, single-row and aggregate functions, GROUP BY and HAVING, and the Cartesian product and JOIN. The notes are useful for the CBSE board exam, and the same ideas help with CUET Computer Science and JEE-level programming questions.



Comments