Skip to content
Question 93 of 95

Q.Write outputs for SQL queries

(i) to (iii), which are based on the following tables, CUSTOMERS and PURCHASES: Table : CUSTOMERS CNO CNAME CITIES C1 SANYAM DELHI C2 SHRUTI DELHI C3 MEHER MUMBAI C4 SAKSHI CHENNAI C5 RITESH INDORE C6 RAHUL DELHI C7 AMEER CHENNAI C8 MINAKSHI BANGALORE C9 ANSHUL MUMBAI Table : PURCHASES SNO QTY PUR_DATE CNO S1 15 2018-12-25 C2 S2 10 2018-11-10 C1 S3 12 2018-11-10 C4 S4 7 2019-01-12 C7 S5 11 2019-02-12 C2 S6 10 2018-10-12 C6 S7 5 2019-05-09 C8 S8 20 2019-05-09 C3 S9 8 2018-05-09 C9 S10 15 2018-11-12 C5 S11 6 2018-08-04 C7
(i) SELECT COUNT(DISTINCT CITIES) FROM CUSTOMERS;
(ii) SELECT MAX(PUR_DATE) FROM PURCHASES;
(iii) SELECT CNAME, QTY, PUR_DATE FROM CUSTOMERS, PURCHASES WHERE CUSTOMERS.CNO = PURCHASES.CNO AND QTY IN (10,20);
Uttarakhand UbseCBSE Class XII Board 2020Subjective· 3mImportance★★★★★
98% · 93/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 →

The queries count distinct cities, find the latest purchase date, and list customer names, quantities, and purchase dates for specific quantities.

In database management, understanding the structure of tables and how they relate is fundamental to extracting meaningful information. Each table typically has a Primary Key, a column (or set of columns) that uniquely identifies each record. This key ensures data integrity and allows for efficient retrieval and linking of information across different tables.

For instance, in the CUSTOMERS table, CNO (Customer Number) serves as the Primary Key. Each customer has a unique CNO, ensuring that we can distinguish between SANYAM (C1) and SHRUTI (C2), even if they share the same city. This CNO then appears in the PURCHASES table as a Foreign Key, linking each purchase record back to the specific customer who made it. This relationship is crucial for queries that involve data from both tables, such as finding out which customer made a particular purchase.

Important

A Primary Key uniquely identifies each record in a table, while a Foreign Key establishes a link between two tables by referencing the Primary Key of another table. This relational structure is the backbone of relational databases.

Let's now examine the specific SQL queries and their outputs, understanding the logic behind each operation.

Query (i): SELECT COUNT(DISTINCT CITIES) FROM CUSTOMERS;

This query aims to determine the number of unique cities from which our customers originate. The DISTINCT keyword is vital here; without it, the query would simply count every entry in the CITIES column, including duplicates. By specifying DISTINCT CITIES, we instruct the database to consider only the unique city names before counting them.

Looking at the CUSTOMERS table:

  • DELHI appears multiple times (for C1, C2, C6).
  • MUMBAI appears twice (for C3, C9).
  • CHENNAI appears twice (for C4, C7).
  • INDORE appears once (for C5).
  • BANGALORE appears once (for C8).

The unique cities are DELHI, MUMBAI, CHENNAI, INDORE, and BANGALORE. Counting these distinct entries gives us the total number of unique cities.

Output for Query (i):

COUNT(DISTINCT CITIES)
5

Query (ii): SELECT MAX(PUR_DATE) FROM PURCHASES;

This query seeks to identify the latest date on which a purchase was made. The MAX() aggregate function is used to find the highest value within a specified column. For date columns, MAX() returns the most recent date.

Examining the PUR_DATE column in the PURCHASES table:

  • 2018-12-25
  • 2018-11-10
  • 2018-11-10
  • 2019-01-12
  • 2019-02-12
  • 2018-10-12
  • 2019-05-09
  • 2019-05-09
  • 2018-05-09
  • 2018-11-12
  • 2018-08-04

By comparing all these dates, the latest date recorded is 2019-05-09.

Output for Query (ii):

MAX(PUR_DATE)
2019-05-09

Query (iii): SELECT CNAME, QTY, PUR_DATE FROM CUSTOMERS, PURCHASES WHERE CUSTOMERS.CNO = PURCHASES.CNO AND QTY IN (10,20);

This query is more complex, involving a join between two tables and a filtering condition. …

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.