Follow Us
माध्यम चुनें / Select Medium:
Eng (English) Hindi (हिन्दी)
झारखण्ड बोर्ड (JAC) • कक्षा XII • Computer Science • अध्याय 9
अनुमानित समय: 45 Mins
प्रगति: अध्ययनरत

स्ट्रक्चर्ड क्वेरी लैंग्वेज (SQL) (Structured Query Language (SQL))

In CBSE Class 12 Computer Science, "Structured Query Language (SQL)" provides an authoritative, hands-on master guide to relational database querying. This comprehensive chapter covers SQL command taxonomy (DDL, DML, DQL, TCL), database and table management (`CREATE`, `ALTER`, `DROP`), data manipulation (`INSERT`, `UPDATE`, `DELETE`), advanced data queries (`SELECT` with `WHERE`, `ORDER BY`, `GROUP BY`, `HAVING`), SQL constraints (`PRIMARY KEY`, `FOREIGN KEY`, `NOT NULL`, `UNIQUE`, `CHECK`, `DEFAULT`), aggregate functions (`COUNT`, `SUM`, `AVG`, `MIN`, `MAX`), pattern matching with `LIKE` (`%`, `_`), and relational multi-table JOIN operations (Equi-Join, Natural Join) aligned with the 2026–27 CBSE curriculum.

How Does a Single SQL Query Retrieve Exact Data from Tables with Millions of Rows?

When Amazon processes millions of daily orders, how does it instantly retrieve all orders placed by customers in Mumbai between March and June with a total value exceeding ₹5,000, sorted from highest to lowest? Writing nested Python loops to parse through millions of disk records would take minutes. In SQL (Structured Query Language), you describe WHAT data you want rather than HOW to fetch it. The database engine's query optimizer translates your declarative SQL statement into high-speed relational algebra executing in milliseconds. How do DDL, DML, aggregate functions, and multi-table joins orchestrate relational databases? This chapter masters SQL query engineering.

यह अध्याय क्यों महत्वपूर्ण है

SQL is the most durable, universally demanded programming language in software engineering, backend web development, and data science. Frameworks, languages, and frontends change constantly, but SQL has remained the dominant database query language for over 45 years. Mastering SQL commands, writing complex `GROUP BY...HAVING` aggregations, and executing multi-table joins is critical for scoring full marks in CBSE board exams and building real-world database applications.

अध्ययन से पूर्व (आवश्यक ज्ञान)

  • Relational database concepts: relations, tuples, attributes, and keys (Chapter 8).
  • Basic arithmetic operators and logical conditions (`AND`, `OR`, `NOT`).
  • Understanding of data types: integers, characters, dates, and decimals.

इस अध्याय के लक्ष्य

  • Classify SQL commands into DDL (Data Definition Language), DML (Data Manipulation Language), DQL, and TCL.
  • Construct and modify database schemas using `CREATE TABLE`, `ALTER TABLE` (ADD, MODIFY, DROP), and `DROP TABLE`.
  • Apply table constraints: `PRIMARY KEY`, `FOREIGN KEY REFERENCES`, `NOT NULL`, `UNIQUE`, `CHECK`, and `DEFAULT`.
  • Execute data modification statements: `INSERT INTO`, `UPDATE...SET`, and `DELETE FROM`.
  • Formulate advanced `SELECT` queries with filtering (`WHERE`, `BETWEEN`, `IN`, `LIKE`), sorting (`ORDER BY ASC/DESC`), and distinct values (`DISTINCT`).
  • Aggregate tabular data using `SUM()`, `AVG()`, `COUNT(*)`, `COUNT(col)`, `MIN()`, and `MAX()`.
  • Differentiate between row filtering (`WHERE`) and group filtering (`HAVING`) with `GROUP BY`.
  • Perform multi-table relational operations: Cartesian Product, Equi-Join, and Natural Join.

अध्याय रूपरेखा एवं प्रगति

1 1. SQL Command Classification: DDL,...
2 2. DDL & DML Command Syntax
3 3. Advanced Querying with `SELECT`,...
4 4. Multi-Table Relational JOIN Oper...

सम्पूर्ण सैद्धांतिक एवं वैचारिक अध्ययन

1. SQL Command Classification: DDL, DML, DQL & Constraints

