Week 5
Joining
Objective: of this lab Manual is to get familiar with join in databases.
Background: The SQL Joins clause is used to combine records from two or more tables in a database. A JOIN is a means for combining fields from two tables by using values common to each.
Pre-lab:   previous labs.
Assessment tools:
The assessment is according to the student participation in the lab if the student solves the exercise, he will get the participation mark for this lab.


Joining
You can use the Student_Quiz_db  for these queries
Inner Join
SELECT column_names FROM table1
INNER JOIN table2 ON table1.column_name = table2.column_name;

Fetch all data of student_t and courses_t by using inner joining.

SELECT students.cms_id,students.name,students.email,students.mob_number,courses.name FROM students
INNER JOIN courses ON students.cms_id = courses.cms_id;                              

Fetch all data of teacher_t and courses_t by using inner joining.

select teachers.t_cms_id,teachers.name,teachers.email,teachers.mob_number,courses.name FROM teachers INNER JOIN courses ON teachers.t_cms_id=courses.t_cms_id

Fetch all data of student_t , courses_t and student_t at a single time by using inner joinng .
select teachers.t_cms_id,teachers.name,teachers.email,teachers.mob_number,courses.name,students.name,students.email,students.mob_number FROM ((courses INNER JOIN teachers ON teachers.t_cms_id=courses.t_cms_id)INNER JOIN students ON students.cms_id=courses.cms_id)

Fetch all data of student_t and courses_t by using self joining .
SELECT teachers.t_cms_id,teachers.name,teachers.email, teachers.mob_number,courses.name FROM teachers,courses WHERE teachers.t_cms_id=courses.t_cms_id


Different Types of SQL Joins:

  • (INNER) JOIN: Returns records that have matching values in both tables
  • LEFT (OUTER) JOIN: Returns all records from the left table, and the matched records from the right table
  • RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched records from the left table
  • FULL (OUTER) JOIN: Returns all records when there is a match in either left or right table
SQL LEFT JOIN
w3school
SQL INNER JOIN
w3school
SQL FULL OUTER JOIN
w3school
SQL RIGHT JOIN
w3school
Special Thanks to w3school





5 Comments

Post a Comment

Previous Post Next Post