← All SQL interview questions

Pick the best contact channel for every customer

EasyNULL / NOT NULL HandlingZomatoEXL

The CRM team is preparing a re-engagement campaign. Each contact may have an email, a phone number, both, or neither, and some records also have no city captured.

From crm_contacts, return contact_id, full_name, city and a new column primary_contact for every contact whose city is known. primary_contact should be the email when present, otherwise the phone number, otherwise the text NO CONTACT DETAILS. Order by contact_id.

Sample data

Table: crm_contacts

12 row(s) · showing first 8

contact_idINTfull_nameTEXTemailTEXTphoneTEXTcityTEXT
1Aarav Mehtaaarav@example.in9810011122Delhi
2Bhavna RaoNULL9820033344Mumbai
3Chirag Jainchirag@example.inNULLJaipur
4Divya NairNULLNULLKochi
5Esha Kulkarniesha@example.in9845055566NULL
6Farhan QureshiNULL9830077788Kolkata
7Gauri Desaigauri@example.inNULLPune
8Harsh VoraNULLNULLNULL