SQL Notes for Class 12 Computer Science (083) | Unit-3 Database Management
Learn every SQL command with clear concepts, syntax, practical examples, and step-by-step explanations in this SQL Notes specially curated for CBSE Class 12 Computer Science (Code 083). these notes are fully aligned with the latest syllabus.
⚠️ Before learning SQL, make sure you have studied Database Concepts, as they form the foundation of everything that follows. If not, use the link below first.
Database Concepts Notes Class 12 CS
Unlike ordinary notes that focus only on theory, these CBSE Class 12 CS SQL notes takes you from basic table creation to complex GROUP BY clauses and JOINS in the exact logical sequence you need to learn it.
It’s your all-in-one resource to ace your board exams, practicals, and last-minute revision without any textbook hopping.
Whether you’re preparing for board exams, practicals, or a quick last-minute revision, these notes serve as your all-in-one resource—no textbook hopping required. ✨
Structured Query Language (SQL)
- Structured Query Language (SQL) is a standard language used to access and manage databases.
- It is a special-purpose programming language used to create tables, store, retrieve, and manipulate data in a database.
Types of SQL Statements
SQL statements are divided into different categories based on their purpose:
- Data Definition Language (DDL)
- Data Manipulation Language (DML)
Data Definition Language (DDL)
- DDL statements are used to define and manage the structure of a database or table.
- They are used to create, modify, and delete tables.
- DDL Statements are: CREATE TABLE, ALTER TABLE, DROP TABLE
Data Manipulation Language (DML)
- DML statements are used to access and manipulate data stored in tables.
- They are used to insert, retrieve, update, and delete data.
- DML Statements are: SELECT, INSERT, UPDATE, DELETE
Data Types
- A Data Type defines the type of value that can be stored in a table column.
- Each value in an SQL database is associated with a specific data type.
| Data Type | Description |
| CHAR(n) | Stores fixed-length text data. |
| VARCHAR(n) | Stores variable-length text data. |
| INT | Stores whole numbers. |
| FLOAT | Stores decimal numbers. |
| DATE | Stores date in YYYY-MM-DD format. |
SQL Constraints
- Constraints are rules or restrictions applied on table attributes (columns).
- They help maintain correct, accurate, and reliable data in a database.
- It is not necessary to apply constraints to every attribute of a table.
- Common SQL constraints are NOT NULL, UNIQUE, DEFAULT, PRIMARY KEY, and FOREIGN KEY.
| Constraint | Description |
| NOT NULL | Ensures that a column cannot have empty or NULL values. |
| UNIQUE | Ensures that all values in a column are unique. |
| DEFAULT | Assigns a default value if no value is provided. |
| PRIMARY KEY | Uniquely identifies each record in a table. |
| FOREIGN KEY | Creates a link between two tables using a primary key of another table. |
Example:
Create the following table:
Table: STUDENT
| Column Name | Data Type | Constraint |
| Adno | NUMERIC(3) | PRIMARY KEY |
| Name | VARCHAR(20) | NOT NULL |
| Class | NUMERIC(2) | — |
| Section | CHAR(1) | — |
| VARCHAR(30) | UNIQUE | |
| City | VARCHAR(20) | DEFAULT ‘Delhi’ |
Solution:
CREATE TABLE STUDENT
(
Adno NUMERIC(3) PRIMARY KEY,
Name VARCHAR(20) NOT NULL,
Class NUMERIC(2),
Section CHAR(1),
Email VARCHAR(30) UNIQUE,
City VARCHAR(20) DEFAULT ‘Delhi’
);
CREATE DATABASE
The CREATE DATABASE statement is used to create a new database.
Example:
CREATE DATABASE School;
USE DATABASE
The USE statement is used to select a database for performing operations.
Example:
USE School;
SHOW DATABASES
The SHOW DATABASES statement is used to display all available databases.
Example:
SHOW DATABASES;
DROP DATABASE
The DROP DATABASE statement is used to permanently delete a database.
Example:
DROP DATABASE School;
SHOW TABLES
The SHOW TABLES statement is used to display all tables in the selected database.
Example:
SHOW TABLES;
CREATE TABLE
The CREATE TABLE statement is used to create a new table.
Syntax:
CREATE TABLE <table name>
(
<column 1> <data type> [constraint],
<column 2> <data type> [constraint],
<column 3> <data type> [constraint]
);
Note: [constraint] is optional.
Example:
CREATE TABLE Student
(
Adno NUMERIC(3) PRIMARY KEY,
Name VARCHAR(20) NOT NULL,
Class NUMERIC(2),
Email VARCHAR(30) UNIQUE,
City VARCHAR(20)
);
DESCRIBE TABLE
The DESCRIBE statement is used to display the structure of a table.
Example:
DESCRIBE Student;
ALTER TABLE
The ALTER TABLE statement is used to change the structure of an existing table. It is used to:
- Add or remove attributes (columns)
- Modify the data type of an existing attribute
- Add or remove constraints from a table
ALTER TABLE (Add Attribute)
The ALTER TABLE … ADD statement is used to add a new column to a table.
Example:
ALTER TABLE Student
ADD Fees NUMERIC(6);
ALTER TABLE (Remove Attribute)
The ALTER TABLE … DROP COLUMN statement is used to remove a column from a table.
Example:
ALTER TABLE Student
DROP COLUMN Fees;
ALTER TABLE (Add PRIMARY KEY)
The ALTER TABLE … ADD PRIMARY KEY statement is used to add a primary key to a table.
Example:
ALTER TABLE Student
ADD PRIMARY KEY(Adno);
ALTER TABLE (Remove PRIMARY KEY)
The ALTER TABLE … DROP PRIMARY KEY statement is used to remove the primary key from a table.
Example:
ALTER TABLE Student
DROP PRIMARY KEY;
INSERT Command
The INSERT statement is used to add new records into a table.
Example:
INSERT INTO Student
VALUES(101, ‘Rahul’, 12);
SELECT Command
The SELECT statement is used to retrieve data from a table.
Example:
SELECT * FROM Student;
DELETE Command
The DELETE statement is used to remove records from a table.
Example:
DELETE FROM Student
WHERE Adno = 101;
Operators in SQL
Mathematical Operators
Used to perform calculations.
| Operator | Meaning |
| + | Addition |
| – | Subtraction |
| * | Multiplication |
| / | Division |
Example:
SELECT Marks + 5 FROM Student;
Relational Operators
Used to compare values.
| Operator | Meaning |
| = | Equal to |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal to |
| <= | Less than or equal to |
| <> | Not equal to |
Example:
SELECT * FROM Student
WHERE Marks > 80;
Logical Operators
Used to combine conditions.
| Operator | Meaning |
| AND | Both conditions must be true |
| OR | Any one condition must be true |
| NOT | Reverses the condition |
Example:
SELECT * FROM Student
WHERE Class = 12 AND Marks > 80;
IN Operator
The IN operator is used to match values from a given list.
Syntax:
SELECT * FROM table_name
WHERE column_name IN(value1, value2);
Example:
SELECT * FROM Student
WHERE Class IN(11, 12);
BETWEEN Operator
The BETWEEN operator is used to select values within a range.
Syntax:
SELECT * FROM table_name
WHERE column_name BETWEEN value1 AND value2;
Example:
SELECT * FROM Student
WHERE Marks BETWEEN 70 AND 90;
NULL
NULL means a value is missing, unknown, or not applicable.
IS NULL
The IS NULL condition is used to check NULL values.
Example:
SELECT * FROM Student
WHERE Marks IS NULL;
IS NOT NULL
The IS NOT NULL condition is used to check non-NULL values.
Example:
SELECT * FROM Student
WHERE Marks IS NOT NULL;
LIKE Operator
The LIKE operator is used for pattern matching.
Syntax:
SELECT * FROM table_name
WHERE column_name LIKE pattern;
Example:
SELECT * FROM Student
WHERE Name LIKE ‘R%’;
Aliasing – Renaming of Column
Aliasing is used to give a temporary name to a column or table.
Syntax:
SELECT column_name AS alias_name
FROM table_name;
Example:
SELECT Name AS Student_Name
FROM Student;
DISTINCT Clause
The DISTINCT clause is used to display unique values.
Example:
SELECT DISTINCT Class
FROM Student;
WHERE Clause
The WHERE clause is used to filter records based on a condition.
Syntax:
SELECT * FROM table_name
WHERE condition;
Example:
SELECT * FROM Student
WHERE Class = 12;
ORDER BY Clause
The ORDER BY clause is used to sort records in ascending or descending order.
Syntax:
SELECT * FROM table_name
ORDER BY column_name ASC/DESC;
Example:
SELECT * FROM Student
ORDER BY Name;
UPDATE Command
The UPDATE statement is used to modify existing records.
Syntax:
UPDATE table_name
SET column_name = value
WHERE condition;
Example:
UPDATE Student
SET Marks = 95
WHERE Adno = 101;
DELETE Command
The DELETE statement is used to remove records from a table.
Syntax:
DELETE FROM table_name
WHERE condition;
Example:
DELETE FROM Student
WHERE Adno = 101;
Aggregate function
- Aggregate functions are used to implement calculation based upon a particular column.
- These functions always return a single value.
- It helps to summarize large volume of data
- Multiple aggregate functions can be used together in a SQL Query by separating them using comma.
| Function | Description | Example |
| SUM(field) | Returns the total sum of values in a column. | SELECT SUM(Marks) FROM Student; Output: 450 |
| AVG(field) | Returns the average value of a column. | SELECT AVG(Marks) FROM Student; Output: 75 |
| MAX(field) | Returns the highest value from a column. | SELECT MAX(Marks) FROM Student; Output: 95 |
| MIN(field) | Returns the smallest value from a column. | SELECT MIN(Marks) FROM Student; Output: 55 |
| COUNT(field) | Counts the total number of values in a column. | SELECT COUNT(Marks) FROM Student; Output: 6 |
Group By
- The GROUP BY clause is used to group rows that have the same values in a column.
- It helps in summarizing large amounts of data into groups.
- Aggregate functions like SUM(), AVG(), COUNT(), MAX(), and MIN() are commonly used with GROUP BY.
Syntax:
SELECT <field(s)>, <aggregate function(s)>
FROM <table name>
WHERE <condition>
GROUP By <field(s)>
Example: Display the total marks of students class-wise.
Table: STUDENT
| Adno | Name | Class | Marks |
| 101 | Rahul | 12 | 85 |
| 102 | Priya | 12 | 90 |
| 103 | Aman | 11 | 78 |
| 104 | Neha | 11 | 82 |
Solution:
SELECT Class, SUM(Marks)
FROM Student
GROUP BY Class;
Having clause
- Helps to filter summarized result produced after grouping.
- It is like post filter which retrieves records from resultset produced after group by query.
- It can include aggregate functions to set the filter criteria
Syntax:
SELECT <field(s)>, <aggregate function(s)>
FROM <table name>
WHERE <condition>
GROUP By <field(s)>
HAVING <expression>
ORDER BY <field(s)> ASC/DESC
Example:
Display the total marks of each class where the total marks are greater than 160 using GROUP BY and HAVING.
Table: STUDENT
| Adno | Name | Class | Marks |
| 101 | Rahul | 12 | 85 |
| 102 | Priya | 12 | 90 |
| 103 | Aman | 11 | 78 |
| 104 | Neha | 11 | 82 |
Solution:
SELECT Class, SUM(Marks)
FROM Student
GROUP BY Class
HAVING SUM(Marks) > 160;
Using Two Relations In A Query
Cartesian product on two tables
- Cartesian Product is the result when every row of one table is combined with every row of another table.
- It happens when we do a join without any condition.
- If Table A has 2 rows and Table B has 3 rows, then:
Cartesian Product = 2 × 3 = 6 rows
Example: Cartesian Product
Table: STUDENT
| Student_ID | Name |
| 1 | Rahul |
| 2 | Priya |
Table: SUBJECT
| Subject_ID | Subject |
| 101 | Maths |
| 102 | Science |
SQL Query:
SELECT *
FROM Student, Subject;
Output (Cartesian Product):
| Student_ID | Name | Subject_ID | Subject |
| 1 | Rahul | 101 | Maths |
| 1 | Rahul | 102 | Science |
| 2 | Priya | 101 | Maths |
| 2 | Priya | 102 | Science |
Joins
- Joins are used to combine rows from two or more tables based on a related column between them.
- They help in retrieving meaningful data by connecting related tables.
JOIN Operation in SQL – Equi Join and Natural Join
For given two tables Book and Issue:
Table: BOOK
| BCode | BName | Author |
| 1 | Physics | H.C. Verma |
| 2 | Chemistry | O.P. Tandon |
| 3 | Maths | R.D. Sharma |
Table: ISSUE
| BCode | Student_Name | Issue_Date |
| 1 | Rahul | 2026-01-10 |
| 1 | Priya | 2026-01-12 |
| 2 | Aman | 2026-01-15 |
| 3 | Neha | 2026-01-18 |
Display BCode, BName, Author, Student_Name and Issue_Date of related records from both tables.
Note:
- BCode is the common attribute between both tables.
- It is the Primary Key in BOOK table and Foreign Key in ISSUE table.
- It is used to combine related records from both tables.
Solution
Using WHERE clause (Equi Join)
SELECT *
FROM BOOK B, ISSUE I
WHERE B.BCode = I.BCode;
Using JOIN clause (Explicit JOIN)
SELECT *
FROM BOOK B JOIN ISSUE I
ON B.BCode = I.BCode;
Using NATURAL JOIN
SELECT *
FROM BOOK
NATURAL JOIN ISSUE;
Note:
- In WHERE / JOIN / NATURAL JOIN, the condition is based on the common attribute.
- NATURAL JOIN automatically removes duplicate columns from the result.
- The common attribute is usually the Primary Key in one table and Foreign Key in another table.
⚠️ Don’t stop at SQL! To complete your understanding of Unit 3: Database Management in Class 12 Computer Science, you must learn Python Connectivity with SQL, where you’ll learn learn how to connect Python program with SQL databases and perform database operations through code. Find the link below.