Understand
CategoryFull NamePurposeCore SQL Commands
DDLData Definition LanguageDefines, alters, and destroys database structure and schemas. (Auto-commits!)`CREATE`, `ALTER`, `DROP`, `TRUNCATE`
DMLData Manipulation LanguageInserts, updates, and deletes data records inside existing tables.`INSERT`, `UPDATE`, `DELETE`
DQLData Query LanguageRetrieves data records from tables without modifying data.`SELECT`
TCLTransaction Control LanguageManages database transaction states and ACID integrity.`COMMIT`, `ROLLBACK`, `SAVEPOINT`
Table Constraints

Rules enforced on columns to ensure data validity and referential integrity:

  • `PRIMARY KEY`: Uniquely identifies each row (implies `UNIQUE` and `NOT NULL`).
  • `FOREIGN KEY...REFERENCES`: Enforces referential integrity pointing to a parent table primary key.
  • `NOT NULL`: Forbids column from holding missing/NULL values.
  • `UNIQUE`: Ensures all column values are distinct (allows NULLs).
  • `CHECK`: Validates an algebraic condition (e.g., `CHECK (Price > 0)`).
  • `DEFAULT`: Assigns a fallback value when none is supplied (e.g., `DEFAULT 'Active'`).

2. DDL & DML Command Syntax

Syntax & Examples
A. Table Creation & Schema Alteration (DDL)
-- Create Parent Table:
CREATE TABLE DEPARTMENT (
    DeptID VARCHAR(5) PRIMARY KEY,
    DeptName VARCHAR(30) NOT NULL
);

-- Create Child Table with Foreign Key:
CREATE TABLE EMPLOYEE (
    EmpID INT PRIMARY KEY,
    Name VARCHAR(40) NOT NULL,
    Salary DECIMAL(10, 2) CHECK (Salary >= 10000),
    DeptID VARCHAR(5),
    FOREIGN KEY (DeptID) REFERENCES DEPARTMENT(DeptID)
);

-- Alter Table: Adding a new column:
ALTER TABLE EMPLOYEE ADD Email VARCHAR(50);

-- Alter Table: Modifying existing column data type:
ALTER TABLE EMPLOYEE MODIFY Name VARCHAR(60);

-- Alter Table: Dropping a column:
ALTER TABLE EMPLOYEE DROP COLUMN Email;
B. Data Manipulation (DML)
-- Insert record:
INSERT INTO DEPARTMENT VALUES ('D01', 'Engineering');
INSERT INTO EMPLOYEE VALUES (101, 'Aarav Sharma', 75000.00, 'D01');

-- Update record (CRITICAL: Always use WHERE to prevent mass updates!):
UPDATE EMPLOYEE SET Salary = Salary * 1.10 WHERE DeptID = 'D01';

-- Delete record:
DELETE FROM EMPLOYEE WHERE EmpID = 101;

3. Advanced Querying with `SELECT`, Aggregates & Grouping

Understand & Querying
A. Pattern Matching with `LIKE`
  • Percent (`%`): Matches zero, one, or multiple characters (e.g., `WHERE Name LIKE 'A%'` matches names starting with 'A').
  • Underscore (`_`): Matches exactly one single character (e.g., `WHERE Name LIKE '_a%'` matches names whose second letter is 'a').
B. Aggregate Functions & `COUNT(*)` vs `COUNT(col)`
  • `COUNT(*)`: Counts the total number of rows in the table, including rows with NULL values.
  • `COUNT(col)`: Counts rows where `col` is NOT NULL (ignores NULLs).
  • `SUM(col)`, `AVG(col)`, `MIN(col)`, `MAX(col)`: Compute statistical aggregations, strictly ignoring NULL values.
C. `GROUP BY` vs `HAVING` (THE CRITICAL EXAM DISTINCTION)
  • WHERE: Filters individual rows *before* grouping occurs. Cannot contain aggregate functions!
  • GROUP BY: Combines rows sharing identical values in specified columns into summary groups.
  • HAVING: Filters groups *after* aggregation has occurred. Can contain aggregate functions!
-- Find departments where average salary exceeds 50,000, considering only active staff:
SELECT DeptID, COUNT(*) AS TotalStaff, AVG(Salary) AS AvgSal
FROM EMPLOYEE
WHERE Salary > 20000          -- 1. Filters individual rows BEFORE grouping
GROUP BY DeptID              -- 2. Groups remaining rows by Department
HAVING AVG(Salary) > 50000   -- 3. Filters groups AFTER aggregation
ORDER BY AvgSal DESC;        -- 4. Sorts output descending

4. Multi-Table Relational JOIN Operations

Understand

