Skip to content
Question 89 of 95

Q.Write the output of any three SQL queries

(i) to
(iv) based on the tables COMPANY and CUSTOMER given below : Table : COMPANY CID C_NAME CITY PRODUCTNAME 111 SONY DELHI TV 222 NOKIA MUMBAI MOBILE 333 ONIDA DELHI TV 444 SONY MUMBAI MOBILE 555 BLACKBERRY CHENNAI MOBILE 666 DELL DELHI LAPTOP Table : CUSTOMER CUSTID CID NAME PRICE QTY C01 222 ROHIT SHARMA 70000 20 C02 666 DEEPIKA KUMARI 50000 10 C03 111 MOHAN KUMAR 30000 5 C04 555 RADHA MOHAN 30000 11
(i) SELECT PRODUCTNAME, COUNT() FROM COMPANY GROUP BY PRODUCTNAME HAVING COUNT()> 2;
(ii) SELECT NAME, PRICE, PRODUCTNAME FROM COMPANY C, CUSTOMER CU WHERE C.CID = CU.CID AND C_NAME = 'SONY';
(iii) SELECT DISTINCT CITY FROM COMPANY;
(iv) SELECT * FROM COMPANY WHERE C_NAME LIKE '%ON%';
Puducherry CbseCBSE Class XII Board 2023Subjective· 3mImportance★★★★★
94% · 89/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 three SQL queries retrieve: (i) product names appearing more than twice, (ii) customer details for SONY products, (iii) distinct cities where companies operate, and (iv) companies with 'ON' anywhere in their name.

Understanding the Tables and Queries

Before we execute any SQL query, we need to understand the structure of our two tables. The COMPANY table holds information about various companies, their locations, and the products they manufacture. Each company is identified by a unique CID (Company ID). The CUSTOMER table records purchase transactions, linking customers to companies through the CID foreign key, along with the price and quantity of items purchased.

The relationship between these tables is straightforward: CUSTOMER.CID references COMPANY.CID, allowing us to join information about which customer bought from which company.

Query (i): Grouping and Filtering Products

SELECT PRODUCTNAME, COUNT(*) 
FROM COMPANY 
GROUP BY PRODUCTNAME 
HAVING COUNT(*)> 2;

This query asks: which products appear more than twice in the COMPANY table? The GROUP BY clause collects all rows with the same PRODUCTNAME together, and COUNT(*) tallies how many companies manufacture each product. The HAVING clause then filters these groups, keeping only those where the count exceeds 2.

Looking at the COMPANY table:

  • TV appears in rows with CID 111 and 333 (2 times)
  • MOBILE appears in rows with CID 222, 444, and 555 (3 times)
  • LAPTOP appears in row with CID 666 (1 time)

Output:

PRODUCTNAMECOUNT(*)
MOBILE3

Only MOBILE satisfies the condition of appearing more than twice.

Query (ii): Joining Tables with a Condition

SELECT NAME, PRICE, PRODUCTNAME 
FROM COMPANY C, CUSTOMER CU 
WHERE C.CID = CU.CID AND C_NAME = 'SONY';

This query performs a join between COMPANY and CUSTOMER tables, retrieving customer names, prices paid, and product names—but only for purchases from SONY. The WHERE clause enforces two conditions: the CIDs must match (establishing the relationship), and the company name must be 'SONY'.

From COMPANY, SONY appears twice with CID 111 and CID 444. Now we check CUSTOMER for matching CIDs:

  • CID 111 links to customer MOHAN KUMAR who paid 30000 for a TV
  • CID 444 has no corresponding entry in CUSTOMER

Output:

NAMEPRICEPRODUCTNAME
MOHAN KUMAR30000TV
Note

The join only returns rows where both conditions are met. Since no customer record exists for CID 444 (SONY's MOBILE), that combination doesn't appear in the result.

Query (iii): Finding Unique Cities

SELECT DISTINCT CITY 
FROM COMPANY;

This is the simplest of the four queries. It asks for all unique cities where companies are located. The DISTINCT keyword eliminates duplicates, so even though DELHI appears three times in the COMPANY table, it will appear only once in the output.

Output:

CITY
DELHI
MUMBAI
CHENNAI

The order may vary depending on the database system, as no ORDER BY clause is specified.

Query (iv): Pattern Matching with LIKE

SELECT * FROM COMPANY …

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.