SQL Joins, or joining of database tables is one the most difficult for noobs to understand, but this is one of the more powerful queries out there. Yeah, its nice and comfy when you have just one table and everything is right there. But there will be times, many actually, when your databases will hold various tables that you have to bring together in order to retrieve query results.
Let’s take an example of a two table database where one table is for colleges and the other is for states.
SELECT DISTINCT
c.id,c.name
FROM colleges as c
INNER JOIN state AS s
ON s.college_id = c.id
WHERE id = ” id ” ;
Okay, obviously I need to explain a few things first. For Example, what is c.name ? Up until now we had been calling columns by their name. But what happens when you get to Joins is that sometimes the table and column name start getting long and before you know it the query starts reading longer Tolstoy’s war and peace.
So, taking it peace by peace:
SELECT DISTINCT (this helps to reduce any duplicates that could be in your DB)
c.id,c.name
FROM colleges as c (here we’re choosing the column’s id and name from the colleges table and giving them the alias of c (for colleges)
INNER JOIN (this is what is going to join the two tables)
state AS s (assigning the table to s)
ON (here’s the comparison, or with)
s.college_id = c.id (this says where the table holding ‘state’ and college_id should equal the table ‘college’ id
WHERE id=’id’ (this is the final refinement that gets all matches of id of this state has id of these colleges)