A JOIN query combines columns from two or more tables based on a related common attribute:

  • Equi-Join: Joins tables where the join condition uses the equality operator (`=`):
    SELECT E.Name, E.Salary, D.DeptName
    FROM EMPLOYEE E, DEPARTMENT D
    WHERE E.DeptID = D.DeptID;
  • Natural Join: Compares common columns with identical names in both tables automatically, eliminating duplicate join columns from the output.

प्रोग्रामिंग सिंटेक्स, स्टेटमेंट्स एवं भाषा अनुवादक नियम

SQL Clause Execution Order
$$\text{FROM} \to \text{WHERE} \to \text{GROUP BY} \to \text{HAVING} \to \text{SELECT} \to \text{ORDER BY}$$
Internal logical processing pipeline of an SQL query.
Aggregate Null Handling
$$\text{AVG}(col) = \frac{\sum \text{non-null values}}{\text{COUNT}(col)}$$
Aggregates strictly ignore NULLs.

SQL Query Execution Pipeline & Grouping Architecture

SQL Query Execution Pipeline: FROM to ORDER BY 1. FROM Tables / Joins 2. WHERE Filter Rows 3. GROUP BY Aggregate 4. HAVING Filter Groups 5. SELECT Project Cols 6. ORDER BY Sort ASC/DESC Crucial Comparison: WHERE vs HAVING Clause WHERE Clause (Row Filter) • Filters individual records BEFORE grouping • Applies to individual row values • CANNOT use aggregate functions (SUM/AVG) Example: WHERE Salary > 25000 HAVING Clause (Group Filter) • Filters summary groups AFTER aggregation • Requires a preceding GROUP BY clause • CAN and DOES use aggregate functions Example: HAVING AVG(Salary) > 50000

अध्याय का सार संक्षेप एवं 10 मुख्य निष्कर्ष

मुख्य बिंदु 1
SQL is a declarative language categorized into DDL (structure), DML (records), DQL (queries), and TCL (transactions).
मुख्य बिंदु 2
`CREATE TABLE` defines attributes, types, and constraints; `ALTER TABLE` modifies table schemas (ADD, MODIFY, DROP).
मुख्य बिंदु 3
`DROP TABLE` destroys both table schema and data; `DELETE FROM` erases data rows while preserving table structure.
मुख्य बिंदु 4
`PRIMARY KEY` guarantees uniqueness and forbids NULLs; `FOREIGN KEY` enforces referential integrity across relations.
मुख्य बिंदु 5
`SELECT` queries filter rows using `WHERE` and sort output using `ORDER BY ASC/DESC`.
मुख्य बिंदु 6
`LIKE` operator performs pattern matching: `%` matches zero or more characters; `_` matches exactly one character.
मुख्य बिंदु 7
`COUNT(*)` counts all rows including NULLs; `COUNT(col)` counts only non-null values.
मुख्य बिंदु 8
Aggregate functions (`SUM`, `AVG`, `MIN`, `MAX`) ignore NULL values during calculation.
मुख्य बिंदु 9
`WHERE` filters individual rows before grouping; `HAVING` filters aggregated groups after `GROUP BY`.
मुख्य बिंदु 10
An Equi-Join combines tables using equality on common attributes: `WHERE TableA.Key = TableB.Key`.

स्व-मूल्यांकन अभ्यास (Check Your Understanding)

मूल वैचारिक स्पष्टता की जांच के लिए नैदानिक प्रश्न। पहले स्वयं हल करें, फिर उत्तर देखें।

