Skip to content
Question 94 of 95

Q.Write SQL queries for

(i) to (iv), which are based on the tables: CUSTOMERS and PURCHASES given in the question 4(g):
(i) To display details of all CUSTOMERS whose CITIES are neither Delhi nor Mumbai.
(ii) To display the CNAME and CITIES of all CUSTOMERS in ascending order of their CNAME.
(iii) To display the number of CUSTOMERS along with their respective CITIES in each of the CITIES.
(iv) To display details of all PURCHASES whose Quantity is more than 15.
Yanam BieapCBSE Class XII Board 2020Subjective· 4mImportance★★★★★
99% · 94/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 →

Four SQL queries on the CUSTOMERS (CNO, CNAME, CITIES) and PURCHASES (SNO, QTY, PUR_DATE, CNO) tables: filter with WHERE ... NOT IN, sort with ORDER BY, group with GROUP BY ... COUNT(*), and filter a numeric column with WHERE QTY > 15.

Understanding the task

These queries use the same CUSTOMERS and PURCHASES tables as the previous question. CUSTOMERS has columns CNO, CNAME and CITIES; PURCHASES has SNO, QTY, PUR_DATE and CNO. Each query must use these exact column names — referring to a column that does not exist (for example CITY instead of CITIES, or Quantity instead of QTY) would raise an error.

(i) Customers not in Delhi or Mumbai

"Neither Delhi nor Mumbai" means exclude both cities. NOT IN is the cleanest way to exclude a list of values:

SELECT * FROM CUSTOMERS
WHERE CITIES NOT IN ('Delhi', 'Mumbai');

An equivalent form uses two conditions joined by AND:

SELECT * FROM CUSTOMERS
WHERE CITIES <> 'Delhi' AND CITIES <> 'Mumbai';

Both return every customer whose CITIES value is not Delhi and not Mumbai (the Chennai, Indore and Bangalore customers).

(ii) Names and cities, sorted by name

Select just CNAME and CITIES and sort by name. ORDER BY sorts the result; ascending is the default, but stating ASC makes the intent clear:

SELECT CNAME, CITIES FROM CUSTOMERS
ORDER BY CNAME ASC;

(iii) Customer count per city

To count how many customers belong to each city, GROUP BY CITIES collapses all rows of the same city into one group, and COUNT(*) tallies the rows in each group:

SELECT CITIES, COUNT(*) AS NO_OF_CUSTOMERS
FROM CUSTOMERS
GROUP BY CITIES;
``` …

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.