0% found this document useful (0 votes)
10 views

sql join - advanced sql _ bipp analytics

This document provides an overview of SQL JOIN operations, specifically focusing on INNER JOIN and LEFT OUTER JOIN. It explains how to combine data from multiple tables using a shared column, with examples using sample tables for tours and cities. The document also illustrates how to query data based on specific conditions and highlights the differences between the two JOIN types.

Uploaded by

Yanet Cesaire
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

sql join - advanced sql _ bipp analytics

This document provides an overview of SQL JOIN operations, specifically focusing on INNER JOIN and LEFT OUTER JOIN. It explains how to combine data from multiple tables using a shared column, with examples using sample tables for tours and cities. The document also illustrates how to query data based on specific conditions and highlights the differences between the two JOIN types.

Uploaded by

Yanet Cesaire
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Platform Features Pricing Resources Learn Company Connect Sign in Try for free

Search SQL Tutorial

SQL Tutorial > Advanced SQL > SQL Joins

Selecting Data From Multiple Tables: SQL JOINS


This lesson covers perhaps the most important element of the SQL language: the SQL IN THIS PAGE
JOIN operation. As you know, a database stores data in several tables. When you
need to combine data from multiple tables you do a JOIN between the tables where Sample Tables
the data is stored. SQL INNER JOIN
Operator
This article presents examples using the SQL JOIN clause. SQL OUTER JOIN
Operator
Closing Words
Sample Tables
There are two sample tables: tours and cities.

Table tours

Tour_Name Best_Season Price Duration (days)

United States and Canada March to Sep 3600 8

United States Big Cities All year 4300 12

Africa Tour All year 2100 7

Brazil Beaches Dec to March 1800 8

Table cities

City_Name Type_of_City Tour_Name Days Staging

Washington culture United States and Canada 5

Washington culture United States Big Cities 5

Cairo culture Africa Tour 4

Rio de Janeiro beach Brazil Beaches 4


Johannesburg safari Africa Tour 3

Florianópolis beach Brazil Beaches 4

Quebec culture United States and Canada 3

New York culture United States Big Cities 7

To use the JOIN clause to combine the two tables, there must be a shared column
appearing in both tables. In this database, the column is tour_name. It is easy to
identify the shared column because it has the same name on both tables. In other
databases, you have to look at the values as the shared column can have different
names. The important thing is the values of the columns, as the JOIN operator creates
pairs of records for those records having the same value on the shared column.

Two types of JOIN operators are presented: INNER JOIN and LEFT OUTER JOIN.

SQL INNER JOIN Operator


Here is a simple SQL JOIN query to obtain the pairs:

Copy

SELECT
tour_name,
best_season,
price,
city_name,
type_of_city,
days_staging
FROM tours INNER JOIN cities ON tours.tour_name = cities.to
ur_name

How is a pair of records created by the SQL INNER JOIN operator?. For each record
on the first table (tours), every record on the second table (cities) having the same
value in the columns specified in the ON clause (tour_name) is paired with the record
from the first table.

Here is the result:

tour_name best_season price city_name type_of city days_staging


United States Canada March to Sep 3600 Quebec culture

United States Canada March to Sep 3600 Washington culture

United States Big Cities All year 4300 New York culture

United States Big Cities All year 4300 Washington culture


Africa Tour All year 2100 Johannesburg safari

Africa Tour All year 2100 Cairo safari

Brazil Beaches Dec to March 1800 Florianópolis beach


Brazil Beaches Dec to March 1800 Rio de Janeiro beach

Suppose you want to know what tours are available for a culture city for less than $
3000. Here is the query:

Copy

SELECT
tour_name,
price
FROM tours INNER JOIN cities ON tours.tour_name = cities.to
ur_name
WHERE price < 3000
AND type_of_city = ‘culture’

The result is:

tour_name price
Africa Tour 2100.00

SQL OUTER JOIN Operator


In some cases a record in the first table does not have a counterpart record in the
second table in order to form a pair. For example, there could be a tour_name not
related with any city, perhaps because it is a tour to the jungle or a one week sailing
trip. Here is a new version of the table tours:

Tour_Name Best_Season Price Duration (days)

United States and Canada March to Sep 3600 8

United States Big Cities All year 4300 12


Africa Tour All year 2100 7

Brazil Beaches Dec to March 1800 8

Caribbean: a sailing week May to October 5300 7

5 days in the Amazonas Jungle All year 900 5

If you want to obtain a list of the available tours, you can execute the first INNER
JOIN again:

Copy

SELECT
tour_name,
best_season,
price,
city_name,
type_of_city,
days_staging
FROM tours INNER JOIN cities ON tours.tour_name = cities.to
ur_name

The tours Caribbean: a sailing week and 5 days in the Amazonas Jungle are not
returned in the results as there is no associated city.

tour_name best_season price city_name type_of_city days_staging

United States Canada March to Sep 3600 Quebec culture

United States Canada March to Sep 3600 Washington culture


United States Big Cities All year 4300 New York culture

United States Big Cities All year 4300 Washington culture

Africa Tour All year 2100 Johannesburg safari


Africa Tour All year 2100 Cairo safari
Brazil Beaches Dec to March 1800 Florianópolis beach
tour_name best_season price city_name type_of_city days_staging
Brazil Beaches Dec to March 1800 Rio de Janeiro beach

To include the other tours, you use the SQL LEFT OUTER JOIN. The SQL LEFT
OUTER JOIN is does not discard a row with no counterpart on the second table.

Here is the query:

Copy

SELECT
tour_name,
best_season,
price,
city_name,
type_of_city,
days_staging
FROM tours LEFT OUTER JOIN cities ON tours.tour_name = citi
es.tour_name

In the results, you can see the tours without cites are included. The values of the
columns from the second table are NULL, as this is the normal behavior of the SQL
LEFT OUTER JOIN operator.

tour_name best_season price city_name type_of city

United States Canada March to Sep 3600 Quebec culture


United States Canada March to Sep 3600 Washington culture
United States Big Cities All year 4300 New York culture

United States Big Cities All year 4300 Washington culture


Africa Tour All year 2100 Johannesburg safari

Africa Tour All year 2100 Cairo safari


Brazil Beaches Dec to March 1800 Florianópolis beach

Brazil Beaches Dec to March 1800 Rio de Janeiro beach


Caribbean: a sailing week May to Oct 5300

5 days in the Amazonas Jungle All year 900

Closing Words
In this lesson you learned two of the SQL JOIN operators INNER JOIN and LEFT
OUTER JOIN. There are other less frequently used JOIN operators you can explore
now that you understand the basics. Keep going, learn SQL and increase your skills!

SQL Window Function Examples Visual Representation of SQL JOINS

Sign up for a Connect to leading SQL


databases
Build visual data models
without learning a new
Create two dashboards
and ten reports for free
forever free language

account!
Sign up for free

Platform Features Resources Learn Company Connect


Business Intelligence bipp Data Modeling Layer Blog Documentation About Request Demo

Embedded Analytics Visual SQL Data Explorer Reports bipp Tutorial Meet The Team Support

Professional Services Data Visualization Release Notes Why bipp? Careers Contact Us

Security Git-based Version Control Community SQL Tutorial News

Pricing In-Database Migrate from Chartio Definitions Case studies

SQL Editor Compare Disclosure

Scheduled Delivery and Privacy


Alerts
Terms
Real-Time BI

© 2022 bipp Inc.

You might also like