Tuesday, June 29, 2010

Inner joins and its usage with a Sample Query

When I was writing the query for my application, I used the following Formatted Query
SELECT JS.JobseekerID
, JS.FirstName
, JS.LastName
, JS.Currency
, JS.AccountRegDate
, JS.LastUpdated
, JS.NoticePeriod
, JS.Availability
, C.CountryName
, S.SalaryAmount
, DD.DisciplineName
, DT.DegreeLevel
FROM Jobseekers JS, Countries C, SalaryBracket S, DegreeDisciplines DD
, DegreeType DT
WHERE
JS.CountryID = C.CountryID
AND JS.MinSalaryID = S.SalaryID
AND JS.DegreeDisciplineID = DD.DisciplineID
AND JS.DegreeTypeID = DT.DegreeTypeID
AND JS.ShowCV = 'Yes'

In corner of my mind I was not satisfied with the above and was trying to make it better with the other ways of writing query, when I met our Database tester, She gave me the following idea to write, It was very effective way of writing that Query
SELECT JS.JobseekerID
, JS.FirstName
, JS.LastName
, JS.Currency
, JS.AccountRegDate
, JS.LastUpdated
, JS.NoticePeriod
, JS.Availability
, C.CountryName
, S.SalaryAmount
, DD.DisciplineName
, DT.DegreeLevel
FROM Jobseekers JS
INNER
JOIN Countries C
ON JS.CountryID = C.CountryID
INNER
JOIN SalaryBracket S
ON JS.MinSalaryID = S.SalaryID
INNER
JOIN DegreeDisciplines DD
ON JS.DegreeDisciplineID = DD.DisciplineID
INNER
JOIN DegreeType DT
ON JS.DegreeTypeID = DT.DegreeTypeID
WHERE
JS.ShowCV = 'Yes'

I hope this will help to understand How you can use the inner joins in the queries

1 comment:

  1. Hello.

    I have a problem with this Inner Join Query:

    SELECT logins.log_log, logins.log_pas, logins.log_cli_id, client_cli_nom, vehicle.veh_pla, vehicle.veh_cli_id, vehicle.veh_sc_ser, sc.sc_ser

    FROM logins INNER JOIN (client INNER JOIN (vehicle INNER JOIN sc ON vehicle.veh_sc_ser = sc.sc_ser) ON client.cli_id = vehicle.veh_cli_id) ON logins.log_cli_id = client.cli_id

    WHERE ((logins.log_log)=' User1 ') AND ((logins_cliente.log_pas)=' Password 1'));


    It returns an an error where the first INNER JOIN starts.

    This is how I know how to make those BIG INNER JOINS Queries. I'm new with Mysql so I suppose this type of database works differently with the INNER JOINS.

    What I'm trying to do is find the client, vehicle and sc from the logins table, which is the table that relates to the client table, which relates to the vehicle table and so forth.

    Any Ideas?

    ReplyDelete