CBSE Class 12 Computer Science Chapter 12: Structured Query Language (SQL) NCERT Solutions
This chapter provides essential NCERT Solutions for Class 12 Computer Science, focusing on Structured Query Language (SQL). It covers fundamental SQL commands like DELETE, DROP TABLE, ALTER TABLE, and the use of wildcard characters for pattern matching. The solutions explain how to manipulate data and table structures, including adding columns and removing rows while preserving table definitions. Key concepts such as DML and DDL commands are differentiated. The exercises also involve writing SQL queries for specific data retrieval and manipulation tasks, including grouping and ordering results. These solutions are designed to help students understand and apply SQL concepts effectively for their exams, offering clear explanations and step-by-step query implementations.
Quick info
| Board | CBSE |
|---|---|
| Class | Class 12 |
| Subject | Computer Science (C++) |
| Session | 2026 |
| Language | English |
| Type | NCERT Solutions |
| Chapter | Chapter 12 |
Chapter summary
Chapter 12 focuses on Structured Query Language (SQL) for Class 12 Computer Science. It covers essential SQL commands for database management, including differentiating between DML (like DELETE) and DDL (like DROP TABLE) commands. The chapter also explains the use of wildcard characters (%) and (_) for pattern matching in queries and demonstrates how to modify table structures using ALTER TABLE. Exercises include practical query writing for adding columns, removing rows, and retrieving specific data based on conditions and patterns.
Learning outcomes
- Understand the difference between DELETE and DROP TABLE commands.
- Learn to use wildcard characters (%) and (_) in SQL queries.
- Apply the ALTER TABLE command to add new columns to existing tables.
- Write SQL queries to remove all rows from a table using the DELETE command.
- Identify and correct errors in SQL queries involving wildcard usage.
- Execute SQL queries involving GROUP BY, HAVING, ORDER BY, and aggregate functions.
Topics covered
Paper topics
- Structured Query Language (SQL)
- DELETE Command
- DROP TABLE Command
- ALTER TABLE Command
- Wildcard Characters
- LIKE Operator
- DML Commands
- DDL Commands
- SQL Query Writing
- Aggregate Functions
- GROUP BY Clause
- HAVING Clause
Important topics
- DELETE vs DROP TABLE
- Wildcard usage with LIKE
- ALTER TABLE syntax
- Writing SQL queries for data retrieval
- GROUP BY and HAVING clauses
PDF preview
Read page by page below. PDF is streamed from the official NCERT website — no download button on this page.
Questions and Solutions
Question 1
The primary distinction between the DELETE and DROP TABLE commands lies in their function and scope:
- DELETE Command: This is a Data Manipulation Language (DML) command. It is used to remove one or more rows from a table based on a specified condition. If no condition is provided (
DELETE FROM table_name;), it removes all rows from the table, but the table's structure (columns, data types, constraints) remains intact. - DROP TABLE Command: This is a Data Definition Language (DDL) command. It is used to remove the entire table from the database. This includes removing all the data within the table as well as its structure (schema). Once a table is dropped, it cannot be recovered without restoring from a backup.
In summary, DELETE affects the data within a table, while DROP TABLE affects the table itself.
Question 2
Wildcard characters in SQL are special symbols used in the WHERE clause with the LIKE operator to search for specific patterns within a column's data. They allow for flexible searching when you don't know the exact value or want to find values that match a certain format.
The two main wildcard operators are:
- % (Percent sign): Represents zero, one, or multiple characters. For example,
LIKE 'A%'would find all values starting with 'A'.LIKE '%a'would find all values ending with 'a'.LIKE '%an%'would find all values containing 'an' anywhere. - _ (Underscore): Represents a single character. For example,
LIKE '_a%'would find all values where the second character is 'a'.
Question 3
To add a new column to an existing table, the ALTER TABLE command is used. The syntax involves specifying the table name, the ADD clause, the new column name, and its data type and size.
The SQL query to implement this is:
ALTER TABLE product ADD total_price NUMERIC(10, 2);
Here, NUMERIC(10, 2) specifies that the column can store numbers with a total of 10 digits, with 2 digits after the decimal point.
Question 4
The command used to add a new column to an existing table in SQL is ALTER TABLE. This command allows you to modify the structure of a table after it has been created.
To add the 'price' column to the 'customer' table, Rahula would use the following SQL query:
ALTER TABLE customer ADD price NUMERIC(10, 2);
This query adds a column named 'price' to the 'customer' table, specifying its data type as NUMERIC with a precision of 10 digits and a scale of 2 decimal places.
Question 5
To remove all rows from a table while keeping its structure intact, Deepika should use the DELETE command without any condition. This command is part of Data Manipulation Language (DML) and specifically targets the data within the table.
The SQL command to achieve this is:
DELETE FROM BANK;
This command will empty the BANK table, but the table definition (columns, data types, etc.) will remain unchanged.
Question 6
Select name from teacher where name = "$$0?"; But the query isn't producing the result. Identify the problem.
The problem with Sonal's query lies in the incorrect use of wildcard characters and the comparison operator. The query uses '=' which requires an exact match, and the symbols '$$0?' are not standard SQL wildcards for pattern matching.
To correctly find names where the third character is '0', Sonal should use the LIKE operator with the appropriate wildcards. The underscore symbol (_) represents a single character. Therefore, two underscores are needed to represent the first two characters, followed by '0', and then a percent sign (%) to represent any subsequent characters.
The corrected SQL query is:
SELECT name FROM teacher WHERE name LIKE '__0%';
Question 7
Select Designation, Count (*) From Admin Group By Designation Having Count (*) <2;SELECT max (EXPERIENCE) FROM SCHOOL;SELECT TEACHERNAME FROM SCHOOL WHERE EXPERIENCE >12 ORDER BY TEACHERNAME;SELECT COUNT (*), GENDER FROM ADMIN GROUP BY GENDER;
Table: SCHOOL
| CODE | TEACHER | SUBJECT | DOJ | PERIODS | EXPERIENCE |
| 1001 | RAVI SHANKAR | ENGLISH | 12/3/2000 | 24 | 10 |
| 1009 | PRIYA RAI | PHYSICS | 03/09/1998 | 26 | 12 |
| 1203 | LIS ANAND | ENGLISH | 09/04/2000 | 27 | 5 |
| 1045 | YASHRAJ | MATHS | 24/8/2000 | 24 | 15 |
| 1123 | GANAN | PHYSICS | 16/7/1999 | 28 | 3 |
| 1167 | HARISHB | CHEMISTRY | 19/10/1999 | 27 | 5 |
| 1215 | UMESH | PHYSICS | 11/05/1998 | 22 | 16 |
TABLE: ADMIN
| CODE | GENDER | DESIGNATION |
| 1001 | MALE | VICE PRINCIPAL |
| 1009 | FEMALE | COORDINATOR |
Let's analyze each query based on the provided tables:
-
Query:
Select Designation, Count (*) From Admin Group By Designation Having Count (*) <2;Explanation: This query groups the 'Admin' table by 'Designation' and counts the number of entries for each designation. TheHAVINGclause then filters these groups to show only those designations that have a count less than 2. Admin Table Analysis:- VICE PRINCIPAL: Count = 1
- COORDINATOR: Count = 1
DESIGNATION COUNT(*) VICE PRINCIPAL 1 COORDINATOR 1 -
Query:
SELECT max (EXPERIENCE) FROM SCHOOL;Explanation: This query finds the maximum value in the 'EXPERIENCE' column of the 'SCHOOL' table. SCHOOL Table Analysis: The 'EXPERIENCE' values are 10, 12, 5, 15, 3, 5, 16. The maximum among these is 16. Output:MAX(EXPERIENCE) 16 -
Query:
SELECT TEACHERNAME FROM SCHOOL WHERE EXPERIENCE >12 ORDER BY TEACHER NAME;Explanation: This query selects the names of teachers from the 'SCHOOL' table where their 'EXPERIENCE' is greater than 12. The results are then ordered alphabetically by teacher name. SCHOOL Table Analysis: Teachers with experience > 12 are YASHRAJ (15) and UMESH (16). Output:TEACHERNAME UMESH YASHRAJ -
Query:
SELECT COUNT (*), GENDER FROM ADMIN GROUP BY GENDER;Explanation: This query groups the rows in the 'Admin' table by 'GENDER' and counts the number of entries for each gender. Admin Table Analysis:- MALE: Count = 1
- FEMALE: Count = 1
COUNT(*) GENDER 1 MALE 1 FEMALE
Common mistakes
- Confusing DELETE (removes data) with DROP TABLE (removes structure and data).
- Incorrectly using wildcard characters or syntax in LIKE clauses.
- Syntax errors when using ALTER TABLE to modify table structures.
- Misunderstanding the scope of conditions in WHERE clauses.
- Errors in applying aggregate functions or GROUP BY clauses.
Revision tips
- Clearly distinguish between DML and DDL commands and their effects.
- Practice writing queries using wildcard characters for various pattern matching scenarios.
- Review the syntax for ALTER TABLE commands to modify table structures.
- Work through the provided query examples to understand data manipulation and retrieval logic.
- Pay close attention to the conditions and clauses (WHERE, GROUP BY, ORDER BY) in complex queries.
Practice MCQs
Q1. Which SQL command is used to remove all rows from a table while keeping its structure intact?
Explanation: The DELETE FROM command removes all rows from a table without affecting its structure. DROP TABLE removes both the structure and the data.
Q2. In SQL, which wildcard character represents a single character?
Explanation: The underscore symbol (_) is used as a wildcard to represent a single character in SQL pattern matching.
Q3. Which command is used to add a new column to an existing table?
Explanation: The ALTER TABLE command is used to modify the structure of an existing table, including adding, deleting, or modifying columns.
Q4. What is the primary difference between DELETE and DROP TABLE?
Explanation: DELETE is a Data Manipulation Language (DML) command that removes rows (data) from a table, while DROP TABLE is a Data Definition Language (DDL) command that removes the entire table structure and its data.
Q5. Which SQL clause is used to filter groups based on a specified condition after grouping?
Explanation: The HAVING clause is used to filter the results of a GROUP BY clause, applying conditions to the aggregated data.
Frequently asked questions
What is the main difference between DELETE and DROP TABLE in SQL?
The DELETE command removes rows (data) from a table, but the table structure remains. The DROP TABLE command removes the entire table, including its structure and all data.
How are wildcard characters used in SQL?
Wildcard characters like '%' (zero or more characters) and '_' (a single character) are used with the LIKE operator to search for specific patterns within text data in a column.
Which SQL command is used to modify the structure of an existing table?
The ALTER TABLE command is used to modify the structure of an existing table, such as adding, deleting, or modifying columns.
What is the purpose of the HAVING clause in SQL?
The HAVING clause is used to filter groups based on a condition, similar to how the WHERE clause filters individual rows. It is typically used with aggregate functions in conjunction with the GROUP BY clause.
Can I add a new column to a table after it has been created?
Yes, you can add a new column to an existing table using the ALTER TABLE command with the ADD COLUMN clause.
What does the SQL query SELECT max (EXPERIENCE) FROM SCHOOL; do?
This query finds and returns the maximum value from the EXPERIENCE column in the SCHOOL table.
Content reviewed by the NCERT Help team. Editorial Team and update policy
NCERT Solutions PDF PDF on NCERT Help. URL unchanged for search indexing.