CBSE Class 12 Computer Science Chapter 5: Structured Query Language (SQL) NCERT Solutions

NCERT Solutions PDF Class 12 PDF

This section provides detailed NCERT Solutions for Chapter 5 of the CBSE Class 12 Computer Science syllabus, focusing on Structured Query Language (SQL). It covers essential SQL commands for data manipulation and retrieval, including SELECT, WHERE, ORDER BY, GROUP BY, HAVING, and DISTINCT. The solutions guide students through writing queries to extract specific information from given tables like 'VEHICLE' and 'TRAVEL', and also demonstrate how to interpret the output of pre-written SQL queries. Key concepts such as filtering data based on conditions, sorting results, grouping records, and identifying unique values are explained. These solutions are designed to help students understand the practical application of SQL in database management and prepare effectively for their examinations by reinforcing core concepts and problem-solving skills.

Quick info

BoardCBSE
ClassClass 12
SubjectComputer Science (Python)
Session2026
LanguageEnglish
TypeNCERT Solutions
ChapterChapter 5

Chapter summary

Chapter 5 of the CBSE Class 12 Computer Science curriculum introduces Structured Query Language (SQL). This set of NCERT Solutions focuses on practical application, covering how to write SQL queries to select, filter, and sort data from given tables ('VEHICLE' and 'TRAVEL'). It also includes interpreting the results of SQL queries involving aggregate functions, grouping, and distinct values. The solutions address common query writing tasks, helping students build proficiency in database querying.

Learning outcomes

  • Understand the basic structure of SQL queries.
  • Write SQL queries to retrieve specific data from tables.
  • Apply WHERE clause for data filtering based on conditions.
  • Use ORDER BY clause for sorting query results.
  • Interpret the output of SQL queries involving COUNT and GROUP BY.
  • Identify and use DISTINCT keyword to get unique values.
  • Formulate queries involving joins between tables.
  • Calculate derived values using arithmetic operations within queries.

Topics covered

Paper topics

  • Structured Query Language (SQL)
  • SQL SELECT Statement
  • SQL WHERE Clause
  • SQL ORDER BY Clause
  • SQL GROUP BY Clause
  • SQL HAVING Clause
  • SQL DISTINCT Keyword
  • SQL Joins (Implicit)
  • Aggregate Functions (COUNT)
  • Date and String Comparisons
  • Arithmetic Operations in Queries
  • Table Operations

Important topics

  • Writing SQL Queries
  • Filtering Data with WHERE
  • Sorting Data with ORDER BY
  • Grouping and Aggregation with GROUP BY and HAVING
  • Interpreting Query Outputs
  • Using Joins for Data Combination

PDF preview

Read page by page below. PDF is streamed from the official NCERT website — no download button on this page.

Loading document …
Page of
Loading page …

Questions and Solutions

Question 1 (i)

Write a query to display CNO, CNAME, and TRAVELDATE from the table TRAVEL in descending order of CNO.
Solution: To display the customer number (CNO), customer name (CNAME), and travel date (TRAVELDATE) from the TRAVEL table, sorted by CNO in descending order, you would use the following SQL query:

SELECT CNO, CNAME, TRAVELDATE FROM TRAVEL ORDER BY CNO DESC;

This query selects the specified columns and arranges the rows so that the customer with the highest CNO appears first.

Question 1 (ii)

Write a query to display the CNAME of all customers from the table TRAVEL who are travelling by vehicle with code V01 or V02.
Solution: To find the names of customers travelling in vehicles with codes 'V01' or 'V02', you can use the WHERE clause with the OR operator or the IN operator. The IN operator is generally more concise for multiple OR conditions on the same column.

Using the IN operator:

SELECT CNAME FROM TRAVEL WHERE VCODE IN ('V01', 'V02');

Alternatively, using the OR operator:

SELECT CNAME FROM TRAVEL WHERE VCODE = 'V01' OR VCODE = 'V02';

Both queries will return the names of customers whose travel records are associated with either vehicle code 'V01' or 'V02'.

Question 1 (iii)

