SQL INNER JOIN

SQL INNER JOIN clause is used to select data from 2 (or more) tables, at least one match in both tables.

SQL INNER JOIN Clause Syntax

SELECT Table1.Column1, Table1.Column2, ... , Table2.Column1, Table2.Column2, ...
FROM Table1
INNER JOIN Table2
ON Table1.ColumnName = Table2.ColumnName

Note: SQL INNER JOIN is the default type of SQL JOIN. “JOIN” = “INNER JOIN”

SQL INNER JOIN Clause Example

Table: Employees

EmployeeId FirstName LastName Department Salary
203 Mazojys Fxoj Finance 78000
204 Jozzh Lnanyo Finance 45800
205 Syllauu Dfaafk Finance 57000
206 Gecrrcc Srlkrt Finance 62000

Table: Tickets

TicketID TicketNo EmployeeId
1 2349 203
2 2400 204
3 2438 205

Select all the employees with any tickets:

SELECT Employees.EmployeeId
           , Employees.FirstName
           , Employees.LastName
           , Tickets.TicketNo
FROM Employees
INNER JOIN Tickets
ON Employees.EmployeeId = Tickets.EmployeeId

The result will look like:

EmployeeId FirstName LastName TicketNo
203 Mazojys Fxoj 2349
204 Jozzh Lnanyo 2400
205 Syllauu Dfaafk 2438

Note: The employee whose EmployeeId is 206 doesn’t have any records matched in the table Tickets, so this employee will not in the result.

As mentioned above, “JOIN” = “INNER JOIN”, so, the following statement has the same result:

SELECT Employees.EmployeeId
           , Employees.FirstName
           , Employees.LastName
           , Tickets.TicketNo
FROM Employees
JOIN Tickets
ON Employees.EmployeeId = Tickets.EmployeeId

In most database systems, we can even write it shorter:

SELECT Employees.EmployeeId
           , Employees.FirstName
           , Employees.LastName
           , Tickets.TicketNo
FROM Employees, Tickets
WHERE Employees.EmployeeId = Tickets.EmployeeId

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>