tags:

views:

365

answers:

8

I would like to know how this query would be written if no joins were used. I been trying to figure it out for cases where joins aren't viable or can't be used(aren't available).

SELECT
    *
FROM
(
    table1
INNER JOIN
    table2
ON
    table1.id = table2.id
)
INNER JOIN
    table3
ON
(
    table1.id2 = table3.id2
)
AND
(
    table1.id3 = table3.id3
)
WHERE
    table1.id = 1

The reason I can't use joins is because the application uses HQL as opposed to standard SQL and HQL makes joins incredibly difficult to do.

+8  A: 

It is impossible to extract data from two different tables without joining them in one way or another. You are using a JOIN statement BUT you can achieve the same thing by placing it in the where clause for example:

SELECT * FROM table1, table2 WHERE table1.id = table2.id AND ...
uriDium
it's not impossible, it'd just be a cartesian product i believe. cartesian something or other. ie select * from table1,table2
Darren Kopp
That's still a join. Just because it doesn't have the word "join" doesn't mean it isn't one.
Adam Robinson
@Darren, that is still considered a join (cross join)
Nathan Koop
Sometimes, you can combine data from two tables by a UNION. UNION is really not a join. However the class of situations where UNION is the right tool and the class of situations where JOIN is the right tool are just about disjoint.
Walter Mitty
+2  A: 

You can do the selects separated and do the joins within the application.

James Black
A: 

SQL Without Joins...

SELECT * FROM table1
SELECT * FROM table2
SELECT * FROM table3

That isn't going to help you though.

The question is, what is your desired output, and what reasons do you have for not wanting a join?

If you want data from multiple tables in a relational database then you will be joining the data. Maybe this is something you need to do in the application as James Black suggested.

If you could shed some more light on the situation though we may be able to help further.

Robin Day
A: 

often a subquery may be easier than a join if only selecting 1 value

SELECT e.Name, e.HireDate, (select SUM(CheckAmount) from EmployeeCheck where EmployeeID = e.ID) 
FROM Employee e
Darren Kopp
A: 
Stradas
A: 

You can do it with correlated subqueries if you want, but joins are better.

JP Alioto
A: 

If joins are off limits, then stay away from SQL databases.

Walter Mitty
A: 

Hibernate doesn't prevent you from doing joins if you have an association mapping in your hibernate map file. See, for example: http://www.jumpingbean.co.za/blogs/mark/hibernate_hql_inner_join_on_clause

JasonTrue