Write a query to display the CNO and CNAME of those customers from the table TRAVEL who travelled between '2015-12-31' and '2015-05-01'.
Solution: To retrieve the customer number (CNO) and customer name (CNAME) for travels that occurred within a specific date range, you can use the BETWEEN operator or comparison operators (>= and <=). It's important to note that the BETWEEN operator is inclusive of the start and end dates. The order of dates in the condition matters if using BETWEEN.

Using the BETWEEN operator (assuming the intention is to find dates from May 1st, 2015 to December 31st, 2015):

SELECT CNO, CNAME FROM TRAVEL WHERE TRAVELDATE BETWEEN '2015-05-01' AND '2015-12-31';

Using comparison operators (also assuming the same date range):

SELECT CNO, CNAME FROM TRAVEL WHERE TRAVELDATE >= '2015-05-01' AND TRAVELDATE <= '2015-12-31';

If the intention was strictly between the dates provided in the question ('2015-12-31' and '2015-05-01'), and assuming the earlier date should be the start, the query would be:

SELECT CNO, CNAME FROM TRAVEL WHERE TRAVELDATE BETWEEN '2015-05-01' AND '2015-12-31';

Or using comparison operators:

SELECT CNO, CNAME FROM TRAVEL WHERE TRAVELDATE >= '2015-05-01' AND TRAVELDATE <= '2015-12-31';

Note: The source provided multiple equivalent query options. The most standard interpretation for a date range is to list the earlier date first.

Question 1 (iv)

Write a query to display all the details from table TRAVEL for the customers, who have travel distance more than 120 KM in ascending order of NOP (Number of Passengers).
Solution: To display all columns for records where the travel distance (KM) is greater than 120, and to sort these records by the number of passengers (NOP) in ascending order, use the following SQL query:

SELECT * FROM TRAVEL WHERE KM > 120 ORDER BY NOP ASC;

The `SELECT *` statement retrieves all columns from the table. The `WHERE KM > 120` clause filters the records, and `ORDER BY NOP ASC` sorts the filtered results based on the NOP column in ascending order.

Question 1 (v)

Find the output for the SQL query: SELECT COUNT (*), VCODE FROM TRAVEL GROUP BY VCODE HAVING COUNT(*) > 1;
Solution: This SQL query first groups all records in the TRAVEL table by their `VCODE`. Then, it counts the number of records (`COUNT(*)`) within each group. The `HAVING COUNT(*) > 1` clause filters these groups, keeping only those `VCODE`s that appear more than once in the table. The output will show the count and the `VCODE` for such vehicle codes.

Based on the provided TRAVEL table:

  • V01 appears 2 times.
  • V02 appears 2 times.
  • V03 appears 1 time.
  • V04 appears 1 time.
  • V05 appears 1 time.
Therefore, only V01 and V02 have a count greater than 1.

The output will be:

COUNT(*) | VCODE

------------------

2 | V01

2 | V02

Question 1 (vi)

Find the output for the SQL query: SELECT DISTINCT VCODE FROM TRAVEL;
Solution: The `SELECT DISTINCT VCODE` query retrieves all unique vehicle codes present in the TRAVEL table. The `DISTINCT` keyword ensures that each `VCODE` is listed only once, even if it appears multiple times in the table.

Looking at the TRAVEL table, the `VCODE` values are: V01, V03, V02, V02, V04, V05, V01.

The unique `VCODE` values are:

VCODE

------- V01 V02 V03 V04 V05

Question 1 (vii)

Find the output for the SQL query: SELECT A.VCODE, CNAME, VEHICLETYPE FROM TRAVEL A, VEHICLE B WHERE A. VCODE = B. VCODE and KM < 90;
Solution: This query joins the TRAVEL table (aliased as 'A') with the VEHICLE table (aliased as 'B') on their common column `VCODE`. It then filters the results to include only those records where the `KM` (kilometers travelled) in the TRAVEL table is less than 90. Finally, it displays the `VCODE` from the TRAVEL table, the `CNAME` from the TRAVEL table, and the `VEHICLETYPE` from the VEHICLE table for these filtered records.

Let's trace the records with KM < 90:

  • Travel record: (107, John Malina, 2015-02-10, 65, V04, 2) -> KM is 65. VCODE is V04.
  • Travel record: (104, Sahanubhuti, 2016-01-28, 90, V05, 4) -> KM is 90. This is NOT less than 90, so it's excluded.
  • Travel record: (102, Ravi Anish, 2016-01-13, 80, V02, 40) -> KM is 80. VCODE is V02.
