This question already has answers here:
INNER JOIN ON vs WHERE clause
(12 answers)
Closed 7 years ago.
I have two queries:
$query = mysql_query("SELECT ord.orderID, customers.CustomerName,
FROM ord, customers
WHERE customers.CustomerSalary=ord.orderID");
and
$query = mysql_query("SELECT ord.orderID, customers.CustomerName,
FROM ord
INNER JOIN customers
ON customers.CustomerSalary=ord.orderID");
This queries return the same result. What is different between them.
$query = mysql_query("SELECT ord.orderID, customers.CustomerName,
FROM ord
INNER JOIN customers
ON customers.CustomerSalary=ord.orderID");
This query is optimised than the other one as Inner join get applied first and then where clause will be applied.
Other benifit I see that is you can change Inner Outer join.
Clean code.
Both are same. The first query is basically inner join on the back-end but the way you have written second query, you can use left join, right join etc with that query.
Both will perform Cartesian Product on the back-end and filter out the results based on the check.
You will need to use the second query format in order to use left join, right join etc.
Another approach commonly used is known as nested queries. They are pretty faster than joins since there is no Cartesian product on the back-end.
Related
I just recently learned about SQL INNER JOIN and I thought of applying it on a project so basically I have three tables
payers
discounts
items
Now I was just wondering if I can return the results from both of the three tables at once using an INNER JOIN with both of the 3 tables or is it only possible with 2 tables?
If it is possible to use an INNER JOIN with more than 2 tables then kindly please guide me on how to do it and if not then tell me how to do it in any other ways possible.
Now this is the query that I currently have which doesn't work as expected:
SELECT *
FROM payers
INNER JOIN discounts AND items
ON payers.id = discounts.id AND ON payers.id = items.id;
You want two joins. The syntax is:
SELECT *
FROM payers p
INNER JOIN discounts d ON d.id = p.id
INNER JOIN items i ON i.id = p.id
Side notes:
you did not show your actual schema, so this uses the join conditions described in your attempt; you might need to review that
table aliases make the query shorter to write and easier to read
SELECT * is generally not good practice; instead, I would recommend enumerating the columns you want in the SELECT clause, and properly aliasing conflicting columns names, if any (here, all three tables have a column called id, which would cause ambiguity in the resultset)
This question already has answers here:
Difference between JOIN and INNER JOIN
(6 answers)
SQL JOIN and different types of JOINs
(6 answers)
Closed 4 years ago.
SELECT ID, Name, Marks, Grade
FROM Students AS s
JOIN Grades AS g ON s.Marks BETWEEN g.Min_Mark AND g.Max_Mark;
I browsed numbers of resources about left join, right join, inner join and full outer join, but I have no idea what is this "join" means. Is this some shortcut? if not, what is this?
Thanks.
Just read the documentation from MySQL. It says.
In MySQL, JOIN, CROSS JOIN, and INNER JOIN are syntactic equivalents (they can replace each other). In standard SQL, they are not equivalent. INNER JOIN is used with an ON clause, CROSS JOIN is used otherwise.
To answer your question: join is the same like a inner join
I have two mySQL statements. First is:
SELECT o.OrderID, c.CustomerName, o.OrderDate
FROM Customers AS c, Orders AS o
WHERE c.CustomerID=o.CustomerID;
The second is:
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers
ON Orders.CustomerID=Customers.CustomerID;
Both produce the same result, but second doesn't contain reference on Customers table in FROM request.
My question is - what is the difference between these two sql statements? In which cases should I use JOIN and in which cases should I use simple SELECT from two tables?
They are the same except the second is easier to read, so you should use that one.
Those JOIN are different, although the result are the same.
The First one is CROSS JOIN and adds the condition in where, which is implicit CROSS JOIN
The second one is INNER JOIN
If you want to connect two tables I would use INNER JOIN instead of CROSS JOIN Because the intention of the inner join table is clearer
I am trying to write an SQL query which will select records of a student within 3 tables that have the same column_I'd.
This is what I wrote but the the records selected are not accurate:
select
Nov_DEC_billing.*,
Nov_DEC_students_portfolio.*,
admission_form.academic_year
from
Nov_DEC_billing,
Nov_DEC_student_portfolio,
admission_form
where
Nov_DEC_billing.ID = Nov_DEC_student_portfolio.ID=admission_form.ID
AND
admission_form.Program ='Nov/dec'
I get a records selected alright but its not accurate. Please what's the right way to join 3 tables that share the same column_id.???
Use JOIN in your query
SELECT b.*, p.*, a.academic_year
FROM Nov_DEC_billing b
JOIN Nov_DEC_student_portfolio p ON p.id = b.id
JOIN admission_form a ON a.id = b.id
WHERE a.Program='Nov/dec'
You need to join tables something like this:
SELECT Nov_DEC_billing.*,
Nov_DEC_students_portfolio.*,
admission_form.academic_year
FROM Nov_DEC_billing AS ndb,
LEFT JOIN Nov_DEC_student_portfolio AS ndsp ON ndsp.ID=ndb.ID,
LEFT JOIN admission_form AS af ON af.ID=ndb.ID
WHERE af.Program='Nov/dec'
You should join all the tables to a single one.
What you will is join all the tables to a single one and then select from it.
Since you have 2 tables you should first join 2 and then join another one on the result.
See here left join example for the exact syntax.
Nov_DEC_billing.ID=Nov_DEC_student_portfolio.ID=admission_form.ID
doesn't do what you expect. It takes the first part, Nov_DEC_billing.ID=Nov_DEC_student_portfolio.ID and evaluates it. If the values match, that part becomes a 1, if they don't match, it becomes 0. Then the 0 or the 1 is compared against admission_form.ID. That is very likely to give strange results.
So you'd have to split that into:
Nov_DEC_billing.ID=Nov_DEC_student_portfolio.ID
AND Nov_DEC_student_portfolio.ID=admission_form.ID
Or just use explicit join syntax, as the others already advised (and which I do too). That forces you to split this anyway.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
INNER JOIN ON vs WHERE clause
INNER JOIN:
SELECT *
FROM PETS
INNER JOIN OWNER ON PETS.OWNER_ID = 1
AND OWNER.ID = 1
MULTIPLE TABLE SELECT:
SELECT *
FROM PETS, OWNER
WHERE PETS.OWNER_ID = 1
AND OWNER.ID = 1
Is one better than the other?
Faster than the other?
They seem to produce exactly the same results.
Is there any difference at all?
I am trying to learn the best methods. In using the join, I noticed that the exact same thing can be achieved with the multiple table call
read this answer about the difference between a cross join and an inner join Performance of inner join compared to cross join