SIMPLE JOINS

To demonstrate the Joins between two tables in SQL, we are going to take the following two tables called STATE and CITY. In STATE table STATE_CD is the primary key and in CITY table STATE_CD and CITY_NAME makes the primary key.

STATE table

STATE_CD STATE_DESC
AK ARKANSAS
TX TEXAS
NY NEW YORK
NJ NEW JERSY

CITY table

STATE_CD CITY_NAME POPULATION
AK Little Rock 250000
AK Hot Springs 8000
TX Dallas 1000000
TX Irving 100000
TX Austin 1500000
NY New York 30000000
NJ New Jersy 1500000
NJ Paris 10000

We can join two tables if there is a common column exists in both. In the above example we have a common column called STATE_CD, so we can percorm join on these tables.

Say you want to display the CITY_NM, POPULATION, STATE_DESC from the above two tables.

Here we go, CITY_NM and POPULATION columns exists in CITY table, STATE_DESC column is in STATE table. We are going to show information from two different tables, so in the FROM clause of SELECT statement we should include two tables.

SELECT CITY_NM, POPULATION, STATE_DESC

FROM CITY, STATE

WHERE CITY.STATE_CD = STATE.STATE_CD;

The result for the above query

CITY_NM POPULATION STATE_DESC
Little Rock 250000 ARKANSAS
Hot Springs 8000 ARKANSAS
Dallas 1000000 TEXAS
Irving 100000 TEXAS
Austin 1500000 TEXAS
New York 30000000 NEW YORK
New Jersy 1500000 NEW JERSY
Paris 10000 NEW JERSY

How this JOIN works. Each database engine chooses the different ways to process the result set. To understand the JOIN concept better we will take the above example. In the FROM clause we have CITY, STATE tables, so it merges the rows from two tables and makes it as one temporary table in the server memory area, so it will be like

CITY.STATE_CD CITY.CITY_NAME CITY.POPULATION STATE.STATE_CD STATE.STATE_DESC
AK Little Rock 250000 AK ARKANSAS
AK Little Rock 250000 TX TEXAS
AK Little Rock 250000 NY NEW YORK
AK Little Rock 250000 NJ NEW JERSY
AK Hot Springs 8000 AK ARKANSAS
AK Hot Springs 8000 TX TEXAS
AK Hot Springs 8000 NY NEY YORK
AK Hot Springs 8000 NJ NEW JERSY
TX Dallas 1000000 AK ARKANSAS
TX Dallas 1000000 TX TEXAS
TX Dallas 1000000 NY NEW YORK
TX Dallas 1000000 NJ NEW JERSY
TX Irving 100000 AK ARKANSAS
TX Irving 100000 TX TEXAS
TX Irving 100000 NY NEW YOURK
TX Irving 100000 NJ NEW JERSY
TX Austin 1500000 AK ARKANSAS
TX Austin 1500000 TX TEXAS
TX Austin 1500000 TY NEW YORK
TX Austin 1500000 NJ NEW JERSY
NY New York 30000000 AK ARKANSAS
NY New York 30000000 TX TEXAS
NY New York 30000000 NY NEW YORK
NY New York 30000000 NJ NEW JERSY
NJ New Jersy 1500000 AK ARKANSAS
NJ New Jersy 1500000 TX TEXAS
NJ New Jersy 1500000 NY NEW YORK
NJ New Jersy 1500000 NJ NEW JERSY
NJ Paris 10000 AK ARKANSAS
NJ Paris 10000 TX TEXAS
NJ Paris 10000 NY NEW YORK
NJ Paris 10000 NJ NEW JERSY

 

Now the WHERE clause applies on the above shown temporary table. According to the WHERE clause in our select statement state_cd in city table should be equal to state_cd in state table ie (CITY.STATE_CD = STATE.STATE_CD). To show the result set we have to select those records in which state_cd is equal. So in the following table all the records we marked in black is selected and those one marked in reds are discarded as the state_cd is different. This is the way it will filter the un wanted records when you do a join.

CITY.STATE_CD CITY.CITY_NAME CITY.POPULATION STATE.STATE_CD STATE.STATE_DESC
AK Little Rock 250000 AK ARKANSAS
AK Little Rock 250000 TX TEXAS
AK Little Rock 250000 NY NEW YORK
AK Little Rock 250000 NJ NEW JERSY
AK Hot Springs 8000 AK ARKANSAS
AK Hot Springs 8000 TX TEXAS
AK Hot Springs 8000 NY NEY YORK
AK Hot Springs 8000 NJ NEW JERSY
TX Dallas 1000000 AK ARKANSAS
TX Dallas 1000000 TX TEXAS
TX Dallas 1000000 NY NEW YORK
TX Dallas 1000000 NJ NEW JERSY
TX Irving 100000 AK ARKANSAS
TX Irving 100000 TX TEXAS
TX Irving 100000 NY NEW YOURK
TX Irving 100000 NJ NEW JERSY
TX Austin 1500000 AK ARKANSAS
TX Austin 1500000 TX TEXAS
TX Austin 1500000 NY NEW YORK
TX Austin 1500000 NJ NEW JERSY
NY New York 30000000 AK ARKANSAS
NY New York 30000000 TX TEXAS
NY New York 30000000 NY NEW YORK
NY New York 30000000 NJ NEW JERSY
NJ New Jersy 1500000 AK ARKANSAS
NJ New Jersy 1500000 TX TEXAS
NJ New Jersy 1500000 NY NEW YORK
NJ New Jersy 1500000 NJ NEW JERSY
NJ Paris 10000 AK ARKANSAS
NJ Paris 10000 TX TEXAS
NJ Paris 10000 NY NEW YORK
NJ Paris 10000 NJ NEW JERSY

 

So after filtered the un wanted records the work table looks like

CITY.STATE_CD CITY.CITY_NAME CITY.POPULATION STATE.STATE_CD STATE.STATE_DESC
AK Little Rock 250000 AK ARKANSAS
AK Hot Springs 8000 AK ARKANSAS
TX Dallas 1000000 TX TEXAS
TX Irving 100000 TX TEXAS
TX Austin 1500000 TX TEXAS
NY New York 30000000 NY NEW YORK
NJ New Jersy 1500000 NJ NEW JERSY
NJ Paris 10000 NJ NEW JERSY

What are the columns we have in our SELECT statement those are the only columns that will be in our result set, all other columns will get discarded at this final time.

CITY_NM POPULATION STATE_DESC
Little Rock 250000 ARKANSAS
Hot Springs 8000 ARKANSAS
Dallas 1000000 TEXAS
Irving 100000 TEXAS
Austin 1500000 TEXAS
New York 30000000 NEW YORK
New Jersy 1500000 NEW JERSY
Paris 10000 NEW JERSY

That is how the result set will get processed inside the database engine.