Skip to content
Question 95 of 95

Q.Write SQL queries for

(i) to
(iv) and write outputs for SQL queries
(v) to (viii), which are based on the table given below: Table : TRAINS TNO TNAME START END 11096 Ahimsa Express Pune Junction Ahmedabad Junction 12015 Ajmer Shatabdi New Delhi Ajmer Junction 1651 Pune Hbj Special Pune Junction Habibganj 13005 Amritsar Mail Howrah Junction Amritsar Junction 12002 Bhopal Shatabdi New Delhi Habibganj 12417 Prayag Raj Express Allahabad Junction New Delhi 14673 Shaheed Express Jaynagar Amritsar Junction 12314 Sealdah Rajdhani New Delhi Sealdah 12498 Shane Punjab Amritsar Junction New Delhi 12451 Shram Shakti Express Kanpur Central New Delhi 12030 Swarna Shatabdi Amritsar Junction New Delhi Table : PASSENGERS PNR TNO PNAME GENDER AGE TRAVELDATE P001 13005 R N AGRAWAL MALE 45 2018-12-25 P002 12015 P TIWARY MALE 28 2018-11-10 P003 12015 S TIWARY FEMALE 22 2018-11-10 P004 12030 S K SAXENA MALE 42 2018-10-12 P005 12030 S SAXENA FEMALE 35 2018-10-12 P006 12030 P SAXENA FEMALE 12 2018-10-12 P007 13005 N S SINGH MALE 52 2018-05-09 P008 12030 J K SHARMA MALE 65 2018-05-09 P009 12030 R SHARMA FEMALE 58 2018-05-09 NOTE : All Dates are given in 'YYYY-MM-DD' format.
(i) To display details of all Trains which Start from New Delhi.
(ii) To display the PNR, PNAME, GENDER and AGE of all Passengers whose AGE is below 50.
(iii) To display total number of MALE and FEMALE Passengers.
(iv) To display details of all Passengers travelling in Trains whose TNO is 12015.
(v) SELECT MAX (TRAVELDATE), MIN(TRAVELDATE) FROM PASSENGERS WHERE GENDER = 'FEMALE';
(vi) SELECT END, COUNT() FROM TRAINS GROUP BY END HAVING COUNT()>1;
(vii) SELECT DISTINCT TRAVELDATE FROM PASSENGERS;
(viii) SELECT TNAME, PNAME FROM TRAINS T, PASSENGERS P WHERE T.TNO = P.TNO AND AGE BETWEEN 50 AND 60;
Uttar Pradesh UpmspCBSE Class XII Board 2019Subjective· 8mImportance★★★★★
100% · 95/95 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

This response provides SQL queries for specific data retrieval and manipulation tasks, along with the outputs for given queries, based on the provided TRAINS and PASSENGERS tables.

Understanding how to interact with databases using SQL (Structured Query Language) is fundamental in data management. SQL allows us to retrieve, insert, update, and delete data, making it a powerful tool for anyone working with information systems. The core idea behind these queries is to precisely tell the database what information you need and under what conditions.

Let's break down the requests, focusing on the logic behind each SQL statement.

SQL Queries (i) to (iv)

These tasks require us to write SQL SELECT statements, which are used to retrieve data from one or more tables. The key is to identify the correct table, the columns to display, and any conditions to filter the rows.

(i) To display details of all Trains which Start from New Delhi.

To achieve this, we look at the TRAINS table and select all columns (*) for trains where the START column has the value 'New Delhi'.

SELECT *
FROM TRAINS
WHERE START = 'New Delhi';
(ii) To display the PNR, PNAME, GENDER and AGE of all Passengers whose AGE is below 50.

We select the PNR, PNAME, GENDER, and AGE columns from the PASSENGERS table, only for passengers whose AGE is less than 50.

SELECT PNR, PNAME, GENDER, AGE
FROM PASSENGERS
WHERE AGE < 50;
(iii) To display total number of MALE and FEMALE Passengers.

Group the passengers by their GENDER and count how many fall into each category. The GROUP BY clause is essential for aggregation functions like COUNT().

SELECT GENDER, COUNT(*)
FROM PASSENGERS
GROUP BY GENDER;
(iv) To display details of all Passengers travelling in Trains whose TNO is 12015.

Select all details (*) from the PASSENGERS table where the TNO (Train Number) is 12015.

SELECT *
FROM PASSENGERS
WHERE TNO = 12015;

Outputs for SQL Queries (v) to (viii)

For these parts, we are given the SQL queries and determine what data they return based on the provided TRAINS and PASSENGERS tables.

(v) SELECT MAX (TRAVELDATE), MIN(TRAVELDATE) FROM PASSENGERS WHERE GENDER = 'FEMALE';

This finds the latest (MAX) and earliest (MIN) TRAVELDATE among female passengers.

Female passengers and their travel dates:

  • P003 (S TIWARY): 2018-11-10
  • P005 (S SAXENA): 2018-10-12
  • P006 (P SAXENA): 2018-10-12
  • P009 (R SHARMA): 2018-05-09

The maximum is 2018-11-10 and the minimum is 2018-05-09.

MAX(TRAVELDATE)MIN(TRAVELDATE)
2018-11-102018-05-09
(vi) SELECT END, COUNT(*) FROM TRAINS GROUP BY END HAVING COUNT(*)>1;

Group the TRAINS by their END station, count trains per group, and keep only groups with more than one train.

Counting the END stations:

  • Ahmedabad Junction: 1
  • Ajmer Junction: 1
  • Habibganj: 2
  • Amritsar Junction: 2
  • New Delhi: 4 (Prayag Raj Express, Shane Punjab, Shram Shakti Express, Swarna Shatabdi)
  • Sealdah: 1 …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.