Chapter 12: Structured Query Language (SQL) Class 12 Computer Science NCERT Solutions
Structured Query Language (SQL) is the standard language for managing and manipulating relational databases. In Chapter 12 – SQL, students dive into practical database interaction, writing queries to create tables, insert data, update records, and retrieve meaningful information using various SQL commands.
What You Will Learn in Chapter 12
This chapter focuses on hands-on SQL usage, teaching students how to use SQL statements to manage databases. It includes data definition, manipulation, and query operations along with filtering, sorting, pattern matching, and aggregate functions.
Key Topics Covered
SQL Categories
- DDL (Data Definition Language): CREATE, ALTER, DROP
- DML (Data Manipulation Language): INSERT, UPDATE, DELETE
- DQL (Data Query Language): SELECT
Creating Tables Using SQL
CREATE TABLE Employee (
EmpID INT PRIMARY KEY,
Name VARCHAR(50),
Salary INT,
Department VARCHAR(30)
);
Inserting Records
INSERT INTO Employee VALUES (101, \'Anjali\', 50000, \'HR\');
INSERT INTO Employee VALUES (102, \'Ravi\', 60000, \'IT\');
Selecting Data
SELECT * FROM Employee;
SELECT Name, Salary FROM Employee WHERE Department = \'IT\';
Updating and Deleting Records
UPDATE Employee SET Salary = 70000 WHERE EmpID = 102;
DELETE FROM Employee WHERE EmpID = 101;
Filtering Data Using WHERE Clause
SELECT * FROM Employee WHERE Salary > 50000;
SELECT * FROM Employee WHERE Department = \'IT\' AND Salary >= 60000;
Pattern Matching with LIKE
SELECT * FROM Employee WHERE Name LIKE \'A%\';
– Names starting with ASELECT * FROM Employee WHERE Name LIKE \'_a__\';
– Names with \’a\’ as second letter
Using IN, BETWEEN, IS NULL
SELECT * FROM Employee WHERE Department IN (\'HR\', \'IT\');
SELECT * FROM Employee WHERE Salary BETWEEN 40000 AND 70000;
SELECT * FROM Employee WHERE Department IS NULL;
Sorting Records
SELECT * FROM Employee ORDER BY Salary DESC;
SELECT * FROM Employee ORDER BY Department ASC, Salary DESC;
Aggregate Functions
SQL supports built-in aggregate functions to perform calculations on data.
SELECT COUNT(*) FROM Employee;
– Total number of recordsSELECT AVG(Salary) FROM Employee;
– Average salarySELECT MAX(Salary), MIN(Salary) FROM Employee;
– Max and Min salarySELECT SUM(Salary) FROM Employee;
– Total salary payout
Grouping Data Using GROUP BY
SELECT Department, COUNT(*) FROM Employee GROUP BY Department;
SELECT Department, AVG(Salary) FROM Employee GROUP BY Department;
Using HAVING Clause
SELECT Department, AVG(Salary)
FROM Employee
GROUP BY Department
HAVING AVG(Salary) > 55000;
Practice Table – Sample Data
EmpID | Name | Salary | Department |
---|---|---|---|
101 | Anjali | 50000 | HR |
102 | Ravi | 60000 | IT |
103 | Priya | 55000 | Finance |
Important SQL Output Questions
- Write SQL queries to display records based on conditions
- Predict the output of SELECT queries with WHERE, ORDER BY, LIKE
- Output-based MCQs with GROUP BY and HAVING
Common Errors in SQL Queries
- Missing commas in column list
- Incorrect string comparison syntax
- Wrong use of WHERE vs HAVING
- Using aggregate functions without GROUP BY
Download PDF – Chapter 12 SQL Solutions
Includes:
- Textbook-based SQL queries with answers
- Sample tables and query-based outputs
- MCQs and assertion-reason questions on SQL
- Error identification and correction practice
Preparation Tips for SQL
- Memorize syntax of SELECT, WHERE, ORDER BY, GROUP BY
- Practice on paper and database apps like MySQL or SQLite
- Understand logical flow of filters and aggregations
- Solve output-based and query writing questions daily
Additional Resources
- SQL Practice Sheets: Query writing exercises
- Online SQL Editors: Try code live on platforms like db-fiddle
- Flashcards: SQL functions and syntax reminders
- Sample Practical Exams: Realistic school-level SQL tasks
Conclusion
SQL is a vital skill for anyone interested in data, development, or IT. Chapter 12 equips students with powerful tools to manipulate and query data using SQL. With strong command over SQL queries, pattern matching, and grouping logic, students will be ready for board exams and real-world database work.
Class 12 Computer Science (C++) – Chapter 12 Structured Query Language
Short Answer Type Questions-I [2 mark each]
Question 1: Differentiate between DELETE and DROP table command?
DELETE: Used to remove data from specific rows in a table. If used without a WHERE clause, it deletes all rows but retains the table structure. It is a DML (Data Manipulation Language) command. DROP: Removes the entire structure of the table along with its data. It is a DDL (Data Definition Language) command.Question 2: What is the use of wildcard?
Wildcard operators are used with theLIKE
operator to search for values matching a specific pattern in a column.
Wildcard symbols:
%
– Represents 0, 1, or many characters
_
– Represents a single character
Question 3: Write SQL query to add a column total_price with datatype numeric and size (10,2) in a table product.
ALTER TABLE product ADD total_price NUMBER(10,2);
Question 4: While creating table ‘customer’, Rahula forgot to add column ‘price’. Which command is used to add new column in the table?
ALTER TABLE customer ADD price NUMBER(10,2);
Question 5: Deepika wants to remove all rows from the table BANK but keep the structure intact. Which command should she use?
DELETE FROM BANK;
Question 6: Sonal needs to display name of teachers, who have “0” as the third character in their name. She wrote the following query:
SELECT name FROM teacher WHERE name = “$$0?”;
But the query isn’t producing the result. Identify the problem.
The wildcards used in the query are incorrect.
Correct Query:
SELECT name FROM teacher WHERE name LIKE '__0%';
Question 7: Consider the following tables SCHOOL and ADMIN and answer the following queries:
Table: SCHOOL
CODE | TEACHER | SUBJECT | DOJ | PERIODS | EXPERIENCE |
---|---|---|---|---|---|
1001 | RAVI SHANKAR | ENGLISH | 12/3/2000 | 24 | 10 |
1009 | PRIYARAI | 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 |
1203 | FEMALE | COORDINATOR |
1045 | MALE | HOD |
1123 | MALE | SENIOR TEACHER |
1167 | MALE | SENIOR TEACHER |
1215 | MALE | HOD |
(i) SQL Query:
SELECT Designation, COUNT(*) FROM Admin GROUP BY Designation HAVING COUNT(*) < 2;
Answer:
VICE PRINCIPAL – 1
(ii) SQL Query:
SELECT MAX(EXPERIENCE) FROM SCHOOL;
Answer: 16
(iii) SQL Query:
SELECT TEACHERNAME FROM SCHOOL WHERE EXPERIENCE > 12 ORDER BY TEACHERNAME;
Answer:
UMESH
YASHRAJ
Short Answer Type Questions-II [3 mark each]
Question 1: SQL Queries and Outputs
Query (i):
SELECT NO, NAME, TDATE FROM TRAVEL ORDER BY NO DESC;
Query (ii):
SELECT NAME FROM TRAVEL WHERE CODE IN (101, 102);
Query (iii):
SELECT NO, NAME FROM TRAVEL WHERE TDATE BETWEEN '2015-04-01' AND '2015-12-31';
Query (iv):
SELECT * FROM TRAVEL WHERE KM > 100 ORDER BY NOP;
Query (v) Output:
COUNT(*) | CODE |
---|---|
2 | 101 |
2 | 102 |
Query (vi) Output:
101
103
102
104
105
Query (vii) Output:
CODE | NAME | VTYPE |
---|---|---|
104 | Ahmed Khan | CAR |
Query (viii) Output:
NAME | KM*PERKM |
---|---|
Tarun Ram | 14000 |
Question 2: SQL Queries and Outputs
Query (i):
SELECT CNO, CNAME, TRAVELDATE FROM TRAVEL ORDER BY CNO DESC;
Query (ii):
SELECT CNAME FROM TRAVEL WHERE VCODE IN ('V01', 'V02');
Query (iii):
SELECT CNO, CNAME FROM TRAVEL WHERE TRAVELDATE BETWEEN '2015-05-01' AND '2015-12-31';
Query (iv):
SELECT * FROM TRAVEL WHERE KM > 120 ORDER BY NOP;
Query (v) Output:
COUNT(*) | VCODE |
---|---|
2 | V01 |
2 | V02 |
Query (vi) Output:
V01
V03
V02
V04
V05
Query (vii) Output:
VCODE | CNAME | VEHICLETYPE |
---|---|---|
V04 | John Malina | CAR |
Query (viii) Output:
CNAME | KM*PERKM |
---|---|
Sahanubhuti | 30 |