Skip to content
Worked Examples · Example 9.20

Q.Let us use Customer relation shown in Table 9.10 to understand the working of string functions.

a) Display customer name in lower case and customer email in upper case from table CUSTOMER.
b) Display the length of the email and part of the email from the email id before the character '@'. Note - Do not print '@'.
c) Let us assume that four-digit area code is reflected in the mobile number starting from position number 3. For example, 1851 is the area code of mobile number 9818511338. Now, write the SQL query to display the area code of the customer living in Rohini.
d) Display emails after removing the domain name extension ".com" from emails of the customers.
e) Display details of all the customers having yahoo emails only.
Uttar Pradesh UpmspTextbookSubjective· 4mImportance★★★★★
37% · 35/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 is Example 9.20 of the textbook — it uses the real CUSTOMER table (Table 9.10) of the CARSHOWROOM database and demonstrates LOWER/UPPER, LENGTH/LEFT/INSTR, MID, TRIM(... FROM ...), and LIKE.

The real CUSTOMER table (Table 9.10)

CustIdCustNameCustAddPhoneEmail
C0001Amit SahaL-10, Pitampura4564587852amitsaha2@gmail.com
C0002RehnumaJ-12, SAKET5527688761rehnuma@hotmail.com
C0003Charvi Nayyar10/9, FF, Rohini6811635425charvi123@yahoo.com
C0004GurpreetA-10/2, SF, Mayur Vihar3511056125gur_singh@yahoo.com

There is no separate City column — a customer's locality (like "Rohini") is embedded inside the single CustAdd string, so any filter on locality must use LIKE, not =.


(a) Display customer name in lower case and customer email in upper case from table CUSTOMER.

SELECT LOWER(CustName), UPPER(Email) FROM CUSTOMER;

Output

LOWER(CustName)UPPER(Email)
amit sahaAMITSAHA2@GMAIL.COM
rehnumaREHNUMA@HOTMAIL.COM
charvi nayyarCHARVI123@YAHOO.COM
gurpreetGUR_SINGH@YAHOO.COM

(b) Display the length of the email and part of the email from the email id before the character '@'. Note — Do not print '@'.

SELECT LENGTH(Email), LEFT(Email, INSTR(Email, "@")-1) FROM CUSTOMER;

INSTR(Email, "@") finds the 1-based position of @; subtracting 1 gives how many characters come before it, and LEFT(Email, n) takes exactly that many characters from the start.

Output — every email in this table happens to be 19 characters long:

LENGTH(Email)LEFT(Email, INSTR(Email,"@")-1)
19amitsaha2
19rehnuma
19charvi123
19gur_singh

(c) Let us assume that four-digit area code is reflected in the mobile number starting from position number 3. For example, 1851 is the area code of mobile number 9818511338. Now, write the SQL query to display the area code of the customer living in Rohini.

SELECT MID(Phone, 3, 4)
FROM CUSTOMER
WHERE CustAdd LIKE '%Rohini%';

Only C0003 (Charvi Nayyar, address "10/9, FF, Rohini") matches. MID(Phone, 3, 4) on her number 6811635425 takes 4 characters starting at position 3: 1163.

Output

MID(Phone,3,4)
1163
Note

9818511338 in the question is only an illustrative example of the extraction method (its area code is 1851) — it is not any real customer's number in Table 9.10. The query itself runs against the real Phone column of whichever row matches CustAdd LIKE '%Rohini%'.

--- …

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.