PWC SQL Interview Question for a Data Analyst Position | SQL For Analytics

Поделиться
HTML-код
  • Опубликовано: 22 дек 2023
  • In this video we will solve a SQL interview problem asked in PWC. We will solve it using 2 methods. Here is the script :
    create table source(id int, name varchar(5))
    create table target(id int, name varchar(5))
    insert into source values(1,'A'),(2,'B'),(3,'C'),(4,'D')
    insert into target values(1,'A'),(2,'B'),(4,'X'),(5,'F');
    Zero to hero(Advance) SQL Aggregation:
    • All About SQL Aggregat...
    Most Asked Join Based Interview Question:
    • Most Asked SQL JOIN ba...
    Solving 4 Trick SQL problems:
    • Solving 4 Tricky SQL P...
    Data Analyst Spotify Case Study:
    • Data Analyst Spotify C...
    Top 10 SQL interview Questions:
    • Top 10 SQL interview Q...
    Interview Question based on FULL OUTER JOIN:
    • SQL Interview Question...
    Playlist to master SQL :
    • Complex SQL Questions ...
    Rank, Dense_Rank and Row_Number:
    • RANK, DENSE_RANK, ROW_...
    #sql #dataanalytics

Комментарии • 97

  • @ankitbansal6
    @ankitbansal6  6 месяцев назад +13

    Please do hit the like button on the video for more interview questions.

  • @grim_rreaperr
    @grim_rreaperr 6 месяцев назад +9

    SELECT
    COALESCE(a.id,
    b.id) AS id,
    CASE WHEN a.name IS NULL THEN 'New in target'
    WHEN b.name IS NULL THEN 'New in Source'
    ELSE 'Mismatch' END AS comment
    FROM source AS a
    FULL OUTER JOIN
    target AS b
    ON a.id = b.id
    WHERE a.Name b.Name OR a.id IS NULL or b.id IS NULL;

  • @vijaypratap8173
    @vijaypratap8173 6 месяцев назад

    You are the best mentor for me👏🙏

  • @Manik65993
    @Manik65993 6 месяцев назад +3

    A big fan of your SQL play list.. Really helpful.. Thank you so much..

  • @Bibble442
    @Bibble442 6 месяцев назад +2

    Hey man, great video. I'm a BI developer, and i still like yo watch tutorials to learn new stuff or see if i already know how to solve the problem as i still consider myself somewhat new to professional coding. Definitely subscribing and keep up the good content!

    • @Bibble442
      @Bibble442 6 месяцев назад

      Hi dad ur using my account-

    • @ankitbansal6
      @ankitbansal6  6 месяцев назад

      Cool, thanks!

  • @avi8016
    @avi8016 6 месяцев назад

    Thanks for bringing your unique perspective while solving the problems!!!

  • @d.g0101
    @d.g0101 6 месяцев назад

    Thanks Ankit, solved this in one go. Your vidoes are quite helpful.

  • @dhyeypatel1335
    @dhyeypatel1335 6 месяцев назад

    I was asked the same question during the Nineleaps interview, and I successfully solved it.
    with cte as(
    select s.id as s_id,s.name as s_name,t.id as t_id,t.name as t_name
    from source s
    full outer join target t on s.id = t.id
    where s.id is null or t.id is null or s.name != t.name)
    select coalesce(s_id,t_id), case when t_id is null then 'new in source'
    when s_id is null then 'new in target'
    else 'Mismatch' end as comment
    from cte
    This was my approach

  • @thapelomarakalla
    @thapelomarakalla 6 месяцев назад +2

    Sir, my query is as follows:
    with cte as
    (
    select a.id a_id, a."Name" a_name, b.id b_id, b."Name" b_name
    from source a
    full outer join target b
    on a.id = b.id
    )
    select case when a_id is not null then a_id else b_id end id,
    case when a_name is not null and b_name is null then 'New Source'
    when a_name is null and b_name is not null then 'New Target'
    else 'Mismatch' end comment
    from cte
    where (a_id, b_id) not in(select a_id, b_id from cte where a_name = b_name)

  • @riya1703
    @riya1703 2 месяца назад

    You have earned a new subscriber 🎉

  • @baskarandurai6702
    @baskarandurai6702 6 месяцев назад

    You have big fanbase in our company Ankit.. Every time when new joinee came, blindly i am recommending your content only and they become your fan❤❤❤..

    • @ankitbansal6
      @ankitbansal6  6 месяцев назад

      Thank you. Means a lot to me ❤️

  • @pratik903
    @pratik903 Месяц назад

    Hey Ankit
    I solved it using below query. It uses window function:
    with all_id as (
    select id, name, 'source' as flag from source union all select id,name, 'target' as flag from target),
    flag as (
    select *,
    count(1) over (partition by id,name) as cnt_1,
    count(1) over (partition by id) as cnt_2
    from all_id)
    select
    id,
    max(case when cnt_1 =cnt_2 and flag='source' then 'New in Source'
    when cnt_1=cnt_2 and flag='target' then 'New in target'
    else 'Mismatch' end) as Comment
    from flag where cnt_1!=2
    group by id;

  • @jjayeshpawar
    @jjayeshpawar 6 месяцев назад +6

    Hi Ankit, thanks for making SQL very easy. Just one request,
    Can you please make video on how to approach a problem in interviews? Because in interviews we have to write query in notepad and we can't run the parts of the query. We have to visualise everything while writing. If you will make this video, It will be very useful for everyone.
    THANKS

    • @ankitbansal6
      @ankitbansal6  6 месяцев назад +3

      That will come with practice 🙂

  • @Vonneuman8676
    @Vonneuman8676 6 месяцев назад +1

    Great

  • @yoyojohnny3776
    @yoyojohnny3776 6 месяцев назад

    Today you have earned a new subscriber 🎉

  • @radhikagupta7314
    @radhikagupta7314 3 месяца назад

    with sample1 as (
    select * from source
    minus
    select * from target
    ),
    sample2 as (
    select * from target
    minus
    select * from source
    )
    select coalesce(s1.id,s2.id) as id
    ,case
    when s1.name s2.name then 'its a mismatch'
    when s1.name is null then 'new in target'
    when s2.name is null then 'new in source'
    end as "comment" from
    sample1 s1 full join
    sample2 s2 on
    s1.id=s2.id

  • @ritukumari1636
    @ritukumari1636 5 месяцев назад

    Hi Ankit... Thanks for the great videos.Along with multiple solutions for the query if you can explain the performance of all solutions..that would be great.As a developer I also need to write efficient queries.

  • @ankushjain4128
    @ankushjain4128 6 месяцев назад

    Without checking your solution i tried very old way ..
    select id,name ,'New in source' from source where id not in (select distinct id from target )
    UNION ALL
    select id ,name ,'New in Traget' from target where id not in (select distinct id from source )
    UNION ALL
    select s.id ,s.name ,'MisMatch' from source as s join target as t on s.id=t.id and s.name!=t.name
    order by id
    After checking solution i realized there are lot of improvement needed.

  • @ANKITSINGH-ti9ib
    @ANKITSINGH-ti9ib 3 месяца назад

    Thanks

  • @shraddhajain7753
    @shraddhajain7753 5 месяцев назад +2

    (select id, 'new in source' as comment from source where id not in (select id from target))
    union
    (select id, 'new in target' as comment from target where id not in (select id from source))
    union
    (select s.id, 'mismatch' as comment from source s join target t on s.id = t.id and s.name != t.name)

  • @dfkgjdflkg
    @dfkgjdflkg 6 месяцев назад

    I am a fan too, we just don't spend time to express a thank you for all you have tought

  • @swapnam1528
    @swapnam1528 2 месяца назад

    2nd one is awsome

  • @rickway2039
    @rickway2039 6 месяцев назад

    I'm curious what the explain plans look like for both these queries.

  • @mr.pingpong502
    @mr.pingpong502 7 дней назад

    with cte as (
    select *,'Source' as Location from source where id not in (select a.id from source a inner join target b on a.id=b.id and a.name=b.name)
    union all
    select *,'target' as Location from target where id not in (select a.id from source a inner join target b on a.id=b.id and a.name=b.name)
    )
    select distinct a.id,case when occurance>1 then 'Mismatch' when lower(Location)='source' then 'New in Source' when lower(Location)='target' then 'New in Source' else null end as comments
    from cte a inner join (select id,count(id) as occurance from cte group by id )b on a.id=b.id

  • @nandan7755
    @nandan7755 6 месяцев назад

    💯💯❤

  • @vishalsonawane.8905
    @vishalsonawane.8905 2 месяца назад

    Done

  • @nikhilsingh1296
    @nikhilsingh1296 6 месяцев назад +2

    Hi Ankit Sir, I am working as a customer service professional and my day is an Uber Driver, I really don’t want him working so much at this age and I wanna support my family financially. I have been following your videos and have learnt a lot of SQL, I want to master this skill for my life, please suggest me a course of yours which will help me. I want to tell SQL as my Strength in all of the interviews. I want a live class. Please suggest

    • @ankitbansal6
      @ankitbansal6  6 месяцев назад

      Live class will be in March. For now you can take the recorded course from namaste SQL , zero to hero one. Later if you want you can attend live classes by paying the difference.

    • @nikhilsingh1296
      @nikhilsingh1296 6 месяцев назад

      Thanks for suggesting, but I wanna inform that I know a little SQL Basics, but I get confused with Joins and complex problem, do I have to take Zero to Hero SQL recorded, right?@@ankitbansal6

  • @Its_me_DOt
    @Its_me_DOt 6 месяцев назад

    HI ankit,
    Thank you for another excellent sql solved problem
    Everytime i am watch your videos makes me confidence.
    once we see sql query instantly how we think this method we can use it for this query?
    there where i lack. Please advice...Thank you

    • @ankitbansal6
      @ankitbansal6  6 месяцев назад

      Practice practice practice. Solve all my questions at least 2 times without checking the solution

    • @Its_me_DOt
      @Its_me_DOt 6 месяцев назад

      @@ankitbansal6 Thank you!🙂 Mentor

  • @supriyasengar5796
    @supriyasengar5796 6 месяцев назад

    Hi Ankit,
    Below query is also correct for this solution, right?
    Please correct me if i am wrong.
    select a.id, comment
    from
    (select coalesce(s.id,t.id) as id,
    CASE when t.id is null then 'New in Source'
    when s.id is null then 'New in Target'
    when t.name != s.name then 'Mismatch'
    END AS Comment
    from source s
    full join target t on s.id = t.id) a
    where a.comment is not null;
    Thanks!

  • @DEwithDhairy
    @DEwithDhairy 6 месяцев назад +1

    PySpark Approach and Solution explanation video for this problem:
    ruclips.net/video/JlPG-oIAK2E/видео.htmlsi=BptXwtPBjrHUrhQ4

  • @Ashu23200
    @Ashu23200 Месяц назад

    some more challenges please.

    • @ankitbansal6
      @ankitbansal6  Месяц назад +1

      Here you go
      Complex SQL Questions for Interview Preparation: ruclips.net/p/PLBTZqjSKn0IeKBQDjLmzisazhqQy4iGkb

  • @PrakashSingh-fh3uf
    @PrakashSingh-fh3uf 6 месяцев назад

    with cte as (
    select id, name ,case when id is not null then 'new source' end as common from source
    where id not in (select id from target)
    union all
    select id, name, case when id is not null then 'new target' end as common from target
    where id not in (select id from source)
    union all
    select a.id, a.name, case when a.id is not null then 'is mismatch' end as common from target a join source b
    on a.id = b.id
    where a.name != b.name
    )
    select distinct id , common from cte

  • @LoveIsLifeTurkishIndia
    @LoveIsLifeTurkishIndia 4 месяца назад

    This will done using case statement?

  • @mayankk1726
    @mayankk1726 6 месяцев назад +1

    Hi Ankit, i would like to be excel in sql hence planing to take your course therefore could you please let me know about the process of the same and also ur courses are recorded sessions or live one ?

    • @ankitbansal6
      @ankitbansal6  6 месяцев назад

      Recorded
      www.namastesql.com/courses/SQL-For-Analytics-6301f405e4b0238f71788354

  • @abhishekgarg9029
    @abhishekgarg9029 6 месяцев назад +2

    What experience-level Data Analysts can expect such questions Ankit?

    • @ankitbansal6
      @ankitbansal6  6 месяцев назад +2

      3 plus years

    • @abhishekgarg9029
      @abhishekgarg9029 6 месяцев назад +1

      Me being able to solve such problems with 1.25yrs…credits to namaste sql 😉

  • @pranavipulipaka2243
    @pranavipulipaka2243 3 месяца назад

    with cte_1 as(
    select id
    from source
    where id not in (select id from target)),
    cte_2 as(
    select id
    from target
    where id not in (select id from source)),
    cte_3 as(
    select id
    from source
    where id in (select id from target) and name not in (select name from target))
    select id,'new in source' as "comment"
    from cte_1
    union
    select id,'new in target' as "comment"
    from cte_2
    union
    select id,'mismatch' as "comment"
    from cte_3

    • @AnuragYadav-jw1dq
      @AnuragYadav-jw1dq 3 месяца назад

      bhai , kya solution nikala salute . Jo data chahiye wo seggregate krke label lga do .

  • @mahimkhan134
    @mahimkhan134 6 месяцев назад

    with
    cte_new_in_source as (select id,'New in Source' from source where id not in (select id from target)),
    cte_new_in_target AS (select id,'New in Targer' from target where id NOT IN (Select id from source)),
    cte_mismatch as (select s.id, 'Mismatch' from source s inner join target t on s.id=t.id and s.name!=t.name)
    select * from cte_new_in_source union select * from cte_new_in_target union select * from cte_mismatch

  • @2412_Sujoy_Das
    @2412_Sujoy_Das 6 месяцев назад +1

    Sir, my query is as follows:
    with cte_1 as
    (Select A.id as source_id, B.id as target_id,
    CASE WHEN B.name is null THEN 'New in Source' WHEN A.name is null THEN 'New in Target' ELSE 'Mismatch' END as Comment
    FROM source A
    FULL OUTER JOIN target B
    ON A.id = B.id
    WHERE A.name!=B.name OR A.name is null OR B.name is null)
    Select * from
    (Select source_id as id, Comment from cte_1
    UNION
    Select target_id as id, Comment from cte_1)xyz
    WHERE id is not null;

    • @ankitbansal6
      @ankitbansal6  6 месяцев назад

      You can do simply with full outer join single query. Check my solution.

    • @2412_Sujoy_Das
      @2412_Sujoy_Das 6 месяцев назад

      @@ankitbansal6 Yes sir...I checked....

  • @thedominatorscircle6762
    @thedominatorscircle6762 6 месяцев назад

    Sir any suggestions for fresher in sql.
    I completed these topics DDL, DML, DCL, TCL and also join.

    • @ankitbansal6
      @ankitbansal6  6 месяцев назад

      Join , aggregation, case when

  • @jayselokar
    @jayselokar 6 месяцев назад

    my query got a bit lengthy because I considered names first, then had to change for ID
    with cte as ( select s.id as sid, s.name as sn,t.id as tid, t.name as tn from source as s
    full join target as t on s.id = t.id)
    , cte2 as (select sn, tn, sid, tid,
    case when sn = tn then 'existing'
    when sn != tn then 'mismatch'
    when sn is not null and tn is null then 'new in source'
    when sn is null and tn is not null then 'new in taret'
    end as outs
    from cte)
    SELECT sid, outs FROM cte2
    WHERE outs != 'existing' and sid is not null
    UNION
    SELECT tid, outs FROM cte2
    WHERE outs != 'existing' and tid is not null

  • @cryofficial2873
    @cryofficial2873 5 месяцев назад

    Hello Ankit this is my solution. Please tell how it is
    select id,'New In Source' Comment from source
    where id not in (select id from target)
    UNION ALL
    select id,'New In Target'Comment from target
    where id not in (Select id from source)
    UNION ALL
    select source.id,'MISMATCH'Comment from source LEFT JOIN target ON source.id=target.id
    where source.name not in(Select name from target) and source.id in (select id from target)

  • @rahulsah207
    @rahulsah207 5 месяцев назад

    Why min(table_name) gives 'target' instead of 'source' if we are seeing it lexicographically

  • @hiralalpatra500
    @hiralalpatra500 6 месяцев назад +1

    select id,'New in source' as comment from source
    where id not in(select id from target) and name not in (select name from target)
    union
    select id,'New in target' as comment from target
    where id not in(select id from source) and name not in (select name from source)
    union
    select id,'Mismatch' as comment from source
    where id in(select id from target) and name not in (select name from target)
    union
    select id,'Mismatch' as comment from target
    where id in(select id from source) and name not in (select name from source)

    • @ankitbansal6
      @ankitbansal6  6 месяцев назад

      It's really a big solution. You can do it just with a simple full join. Watch the video. Good attempt though 👍

    • @hiralalpatra500
      @hiralalpatra500 6 месяцев назад

      @@ankitbansal6 ok sir❤️ …thank you so much

  • @lotus9229
    @lotus9229 5 месяцев назад

    where s.name != t.name or s.name is null or t.name is null , Isn't it better to use "where s.name is distinct from t.name" ??

  • @parikgrover2281
    @parikgrover2281 6 месяцев назад

    easy way of doing :-
    with cte as
    (select id,name from source
    union all
    select id,name from target)
    select distinct id,comments from(select id,case when id=3 then 'new in source'
    when id=5 then 'new in target'
    when id=4 then 'mismatch'
    end as comments from cte)
    where comments is not null;

    • @parikgrover2281
      @parikgrover2281 6 месяцев назад

      another very easy approach using diff join
      select t1.id,'new in source' as comments
      from source t1
      left outer join target t2
      on t1.id=t2.id
      where t1.id=3
      union all
      select t2.id,'new in target' as comments
      from source t1
      right outer join target t2
      on t1.id=t2.id
      where t2.id=5
      union all
      select t1.id,'mismatch' as comments
      from source t1
      join target t2
      on t1.id=t2.id
      where t1.id=4;

    • @reachrishav
      @reachrishav 6 месяцев назад +1

      You serious bro? 😂😂

    • @prabhatgupta6415
      @prabhatgupta6415 6 месяцев назад +1

      they r not dynamic@@reachrishav

  • @AmanRaj-uf7wx
    @AmanRaj-uf7wx 4 месяца назад

    Solution for MYSQL
    with cte as (
    select *, concat(id,name) as ct, 'ent_sou' as col_table from source
    union all
    select *, concat(id,name) as ct, 'ent_target' as col_table from target
    )
    select distinct id,
    case when (count(col_table) over (partition by id order by id)) =1 then col_table
    else 'mismatch' END AS comment
    from cte
    where ct not in (select concat(id,name) from target)
    or ct not in (select concat(id,name) from source)

  • @DeepakSharma-pn8yt
    @DeepakSharma-pn8yt Месяц назад

    Here is my solution :
    With CTE as(
    Select s.id as s_id, s.name as s_name, t.id as t_id, t.name as t_name
    from source s
    FULL outer join target as t
    on s.id = t.id)
    Select
    CASE
    when s_id is null then t_id else s_id end as id,final_status
    from (
    Select *,
    CASe when s_name = t_name and s_id = t_id then 'All_Match'
    When s_name != t_name and s_id = t_id then 'Name_Mismatch'
    when s_id is null and t_id is not null then 'New_in_target'
    when s_id is not null and t_id is null then 'New_in_Source'
    END as final_status
    from CTE) as a
    Where final_status != 'All_Match'

  • @ashutoshyadav8348
    @ashutoshyadav8348 6 месяцев назад

    with cte as (select s.id as s_id, s.name as s_name,
    t.id as t_id, t.name as t_name
    from source s
    full join target t
    on s.id = t.id)
    select * from(
    select coalesce(s_id,t_id) as id,
    case when t_id is null then 'new in source'
    when s_id = t_id and s_name t_name then 'mismatch'
    when s_id is null then 'new in target'end as comment
    from cte)a where comment is not null

  • @user-kz7hc9qx9j
    @user-kz7hc9qx9j 6 месяцев назад

    how can we execute this query in mysql work bench as full outer join is not working

    • @ankitbansal6
      @ankitbansal6  6 месяцев назад

      Use second approach

    • @arjundev4908
      @arjundev4908 5 месяцев назад

      do left join first and then right join and do a union all. you should see both common and un-common records.

  • @pmohantymohanty7
    @pmohantymohanty7 3 месяца назад

    ALTERNATIVE WAY (Joins, Union ,CASE)
    SELECT
    id,
    CASE
    WHEN Id = 3 THEN 'New in Source'
    WHEN ID = 5 THEN 'New in Target'
    WHEN ID = 4 THEN 'Mismatch'
    ELSE 'Not Found'
    END AS Comment
    FROM
    (
    select * FROM Source where id = 3
    UNION
    select * from Target Where id = 5
    UNION
    select * from Source where id = 4
    ) AS alias_table

  • @deeptisharma9154
    @deeptisharma9154 5 месяцев назад

    The first solution is easy but while using MYSQL full outer join is not available......what can be done in MYSQL?

    • @rohanjamea2446
      @rohanjamea2446 4 месяца назад +1

      First left join then union with right join. Hope you figured it out before itself

  • @florincopaci6821
    @florincopaci6821 6 месяцев назад +1

    Hello, in a hurry, if I was in an interview, I would have answered like this - this is the first thought:
    with notinsource as (select a.id, case when not exists(select * from target where a.id=id )then 'new in source' end as 'comments'
    from source a
    where case when not exists(select * from target where a.id=id )then 'new in source' end= 'new in source' )
    , notintarget as (select a.id, case when not exists(select * from source where a.id=id )then 'new in target' end as 'comments'
    from target a
    where case when not exists(select * from source where a.id=id )then 'new in target' end= 'new in target' )
    , missmatch as(select a.id , case when exists(select * from target where a.id=id and a.namename)then 'missmatch' end as 'comments'
    from source a
    where case when exists(select * from target where a.id=id and a.namename)then 'missmatch' end='missmatch' )
    select * from notinsource
    union all
    select * from notintarget
    union all
    select * from missmatch

    • @ankitbansal6
      @ankitbansal6  6 месяцев назад +2

      That's very long. I think your first click during the interview should be full outer join.

    • @florincopaci6821
      @florincopaci6821 6 месяцев назад

      @@ankitbansal6 I will follow your advice.thank you

    • @florincopaci6821
      @florincopaci6821 6 месяцев назад

      @@ankitbansal6 Now i see the second method which is really great and i thank you for that but if someone answer with this method in an interview having emotions is quite good in sql.
      Thank you

    • @vishwajitkumar3992
      @vishwajitkumar3992 6 месяцев назад

      Hi Ankit thank you for making the video on this question. The way you approach the question makes it easy to understand. Thank you again ❤

  • @shraddhajain7753
    @shraddhajain7753 5 месяцев назад

    select case when s.id = s.id then s.id when t.id = t.id then t.id end as id, case when s.name = s.name then 'new in source' when t.name = t.name then 'new in target' when s.name != t.name then 'mismatch'
    end as comment
    from source s full outer join target t on s.id = t.id where s.name != t.name or s.name is null or t.name is null

  • @user-xd1tb6fg2d
    @user-xd1tb6fg2d 6 месяцев назад

    select s.id,
    case when s.id in(select id from target) and s.name not in(select name from target) then 'mismatch'
    when s.id in(select id from target) then 'source and target'
    when s.id not in(select id from target) then 'new in source'
    end comment
    from source s
    where comment!='source and target'
    union
    select t.id,
    case when t.id in(select id from source) and t.name not in(select name from source) then 'mismatch'
    when t.id in(select id from source) then 'source and target'
    when t.id not in(select id from source) then 'new in target'
    end comment
    from target t
    where comment!='source and target'

  • @ManpreetSingh-tv3rw
    @ManpreetSingh-tv3rw 6 месяцев назад

    Gave a try using your beloved CTE's
    with cte1 as (select id,'New in Source' as comment from source a
    where a.id not in (select b.id from target b)),
    cte2 as (
    select a1.id,'New in Target' as comment from target a1
    where a1.id not in (select b1.id from source b1)),
    cte3 as (select a2.id,'Mismatch in target' as comment from source a2
    inner join target b2 on a2.id=b2.id and a2.name!=b2.name)
    select * from cte1
    union
    select * from cte2
    union
    select * from cte3

    • @ankitbansal6
      @ankitbansal6  6 месяцев назад

      Good attempt. Can be simplified with just a full join . Checkout the video.

    • @ManpreetSingh-tv3rw
      @ManpreetSingh-tv3rw 6 месяцев назад

      @@ankitbansal6 sure, will have a look today.

  • @thedataguyfromB
    @thedataguyfromB 6 месяцев назад

    PySpark Version of this problem
    ruclips.net/video/JlPG-oIAK2E/видео.htmlsi=KrnNak5FgviIsRO1

  • @user-md1jk3qn3j
    @user-md1jk3qn3j 3 месяца назад

    with cte2 as(with cte1 as(select *,case when sid is null then 'New in taret' when tid is null then 'New in source' when tname!=sname and tname is not null and sname is not null then 'Mismatch' else 0 end as Comment from
    (select target.id as tid,target.name as tname,source.id as sid,source.name as sname from source left
    join target on source.id=target.id
    union
    select target.id as tid,target.name as tname,source.id as sid,source.name as sname from source right
    join target on source.id=target.id) a)
    select * from cte1 where Comment!='0')
    select tid,Comment from cte2 where tid is not null
    union all
    select sid,Comment from cte2 where tid is null ;