Now, let's find the corresponding VEHICLETYPE for V04 and V02:
  • For V04: VEHICLETYPE is CAR.
  • For V02: VEHICLETYPE is AC DELUXE BUS.
So, the output will be:

A.VCODE | CNAME | VEHICLETYPE

-----------------------------------

V04 | John Malina | CAR

V02 | Ravi Anish | AC DELUXE BUS

Question 1 (viii)

Find the output for the SQL query: SELECT CNAME, KM*PERKM FROM TRAVEL A, VEHICLE B WHERE A.VCODE = B.VCODE AND A. VCODE = 'V05';
Solution: This query joins the TRAVEL table (aliased as 'A') with the VEHICLE table (aliased as 'B') using the `VCODE` column. It filters the results to include only records where the `VCODE` in the TRAVEL table is 'V05'. For these selected records, it displays the customer name (`CNAME`) and calculates the total charge by multiplying the kilometers travelled (`KM` from TRAVEL) by the per-kilometer charge (`PERKM` from VEHICLE).

First, let's find the relevant records:

  • From TRAVEL, the record with `VCODE` = 'V05' is: (104, Sahanubhuti, 2016-01-28, 90, V05, 4). Here, `CNAME` is 'Sahanubhuti', `KM` is 90, and `VCODE` is 'V05'.
  • From VEHICLE, the record with `VCODE` = 'V05' is: (V05, SUV, 30). Here, `PERKM` is 30.
Now, calculate `KM * PERKM`: 90 * 30 = 2700.

The output will be:

CNAME | KM*PERKM

--------------------------

Sahanubhuti | 2700

Common mistakes

  • Incorrect date format or comparison in WHERE clauses.
  • Errors in specifying table aliases during joins.
  • Misunderstanding the scope of GROUP BY and HAVING clauses.
  • Incorrectly applying aggregate functions without GROUP BY.
  • Syntax errors in SQL keywords or clauses.

Revision tips

  • Practice writing queries for various scenarios based on the provided table structures.
  • Pay close attention to the syntax of each SQL clause (SELECT, FROM, WHERE, ORDER BY, GROUP BY, HAVING).
  • Understand how to interpret the output of complex queries, especially those involving joins and aggregate functions.
  • Review the examples of both writing queries and finding outputs to cover all aspects.
  • Ensure correct use of date formats and comparison operators.

Practice MCQs

Q1. Which SQL clause is used to filter records based on a specified condition?

Q2. What is the purpose of the DISTINCT keyword in a SELECT statement?

Q3. Which clause is used to sort the result-set in ascending or descending order?

Q4. In SQL, which clause is used to group rows that have the same values in one or more columns?

Q5. What does the query `SELECT COUNT(*), VCODE FROM TRAVEL GROUP BY VCODE HAVING COUNT(*) > 1;` do?

Frequently asked questions

What is the main focus of Chapter 5 in CBSE Class 12 Computer Science?

Chapter 5 focuses on Structured Query Language (SQL), teaching students how to write and interpret queries for database management.

How do these NCERT Solutions help students prepare for exams?

These solutions provide clear, rewritten answers to common SQL query problems, reinforcing understanding of syntax, clauses, and practical application, which is crucial for exam success.

What types of SQL queries are covered in these solutions?

The solutions cover queries for selecting data, filtering with WHERE, sorting with ORDER BY, grouping with GROUP BY and HAVING, and interpreting results from joins and aggregate functions.

Are the original questions from the NCERT textbook preserved?

Yes, all original questions are kept exactly the same in terms of numbering and the problem asked, with expanded wording for clarity.

How are the solutions presented?

Each solution is rewritten to be more detailed and explanatory, guiding students through the steps and reasoning behind the SQL commands.

What are the key SQL clauses covered?

Key clauses covered include SELECT, FROM, WHERE, ORDER BY, GROUP BY, and HAVING, along with the DISTINCT keyword.

Content reviewed by the NCERT Help team. Editorial Team and update policy

NCERT Solutions PDF PDF on NCERT Help. URL unchanged for search indexing.