Skip to main content
Learning DUB

How to Use Inner Joins in SQL (Done)

The INNER JOIN is the most frequently used SQL join. Also referred to as an EQUIJOIN, the INNER JOIN returns rows that have matching values in both tables and discards unmatched rows from both tables. Inner join is the default if no join type is specified.

Keeping in mind that an inner join is used to find related data in two tables. On inspecting our AdventureWorks Sample Database, we are tasked to retrieve data about a product and its category from the SalesLT.Product table and SalesLT.ProductCategory table. We will be able to find the relevant product category record for a product based on its ProductCategoryID field; which is a foreign-key in the product table that matches a primary key in the product category table.

SELECT SalesLT.Product.Name AS ProductName, SalesLT.ProductCategory.Name AS Category
FROM SalesLT.Product
INNER JOIN SalesLT.ProductCategory
ON SalesLT.Product.ProductCategoryID = SalesLT.ProductCategory.ProductCategoryID;

This Transact-SQL query is an example of an inner join and as you can see from the result, we are able to return the ProductName from the products table and the corresponding Category from the product category table. Since INNER join is used, any products that do not have corresponding categories, and any categories that do not contain products are omitted from the results.

Keep in Mind:
By default, SQL Server performs an INNER JOIN if only the JOIN keyword is specified. In the above example, if you modify the query and remove the INNER keyword, as follows, the result will still be the same.

How to Use Inner Joins in SQL

SELECT SalesLT.Product.Name AS ProductName, SalesLT.ProductCategory.Name AS Category
FROM SalesLT.Product
JOIN SalesLT.ProductCategory
ON SalesLT.Product.ProductCategoryID = SalesLT.ProductCategory.ProductCategoryID;

This inner join is known as an equijoin - a join based on the testing of equality between two tables. It returns all the columns in both tables (Product and ProductCategory tables), and returns only the rows for which there is an equal value in the join column.

Assigning aliases to tables in the JOIN clause

Table aliases is used to simplify a query, especially when multiple joins must be used. Below are two examples of using aliases.

SELECT oh.OrderDate, oh.SalesOrderNumber, p.Name AS ProductName, od.OrderQty, od.UnitPrice, od.LineTotal
FROM SalesLT.SalesOrderHeader AS oh
JOIN SalesLT.SalesOrderDetail AS od
ON od.SalesOrderID = oh.SalesOrderID
JOIN SalesLT.Product AS p
ON od.ProductID = p.ProductID
ORDER BY oh.OrderDate, oh.SalesOrderID, od.SalesOrderDetailID;

The above query retrieves sales order data from the following 3 tables, SalesLT.SalesOrderHeader, SalesLT.SalesOrderDetail, and SalesLT.Product tables

Implicit VS Explicit JOINS

In SQL, there are ways to express joins: explicit join and implicit join.

Explicit JOINS

Explicit join uses the JOIN keyword to specify the table to join, and the ON keyword to specify the predicates for the join.

SELECT p.ProductID, m.Name AS Model, p.Name AS Product
FROM SalesLT.Product AS p
INNER JOIN SalesLT.ProductModel AS m
ON p.ProductModelID = m.ProductModelID
ORDER BY p.ProductID;

Implicit JOINS

Implicit join simply lists the tables for joining (in the FROM clause of the SELECT statement), using commas to separate them.

SELECT p.ProductID, m.Name AS Model, p.Name AS Product
FROM SalesLT.Product AS p
JOIN SalesLT.ProductModel AS m
ON p.ProductModelID = m.ProductModelID
ORDER BY p.ProductID;

Multiple joins

Multiple join is a query that contains the same or different join types, which are used more than once. Multiple joins gives you the ability to combine multiple tables.

SELECT od.SalesOrderID, m.Name AS Model, p.Name AS ProductName, od.OrderQty
FROM SalesLT.Product AS p
JOIN SalesLT.ProductModel AS m
ON p.ProductModelID = m.ProductModelID
JOIN SalesLT.SalesOrderDetail AS od
ON p.ProductID = od.ProductID
ORDER BY od.SalesOrderID;

////////////////////// F. Use the SQL-92 INNER JOIN syntax The following example returns all product names and sales order IDs.

.

-- By default, SQL Server performs an INNER JOIN if only the JOIN keyword is specified.

SELECT p.Name,
sod.SalesOrderID
FROM SalesLT.Product AS p
INNER JOIN SalesLT.SalesOrderDetail AS sod
ON p.ProductID = sod.ProductID
ORDER BY p.Name;
GO

///////////////////////Copy Code //////////////////
SQL
SQL
///////////////////////////// ///////////////////// PUB Database Examples //////////////////////////// /////// Joins Using Operators Other Than Equal You can also join values in two columns that are not equal. The same operators and predicates used for inner joins can be used for not equal joins. For more information about the available operators and predicates that can be used in joins, see Using Operators in Expressions and WHERE. This Transact-SQL example is of a greater-than (>) join which finds New Moon authors who live in states that come alphabetically after Massachusetts, where New Moon Books is located. USE pubs SELECT p.pub_name, p.state, a.au_lname, a.au_fname, a.state FROM publishers p INNER JOIN authors a ON a.state > p.state WHERE p.pub_name = 'New Moon Books' ORDER BY au_lname ASC, au_fname ASC
**
Here is the result set: pub_name state au_lname au_fname state ---------------- ------- -------------------- -------------------- ----- New Moon Books MA Blotchet-Halls Reginald OR New Moon Books MA del Castillo Innes MI New Moon Books MA Greene Morningstar TN New Moon Books MA Panteley Sylvia MD New Moon Books MA Ringer Albert UT New Moon Books MA Ringer Anne UT (6 row(s) affected) //// Joins Using the Not-equal Operator The not-equal join (< >) is rarely used. As a general rule, not-equal joins make sense only when used with a self-join. For example, this not-equal Transact-SQL join and self-join are used to find the categories with two or more inexpensive (less than $15) books of different prices: USE pubs SELECT DISTINCT t1.type, t1.price FROM titles t1 INNER JOIN titles t2 ON t1.type = t2.type AND t1.price <> t2.price WHERE t1.price < $15 AND t2.price < $15 -------------------------------------------------------------------------------- Note The expression NOT column_name = column_name is equivalent to column_name < > column_name. -------------------------------------------------------------------------------- This Transact-SQL example uses a not-equal join combined with a self-join to find all rows in the titleauthor table in which two or more rows have the same title_id but different au_id numbers (that is, books with more than one author): USE pubs SELECT DISTINCT t1.au_id, t1.title_id FROM titleauthor t1 INNER JOIN titleauthor t2 ON t1.title_id = t2.title_id WHERE t1.au_id <> t2.au_id ORDER BY t1.au_id
**
Here is the result set: au_id title_id ----------- -------- 213-46-8915 BU1032 267-41-2394 BU1111 267-41-2394 TC7777 409-56-7008 BU1032 427-17-2319 PC8888 472-27-2349 TC7777 672-71-3249 TC7777 722-51-5454 MC3021 724-80-9391 BU1111 724-80-9391 PS1372 756-30-7391 PS1372 846-92-7186 PC8888 899-46-2035 MC3021 899-46-2035 PS2091 998-72-3567 PS2091 (15 row(s) affected)

Related Content

Scroll to Top