1
Differentiate between DDL and DML commands. Give two examples of each.
उत्तर एवं व्याख्या देखें
उत्तर: • DDL (Data Definition Language): Commands that define, alter, or delete the physical database structure/schema. They auto-commit permanently. Examples: `CREATE TABLE`, `ALTER TABLE`, `DROP TABLE`.
• DML (Data Manipulation Language): Commands that manipulate, insert, modify, or delete actual data records stored inside existing tables. Examples: `INSERT INTO`, `UPDATE`, `DELETE FROM`.
DDL alters table structures (CREATE/ALTER); DML modifies data records (INSERT/UPDATE/DELETE).
2
What is the critical difference between `DROP TABLE` and `DELETE FROM` in SQL?
उत्तर एवं व्याख्या देखें
उत्तर: `DELETE FROM table_name` is a DML command that deletes data rows from the table while leaving the table structure, column definitions, and constraints completely intact for future inserts. `DROP TABLE table_name` is a DDL command that permanently destroys both the data rows AND the entire table structure/schema from the database dictionary.
DELETE clears data rows keeping table structure; DROP deletes table schema and data permanently.
3
Explain the difference between `COUNT(*)` and `COUNT(column_name)` with an example containing NULL values.
उत्तर एवं व्याख्या देखें
उत्तर: • `COUNT(*)` counts the total number of records/rows in a table, regardless of whether individual columns contain NULL values.
• `COUNT(column_name)` counts only those rows where the specified column contains a valid, non-NULL value.
Example: If a table has 5 rows and column `Bonus` contains values `[1000, NULL, 2000, NULL, 1500]`, `COUNT(*)` returns `5`, while `COUNT(Bonus)` returns `3`.
COUNT(*) counts all rows; COUNT(col) counts only rows where column is NOT NULL.
4
Differentiate between the `WHERE` clause and the `HAVING` clause. Can they be used in the same query?
उत्तर एवं व्याख्या देखें
उत्तर: • `WHERE` filters individual rows *before* grouping occurs and cannot contain aggregate functions (e.g., `WHERE Salary > 20000`).
• `HAVING` filters aggregated groups *after* the `GROUP BY` operation and typically contains aggregate functions (e.g., `HAVING AVG(Salary) > 50000`).
Yes, both can and frequently are used in the same query: `WHERE` first eliminates ineligible rows, remaining rows are grouped, and `HAVING` filters the resulting groups.
WHERE filters rows before grouping; HAVING filters groups after aggregation.
5
Given table `STUDENT(Roll, Name, Marks, Stream)`, write SQL queries to:
(a) Display names of students starting with letter 'S'.
(b) Display distinct streams available.
(c) Display students with marks between 80 and 95.
उत्तर एवं व्याख्या देखें
उत्तर: (a) `SELECT Name FROM STUDENT WHERE Name LIKE 'S%';`
(b) `SELECT DISTINCT Stream FROM STUDENT;`
(c) `SELECT * FROM STUDENT WHERE Marks BETWEEN 80 AND 95;`
Use LIKE "S%", DISTINCT, and BETWEEN 80 AND 95.
6
Write an SQL query to display the Stream and the average marks of each stream, but only for streams with more than 5 students, sorted by average marks in descending order.
उत्तर एवं व्याख्या देखें
उत्तर:
SELECT Stream, AVG(Marks) AS AvgMarks
FROM STUDENT
GROUP BY Stream
HAVING COUNT(*) > 5
ORDER BY AvgMarks DESC;

GROUP BY Stream, HAVING COUNT(*) > 5, ORDER BY AvgMarks DESC.
7
What is an Equi-Join? Write an SQL query joining `CUSTOMER(CustID, Name)` and `ORDERS(OrderID, CustID, Amount)` to display Name and Amount.
उत्तर एवं व्याख्या देखें
उत्तर: An Equi-Join is a join operation that combines rows from two tables where the join condition is an equality match between common attributes.
Query:
SELECT C.Name, O.Amount
FROM CUSTOMER C, ORDERS O
WHERE C.CustID = O.CustID;

Match customer and order using WHERE C.CustID = O.CustID.
8
What is the function of the `ALTER TABLE` command? Provide SQL syntax to add a new column `DOB DATE` to an existing table `TEACHER`.
उत्तर एवं व्याख्या देखें
उत्तर: The `ALTER TABLE` command is a DDL statement used to modify the structure of an existing table (such as adding columns, dropping columns, or modifying column data types) without losing existing data.
Syntax:
ALTER TABLE TEACHER ADD DOB DATE;
ALTER TABLE table_name ADD column_name data_type;
अध्याय का अध्ययन पूर्ण हुआ?
अभ्यास के लिए तैयार?

ऑनलाइन CBT टेस्ट देकर तैयारी का मूल्यांकन करें

झारखण्ड बोर्ड परीक्षा पैटर्न पर आधारित बहुविकल्पीय प्रश्नों का ऑनलाइन टेस्ट दें। तुरंत परिणाम, समय विश्लेषण और प्रत्येक प्रश्न का विस्तृत हल प्राप्त करें।

AI अध्ययन मित्र

त्वरित शंका समाधान

स्ट्रक्चर्ड क्वेरी लैंग्वेज (SQL) (Structured Query Language (SQL)) में कोई संदेह या प्रश्न है? हमारे AI अध्ययन मित्र से तुरंत समझें।