Q.Write SQL queries for
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.