Infogoal Logo
GOAL DIRECTED LEARNING
Master SQL

SQL TUTORIAL HOME

SQL OVERVIEW
SQL SYNTAX
SQL BOOKREVIEWS

SQL BASICS

SQL SELECT
SQL WHERE
SQL INSERT
SQL UPDATE
SQL DELETE

SQL ADMINISTRATION

SQL CREATE DATABASE
SQL DROP DATABASE
SQL CREATE TABLE
SQL ALTER TABLE
SQL DROP TABLE
SQL CREATE INDEX
SQL DROP INDEX
SQL ADD FOREIGN KEY
SQL DROP FOREIGN KEY
SQL CREATE VIEW
SQL DROP VIEW

SQL ADVANCED

SQL CONCAT
SQL SUBSTRING
SQL TRIM
SQL AND & OR
SQL IN
SQL BETWEEN
SQL LIKE
SQL DISTINCT
SQL GROUP BY
SQL AGGREGATE
SQL HAVING
SQL ORDER BY
SQL JOIN
SQL OUTER JOIN

SQL JOIN

Previous | Next

What is SQL JOIN?

The SQL JOIN is a clause that enables a SELECT statement to access more than one table. The JOIN clause controls how tables are linked. It is a qualifier of the SQL FROM clause.

The standard JOIN clause (also known as the INNER JOIN clause) differs from the OUTER JOIN in that rows are returned only when there are matches for the JOIN critieria on the second table.

Why Use SQL JOIN?

Use the SQL JOIN whenever multiple tables must be accessed through a SQL SELECT statement and no results should be returned if there is not a match between the JOINed tables.

How To Use SQL JOIN

SQL JOIN is used as follows. The ON clause describes the conditions of the JOIN.

Important! A "cartesian product" can result if there is no relating the tables for the join. A row would be included for each combination between the two tables so if one table has 1,000 rows and the second table has 2,000 rows then 2,000,000 rows would be returned.

Important! If there are no matches on the JOIN criteria then no rows will be returned. This is known an "INNER JOIN". Use the "OUTER JOIN" in cases where rows should be returned when one side of the join is missing.

SQL JOIN Syntax

SELECT <column_name1>, <column_name2> <aggregate_function>
FROM <table_name>
JOIN <table_name> ON <join_conditions>

SQL JOIN Example

The following example JOINs the region and branch tables on the region_nbr column.

Here are the contents of the tables:

Table: REGION
region_nbrregion_name
100East Region
200Central Region
300Virtual Region
400West Region

Table: BRANCH
branch_nbrbranch_nameregion_nbremployee_count
108New York10010
110Boston1006
212Chicago2005
404San Diego4006
415San Jose4003

This SQL Statement with JOIN is executed:

SELECT region.region_nbr, region.region_name, branch.branch_nbr, branch.branch_name
FROM dbo.region
JOIN dbo.branch
ON branch.region_nbr = region.region_nbr
ORDER BY region.region_nbr

Here is the result. Note that the "Virtual Region" is included in the results even though it has no rows in the branch table. This is the difference between the INNER JOIN and OUTER JOIN.

region_nbr region_namebranch_nbrbranch_name
100East Region 108New York
100East Region 110Boston
200Central Region212Chicago
400West Region 404San Diego
400West Region 415San Jose

Previous | Next


Advertisements

Advertisements:


Infogoal.com is organized to help you gain mastery.
Examples may be simplified to facilitate learning.
Content is reviewed for errors but is not warranted to be 100% correct.
In order to use this site, you must read and agree to the terms of use, privacy policy and cookie policy.
Copyright 2006-2020 by Infogoal, LLC. All Rights Reserved.

Infogoal Logo