sql join multiple tables with conditions

Today, We want to share with you sql join 3 tables.In this post we will show you sql join 3 tables with where clause, hear for sql join 3 tables on same key we will give you demo and example for implement.In this post, we will learn about sql join 3 tables many-to-many with an example.

how to join three tables in sql using joins?

In this tutorial we learn to all about having Examples like as a Three table JOIN, Join 3 Tables (or More), Using joins in sql to join the table, Using parent-child relationship, INNER JOIN, LEFT JOIN or many more.

Also Read: SQL multiple joins for beginners with examples

Three table JOIN syntax in SQL

SELECT table1.col, table3.col FROM table1 
join table2 ON table1.primarykey = table2.foreignkey
join table3 ON table2.primarykey = table3.foreignkey

How to Join 3 Tables (or More) in SQL?

SELECT
  pupil.first_name,
  pupil.last_name,
  subject.name
FROM pupil
JOIN pupil_subject
  ON pupil.id = pupil_subject.pupil_id
JOIN subject
  ON subject.id = pupil_subject.subject_id;

Using joins in sql to join the table

select s_name, score, status, address_city, email_id,
accomplishments 
from pupil s 
inner join marks m on s.s_id = m.s_id 
inner join details d on d.collage_id = m.collage_id;

Using parent-child relationship

select s_name, score, status, address_city, 
email_id, accomplishments 
from pupil s, marks m, details d 
where s.s_id = m.s_id and 
m.collage_id = d.collage_id;

SQL JOIN Examples

SELECT O.RequestNumber, CONVERT(date,O.RequestDate) AS Date, 
       P.MobileName, I.Quantity, I.UnitPrice 
  FROM [Request] O 
  JOIN RequestItem I ON O.Id = I.RequestId 
  JOIN Mobile P ON P.Id = I.MobileId
ORDER BY O.RequestNumber

Join multiple tables using INNER JOIN

SELECT member.first_name, member.last_name, call.start_time, call.end_time, call_outcome.outcome_text
FROM member
INNER JOIN call ON call.member_id = member.id
INNER JOIN call_outcome ON call.call_outcome_id = call_outcome.id
ORDER BY call.start_time ASC;

Join multiple tables using LEFT JOIN

SELECT address.address_name_eng, city.city_name, shopper.shopper_name
FROM address
INNER JOIN city ON city.address_id = address.id
INNER JOIN shopper ON shopper.city_id = city.id;

I will use LEFT JOIN. I will simply replace all “INNER” with “LEFT”

SELECT address.address_name_eng, city.city_name, shopper.shopper_name
FROM address
LEFT JOIN city ON city.address_id = address.id
LEFT JOIN shopper ON shopper.city_id = city.id;

LEFT JOIN – Tables order matters

SELECT address.address_name_eng, city.city_name, shopper.shopper_name
FROM shopper
LEFT JOIN city ON shopper.city_id = city.id
LEFT JOIN address ON city.address_id = address.id;

Join multiple tables using both – INNER JOIN & LEFT JOIN

SELECT address.address_name_eng, city.city_name, shopper.shopper_name
FROM address
INNER JOIN city ON city.address_id = address.id
LEFT JOIN shopper ON shopper.city_id = city.id;

SQL Inner-join with 3 tables

SELECT s.pupilname
    , s.pupilid
    , s.pupildesc
    , h.hallname
FROM pupils s
INNER JOIN hallprefs hp
    on s.pupilid = hp.pupilid
INNER JOIN halls h
    on hp.hallid = h.hallid

I hope you get an idea about sql join 3 tables.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Read Also:  PHP Image Compression before Uploading

About Pakainfo

I am Jaydeep Gondaliya, a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Pakainfo →

Leave a Reply

Your email address will not be published. Required fields are marked *