Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How should i edit WHERE clause in SQL query to get desired result?

Let’s say i have two tables called "Videos" and "Reviews"

Videos:
int: video_id (PK)
String: video_name
.
.

Reviews:
int: review_id (PK)
int: video_id (FK)
String: review
.
.

In a scenario where i want to get all videos and their related reviews, i use the query:

"SELECT * FROM Videos v, Reviews r WHERE v.video_id = r.review_id"

But this query only returns the videos with a review, the videos without a review is not returned, but i also need them.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How should i edit the query to see videos without a review too?

Example result:

(video_id), (video_name), (review_id), (review)
1, "videoName", 1, "review1"
2, "videoName2", NULL, NULL

Thank you for your answers and comments. (sorry for the typo)

>Solution :

Seems you have some typos there – Travels or Videos
Nevertheless, here is the query based on your posted query:

SELECT * FROM Travels t left join Reviews r on t.travel_id = r.review_id

Just a left join will do the trick.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading