Page With No Likes — Facebook Asked SQL Question

  • Post last modified:September 10, 2022
  • Reading time:2 mins read
Photo by Caspar Camille Rubin on Unsplash

Introduction

  • In this article we will solve Page With No Likes SQL question which is asked by Facebook as per DataLemur website.

If you don’t know DatLemur then please do visit , it’s one of the best website to practice SQL questions and improve your SQL skills.

Question

  • We have been given two input tables , pages and page_likes.
  • Our goals is to query all the pages that don’t have any likes.
  • As we can see , page_id 20701 doesn’t have any likes hence it returned as output.

Solution

  • We have unique pages table with us along with likes table, if we do left join on pages table with likes table than all the page will have user_id , basically user who liked the page, except where no user liked the pages.
  • In below example we can see page_id 32728 and 20701 doesn’t have any user id that means no user liked this page.
select * from pages p left join page_likes pl on p.page_id = pl.page_id
  • once we have this joined information, all we have to do is find out cases where user_id is null. finally can order it by user_id , since it was asked in question
select p.page_id from pages p 
left join page_likes pl on p.page_id = pl.page_id
where pl.user_id is null order by p.page_id

Submission

  • Our solution is accepted by the platform.

Conclusion

  • In this we can learn about practical usage of left join. Left join basically keep all the records in left table , but right table will be filled with null if no joined key is found.

Leave a Reply