The Sql Join Clause
- Sql Join Clause is used to combine two or more tables.
- To Combine tables we use common columns of tables.
- The Join Clause makes a artificial relation on tables(not realy, effective only at the query)
- We can use where clause instead of join (not for left join, right join,or full join)
- We can use Inner Join instead of join. They are same.
The Join has different types: Left Join, Right Join, Full Join
Syntax for Join Clause
Select columnNames from Table1 join Table2 on Table1.relationColumn= Table2.relationColumnor with where clause
TO DOWNLOAD THE SAMPLE LİBRARY DATABASE CLICK
The Sql Join Clause Example on Library Database
Sample 1: List all student’s name,surname and the borrow’s taken date.(two tables)
1 2 |
Select name,surname,takenDate from students join borrows on students.studentId = borrows.studentId |
or
1 2 |
Select name,surname,takenDate from students,borrows where students.studentId = borrows.studentId |
Sample 2: List all student’s name,surname,book’s name and the borrow’s taken date.(three table)
1 2 3 |
Select students.name,students.surname,books.name,takenDate from students join borrows on students.studentId = borrows.studentId join books on books.bookId = borrows.bookId |
or
1 2 3 |
Select students.name,students.surname,books.name,takenDate from students,borrows,books where students.studentId = borrows.studentId and books.bookId = borrows.bookId |
Note: If we have same column names at different tables, then we must write the column’s names with their table’s name.
Sample 3: List all student’s name,surname,book’s name,autor name and the borrow’s taken date.
Select students.name as studentName,students.surname,books.name as BookName,takenDate, authors.name as AuthorName,authors.surname as AuthorSurname from students join borrows on students.studentId = borrows.studentId join books on books.bookId = borrows.bookId join authors on authors.authorId = books.authorIdSample 4: List all student’s name,surname and the borrow’s taken date, where student name Jefferson.
1 2 3 |
Select name,surname,takenDate from students join borrows on students.studentId = borrows.studentId where name = 'Jefferson' |