Skip to content
Worked Examples · Example 1.3

Q.Using the CUSTOMER relation (Table 1.2) to understand the working of string functions:

(a) Display the customer name in lower case and the customer email in upper case from the CUSTOMER table.
(b) Display the length of the email, and the part of the email before the character '@' (do not print '@'). (Hint: use LENGTH(), LEFT() and INSTR().)
(c) A four-digit area code is present in the mobile number starting from position 3 (for example, 2630 is the area code in 4726309212). Write the SQL query to display the area code of the customer living in Rohini.
(d) Display the emails after removing the domain name extension '.com' from the customers' emails.
(e) Display the details of all the customers having yahoo emails only.
Uttar Pradesh UpmspTextbookSubjective· 3mImportance★★★★★
13% · 5/40 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 →

Five string-function queries on the real CUSTOMER table (Table 1.2): case conversion (LOWER/UPPER), length + substring extraction (LENGTH/LEFT/INSTR), a fixed-position substring (MID), removing a suffix (TRIM ... FROM), and a pattern filter (LIKE).

This is a code/query task. Every part works on the same four-row CUSTOMER table:

CustIdCustNameCustAddPhoneEmail
C0001AmitSahaL-10, Pitampura4564587852amitsaha2@gmail.com
C0002RehnumaJ-12, SAKET5527688761rehnuma@hotmail.com
C0003CharviNayyar10/9, FF, Rohini6811635425charvi123@yahoo.com
C0004GurpreetA-10/2, SF, MayurVihar3511056125gur_singh@yahoo.com

(a) Case conversion. LOWER()/LCASE() and UPPER()/UCASE() change every character's case:

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

(b) Length and the part before '@'. INSTR(Email, "@") returns the 1-based position of @ in each address (position 10 for amitsaha2@gmail.com). LEFT(Email, that position - 1) then returns everything up to, but excluding, the @ itself — the -1 is what keeps @ out of the result.

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

Forgetting the -1 in LEFT(Email, INSTR(Email,"@")) would include the @ itself in the output — the question explicitly says "do not print '@'".

(c) A fixed-position substring. MID(string, pos, n) (also SUBSTRING/SUBSTR) pulls n characters starting at position pos. The area code sits at position 3, 4 characters long:

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

Only CharviNayyar's address contains "Rohini"; her Phone is 6811635425, and MID(Phone, 3, 4) reads characters 3-6: 1163.

(d) Removing a suffix. TRIM() isn't only for whitespace — TRIM(remstr FROM string) strips a specific substring:

SELECT TRIM(".com" FROM Email) FROM CUSTOMER;
``` …

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.