Menu

1 / 2

INNER JOIN in SQL

DEV Community: sql·Dennis Wachira·8 days ago
#pnaHGdFE
#devtosql#devto#dev#inner#join
Reading 0:00
15s threshold

Before normalization, all my data lived in a single table. Although it was easy to query, it contained a lot of repeated information. After splitting the data into multiple tables, I avoided duplication, but I also needed a way to reconnect the related data whenever I wanted to analyze it. INNER JOIN allows me to do exactly that. My first query combined four tables to display complete hospital visit information.

It brought together: Patient names, doctor names, department names, visit dates, diagnoses, treatment plans, bill amounts and payment status all in a single result set.

select v.visit_id, p.patient_name, d.doctor_name, dp.department_name, v.date_of_visit, v.diagnosis, v.treatment_plan, v.bill_amount, v.payment_status from visits v inner join patients p on v.patient_id = p.patient_id inner join doctors d on v.doctor_id = d.doctor_id inner join departments dp on v.department_id = dp.department_id order by v.visit_id;

Enter fullscreen mode

Exit fullscreen mode

Seeing information from multiple…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.