I want to join two table in such away that If there is no value against first table entry in the second table then it should fetch that row but insert some custom text in that row. Is there any possibility of achieving this any idea would be appreciated. For Example Table 1 have Columns A(primary key), B, C and Table 2 have Columns D(Primcary key), A(foreign key), E; I want Result Like this enter image description here

  • Please add some data example and expected result Commented Feb 8, 2023 at 11:41
  • @ErgestBasha check now
    – Manii
    Commented Feb 8, 2023 at 11:59
  • Help me write this query in SQL
    – Andriy M
    Commented Feb 8, 2023 at 12:30
  • Are you looking for COALESCE(...) or IFNULL(...)?
    – Rick James
    Commented Feb 9, 2023 at 2:09
  • I am looking for IFNULL(.....)
    – Manii
    Commented Feb 9, 2023 at 5:52

1 Answer 1


You are looking for a case expression.

Consider the following data which can be found on this fiddle

create table table1(
  A varchar(10),
  B varchar(10),
  C varchar(10) ) ;
create table table2(
  D varchar(10),
  A varchar(10) ) ;  
insert into table1 values

insert into table2 values


select t1.A,
       case when t2.D is null then 'Add some text' end as D
from table2 t2
left join table1 t1 using(A);

Note. You need to use left join to get the null results from the table2

  • IFNULL(t2.D, 'Replacement text') is way more convenient
    – Kondybas
    Commented Feb 8, 2023 at 12:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.