Free Study Material
This chapter opens Class 12's database work with a quick recap of Class 11's core vocabulary, before this unit moves into new SQL functions and querying techniques.
Recapping the essentials:
NULL, never repeated); every other Candidate Key becomes an Alternate Key.The STUDENT table this unit continues to use:
| RollNo | Name | Stream | Marks |
|---|---|---|---|
| 1 | Aisha | Science | 91 |
| 2 | Rahul | Commerce | 78 |
| 3 | Meera | Science | 85 |
RollNo is the Primary Key — unique for every student, never empty. If Name also happened to be guaranteed unique in this specific school's records, it would be a Candidate Key too, and — since it isn't the chosen Primary Key — an Alternate Key.
Note: A well-chosen Primary Key should be something that genuinely never changes and never repeats for a real person — a roll number or admission number, not a name, which two different students could plausibly share.
This entire vocabulary set exists for one purpose: guaranteeing every row in a table can be told apart from every other row, reliably — the foundation every SQL command in the rest of this unit depends on being true.
A database is managed by a DBMS; the Relational Model organises data into Tables of Rows and Columns; a table's Primary Key (chosen from its Candidate Keys) uniquely identifies each row, with every other Candidate Key becoming an Alternate Key — the STUDENT table's RollNo is this unit's running example of one.
In Class 11 and 12, we study the **Relational Data Model**. In this model, data is organized into **Tables** (also called Relations).
You must know the technical terms used in Board Exams:
| Common Name | Technical Name | Definition |
|---|---|---|
| Table | Relation | A collection of data in rows and columns. |
| Row | Tuple | A single horizontal record. |
| Column | Attribute | A vertical field (e.g., Name, Age). |
These are the two most common 1-mark questions. You are usually given a table and asked to count these.
| ID | Name | City |
|---|---|---|
| 1 | Amit | Delhi |
| 2 | Ravi | Mumbai |
Degree = 3 (ID, Name, City)
Cardinality = 2 (Amit, Ravi)
A Key is used to uniquely identify a row. You cannot have two rows with the same key.
This topic recaps Class 11's Data Definition commands — the ones that build a database's structure before any data goes in.
Recapping the three DDL commands:
CREATE TABLE STUDENT (
RollNo INT PRIMARY KEY,
Name VARCHAR(30) NOT NULL,
Stream VARCHAR(20),
Marks INT
);
ALTER TABLE STUDENT ADD City VARCHAR(20);
CREATE TABLE STUDENT (...) builds this unit's running table, with RollNo as its Primary Key and Name required (NOT NULL). ALTER TABLE STUDENT ADD City VARCHAR(20); then adds a City column to the already-existing table — every row gains the new column, empty until filled in.
Warning:DROP TABLE STUDENT;would remove the entire table and every row of data in it, permanently — a fundamentally more destructive command thanALTER, which only reshapes structure.
CREATE, ALTER, and DROP together cover a table's entire structural lifecycle: bringing it into existence, reshaping it as requirements change, and — if genuinely needed — removing it completely.
CREATE builds a new database or table; ALTER TABLE changes an existing one's structure (ADD/DROP COLUMN/MODIFY); DROP TABLE removes an entire table and its data permanently — the three DDL commands this unit's SQL work continues to rely on.
System Update: SQL Engine Ready.
Action: The code blocks below are pure SQL. You can run them directly to create and modify tables.
When creating a table, you must define what kind of data each column holds.
| Type | Description | Example |
|---|---|---|
| INT / INTEGER | Whole numbers. | 10, 500, -5 |
| CHAR(n) | Fixed-length text. Good for codes (e.g., "M", "F"). | 'A', 'DL' |
| VARCHAR(n) | Variable-length text. Saves memory. | 'Amit', 'Delhi' |
| DATE | Stores Date in 'YYYY-MM-DD' format. | '2024-01-26' |
This is a very common interview/exam question.
/* ==================================================
1. CLEANUP (Standard Practice)
If table exists, delete it so we start fresh.
================================================== */
DROP TABLE IF EXISTS STUDENT;
/* ==================================================
2. CREATE TABLE
Syntax: CREATE TABLE Name (Col Type Constraint, ...);
================================================== */
CREATE TABLE STUDENT (
AdmNo INTEGER PRIMARY KEY,
Name VARCHAR(20),
DOB DATE
);
/* ==================================================
3. VERIFICATION
Check if the table was created (It will be empty).
================================================== */
SELECT * FROM STUDENT;
Constraints are rules enforced on data columns.
This topic recaps Class 11's Data Manipulation commands — the ones that fill and change the rows inside a table CREATE already built.
Recapping the three DML commands:
INSERT INTO STUDENT (RollNo, Name, Stream, Marks) VALUES (1, 'Aisha', 'Science', 91);
INSERT INTO STUDENT (RollNo, Name, Stream, Marks) VALUES (2, 'Rahul', 'Commerce', 78);
UPDATE STUDENT SET Marks = 95 WHERE RollNo = 1;
The two INSERT statements build up the STUDENT table one row at a time. UPDATE STUDENT SET Marks = 95 WHERE RollNo = 1; then changes only Aisha's Marks, leaving Rahul's row completely untouched — the WHERE clause is what limits the change to exactly the intended row.
Warning:UPDATE/DELETEwithout aWHEREclause applies to every row in the table, not just one — always double-checkWHEREis present before running either.
WHERE is the one clause every DML command relies on to target specific rows rather than the whole table — the exact same clause the next topic uses again, for SELECT instead.
INSERT adds a row; UPDATE ... WHERE changes matching rows; DELETE FROM ... WHERE removes matching rows — omitting WHERE on UPDATE/DELETE affects the entire table at once.
System Update: SQL Engine Ready.
Action: The code blocks below are pure SQL. You can run them directly in your SQL Element.
DML commands are used to manage the actual data rows.
| Command | Purpose | Critical Rule |
|---|---|---|
| INSERT | Adds new rows. | Values must match the column order. |
| UPDATE | Modifies existing data. | Always use WHERE (or you update everything!). |
| DELETE | Removes rows. | Always use WHERE (or you wipe the table!). |
/* ==================================================
SYSTEM SETUP (Building the empty table)
================================================== */
DROP TABLE IF EXISTS EMPLOYEE;
CREATE TABLE EMPLOYEE (EmpID INT, Name VARCHAR(50), Salary INT);
/* ==================================================
YOUR TASK: INSERT DATA
Syntax: INSERT INTO TableName VALUES (val1, val2...);
================================================== */
INSERT INTO EMPLOYEE VALUES (101, 'Amit', 50000);
INSERT INTO EMPLOYEE VALUES (102, 'Sumit', 60000);
/* ==================================================
VERIFICATION
================================================== */
SELECT * FROM EMPLOYEE;
/* ==================================================
SYSTEM SETUP (Table with Data)
================================================== */
DROP TABLE IF EXISTS EMPLOYEE;
CREATE TABLE EMPLOYEE (EmpID INT, Name VARCHAR(50), Salary INT);
INSERT INTO EMPLOYEE VALUES (101, 'Amit', 50000);
INSERT INTO EMPLOYEE VALUES (102, 'Sumit', 60000);
/* ==================================================
YOUR TASK: UPDATE AMIT'S SALARY
================================================== */
UPDATE EMPLOYEE
SET Salary = 55000
WHERE EmpID = 101;
/* ==================================================
VERIFICATION (Did Amit's salary change?)
================================================== */
SELECT * FROM EMPLOYEE;
/* ==================================================
SYSTEM SETUP (Fresh Data)
================================================== */
DROP TABLE IF EXISTS EMPLOYEE;
CREATE TABLE EMPLOYEE (EmpID INT, Name VARCHAR(50), Salary INT);
INSERT INTO EMPLOYEE VALUES (101, 'Amit', 55000);
INSERT INTO EMPLOYEE VALUES (102, 'Sumit', 60000);
/* ==================================================
YOUR TASK: REMOVE SUMIT
================================================== */
DELETE FROM EMPLOYEE
WHERE EmpID = 102;
/* ==================================================
VERIFICATION (Is Sumit gone?)
================================================== */
SELECT * FROM EMPLOYEE;
This closing recap topic covers Class 11's SELECT command — the most-used SQL command, and the one the rest of this unit builds directly on top of.
Recapping:
SELECT column1, column2, ... FROM table_name WHERE condition;
SELECT names which columns to retrieve (* for all); FROM names the table; WHERE filters which rows are included.
SELECT * FROM STUDENT;
SELECT Name, Marks FROM STUDENT WHERE Stream = 'Science';
SELECT * FROM STUDENT; retrieves every column, for every row, unfiltered. SELECT Name, Marks FROM STUDENT WHERE Stream = 'Science'; retrieves only two columns, and only for rows where Stream is 'Science' — Rahul (Commerce) would not appear in this result.
Note:BETWEEN,IN,LIKE,IS NULL, andAND/OR/NOT— the more advanced ways to filter withWHERE— were also covered in Class 11, and remain exactly as useful here; this recap keeps to the essentials.
Every new SQL technique this unit covers from here — functions, sorting, grouping, joins — is really just extending what SELECT ... FROM ... WHERE already does: retrieve exactly the data you actually need, nothing more.
SELECT columns FROM table WHERE condition; retrieves data, filtered to matching rows — the foundation this unit's new SQL functions and multi-table querying techniques all build directly on top of.
System Update: SQL Engine Ready.
Action: The code blocks below are pure SQL. Run them to query the database.
The SELECT command retrieves data from a table. You can pick specific columns or get everything.
| Goal | Syntax | Example |
|---|---|---|
| Get All Columns | SELECT * | SELECT * FROM Student; |
| Get Specific Columns | SELECT Col1, Col2 | SELECT Name, Marks FROM Student; |
| Remove Duplicates | SELECT DISTINCT | SELECT DISTINCT City FROM Student; |
/* ==================================================
SYSTEM SETUP (Populating Data)
================================================== */
DROP TABLE IF EXISTS STUDENT;
CREATE TABLE STUDENT (Name VARCHAR(20), City VARCHAR(20), Marks INT);
INSERT INTO STUDENT VALUES ('Amit', 'Delhi', 90);
INSERT INTO STUDENT VALUES ('Ravi', 'Mumbai', 85);
INSERT INTO STUDENT VALUES ('Pooja', 'Delhi', 92);
INSERT INTO STUDENT VALUES ('Sumit', 'Chennai', 88);
/* ==================================================
TASK A: View Everything
================================================== */
SELECT * FROM STUDENT;
/* ==================================================
TASK B: Unique Cities
(Notice 'Delhi' appears only once in the result)
================================================== */
SELECT DISTINCT City FROM STUDENT;
Usually, you don't want all the data. You only want rows that match specific criteria (e.g., "Students who failed" or "People from Delhi").
We use **Relational Operators** inside the WHERE clause:
=, >, <, >=, <=, != (Not Equal).AND (Both must be true), OR (At least one is true), NOT.BETWEEN 10 AND 20, IN (1, 2, 3).
/* ==================================================
SYSTEM SETUP (Re-using previous data)
Amit (Delhi, 90), Ravi (Mumbai, 85),
Pooja (Delhi, 92), Sumit (Chennai, 88)
================================================== */
DROP TABLE IF EXISTS STUDENT;
CREATE TABLE STUDENT (Name VARCHAR(20), City VARCHAR(20), Marks INT);
INSERT INTO STUDENT VALUES ('Amit', 'Delhi', 90);
INSERT INTO STUDENT VALUES ('Ravi', 'Mumbai', 85);
INSERT INTO STUDENT VALUES ('Pooja', 'Delhi', 92);
INSERT INTO STUDENT VALUES ('Sumit', 'Chennai', 88);
/* ==================================================
TASK A: High Scorers (> 89)
================================================== */
SELECT * FROM STUDENT
WHERE Marks > 89;
/* ==================================================
TASK B: Toppers from Delhi
(Only Pooja matches both conditions)
================================================== */
SELECT * FROM STUDENT
WHERE City = 'Delhi' AND Marks > 90;