Jay
Jay

Reputation: 747

SQL INNER JOIN exception

I'm new to SQL queries and I'm trying to join two tables

I need to get all data of the followers of userID = 2

here's the error i get : Syntax error: Encountered "INNER" at line 1, column 39.

and here's the SQL query I ran :

SELECT * FROM FOLLOWER 
WHERE userID = "2" 
INNER JOIN USERS ON FOLLOWER.Follower_userID = USERS.userID 
ORDER BY USERS.follower_count ASC

The tables in my DB are :

FOLLOWER


USERS


P.S I'm using Apache Derby.

Thank you so much guys.

Upvotes: 1

Views: 1699

Answers (6)

ASh
ASh

Reputation: 35681

position of where clause was incorrect

structure of SELECT query is

SELECT fields
FROM tables
WHERE conditions
ORDER BY fields

so you query should be

SELECT * 
FROM FOLLOWER INNER JOIN USERS ON FOLLOWER.Follower_userID = USERS.userID
WHERE userID="2" 
ORDER BY USERS.follower_count ASC

Upvotes: 2

Ajay2707
Ajay2707

Reputation: 5798

The rules of where with join is first made all join and then give where condition which filter more data..

so your where condition just put after inner join as above all suggested.

select * 
from yourtable
join yourothertable
where condition if you want

http://bytes.com/topic/sql-server/answers/850159-performance-conditions-where-clause-vs-conditions-inner-join

Upvotes: 0

Rajesh
Rajesh

Reputation: 1620

The Inner Join syntax

SELECT *
FROM Table1 AS T1
    INNER JOIN Table2 AS T2
    ON T1.Table1ColName = T2.Table2ColName
ORDER BY T1.Table1ColName

Refer this link for Inner Join

Change your query like

SELECT * FROM FOLLOWER
INNER JOIN USERS 
           ON FOLLOWER.Follower_userID = USERS.userID
WHERE userID="2"  
ORDER BY USERS.follower_count ASC

Upvotes: 0

Omar.Alani
Omar.Alani

Reputation: 4130

Try this SELECT * FROM FOLLOWER INNER JOIN USERS ON FOLLOWER.Follower_userID = USERS.userID WHERE FOLLOWER.userID="2" ORDER BY USERS.follower_count ASC

Hope this helps

Upvotes: 0

sdrzymala
sdrzymala

Reputation: 387

First use join and then where.

SELECT * FROM FOLLOWER INNER JOIN USERS ON FOLLOWER.Follower_userID = USERS.userID WHERE userID=2  ORDER BY USERS.follower_count ASC

Upvotes: 0

McElie
McElie

Reputation: 130

Try this statement:

SELECT * FROM FOLLOWER Fl
WHERE userID="2" 
INNER JOIN USERS Us ON Us.userID = Fl.Follower_userID
ORDER BY USERS.follower_count ASC

Let me know if it's works

Upvotes: 0

Related Questions