sql syntax error in JOIN - sql

i have this simple SQL join query, which is giving me a syntax error on the second FROM
SELECT * FROM ##temporderstable P
FROM supporder Y join backorder ON P.catalogid = Y.backorder
GROUP BY P.catalogid
i can't figure out whats wrong with it, any hints?
Thanks in advance

You can't have two FROM clauses like that...
You might mean JOIN but you'd need another ON condition:
SELECT *
FROM ##temporderstable P
JOIN supporder Y ON P.catalogid = Y.backorder
JOIN backorder B ON B.xxxxxxxxx = P.xxxxyyyyy
GROUP BY P.catalogid;
The second ON would need to reference a column of B and a column of either P or Y.

SELECT *
FROM
##temporderstable P
JOIN supporder Y
ON P.catalogid = Y.backorder
GROUP BY P.catalogid
Also, your query doesn't have any aggregate functions, so you should think of the need for GROUPing on P.catalogid

Your query has two FROM clause. It should be something like this.
SELECT
*
FROM
##temporderstable P
INNER JOIN
supporder Y
ON
P.catalogid = Y.backorder
GROUP BY
P.catalogid

Two From clause is not applicable. You have to use only one and inside that you have to join two tables.

Related

SQL-Server An aggregate (Count) may not appear in WHERE

Im my query I get the following error message:
An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.
My query is following:
SELECT f.ID as IdFlight
From Flights as f
inner join BookingsFlightsPassenger as b on f.ID = b.ID_flight
WHERE count(b.ID_Flight)<100
How can I solve this? I am not sure what the error message is suggesting
replace "where" by "having" and add group by
SELECT f.ID as IdFlight
From Flights as f inner join BookingsFlightsPassenger as b on f.ID = b.ID_flight
group by f.id
having count(b.ID_Flight)<100
The most efficient way to write this query doesn't use a join:
SELECT bfp.ID_flight as as IdFlight
FROM BookingsFlightsPassenger bfp
GROUP BY bfp.ID_flight
WHERE count(*) < 100;

use Inner Join in SQL

I want to join two tables and then I want to join this result with another table
but it doesn't work
select * from
(
(select SeId,FLName,Company from Sellers) s
inner join
(select SeId,BIId from BuyInvoices) b
on s.SeId=b.SeId
) Y
inner join
(select * from BuyPayments) X
on Y.BIId=X.BIId
thanks
In most databases, your syntax isn't going to work. Although parentheses are allowed in the FROM clause, they don't get their own table aliases.
You can simplify the JOIN. This is a simpler way to write the logic:
select s.SeId, s.FLName, s.Company, bp.*
from Sellers s inner join
BuyInvoices b
on s.SeId = b.SeId inner join
BuyPayments bp
on bp.BIId = b.BIId;

How to using Left Outer Join or Right Outer Join in Oracle 11g

I have a query using "=" in where clause, but it is long time to execute when many datas. How to use the Left Outer Join or Right Outer Join or something like that to increase performance
This is query:
select sum(op.quantity * op.unit_amount) into paid_money
from tableA op , tableB ssl, tableC ss, tableD pl, tableE p
where (op.id = ssl.id and ssL.id = ss.id and ss.type='A')
or
(op.id = pl.id and pl.id = p.id and p.type='B');
Your problem is not left or right joins. It is cross joins. You are doing many unnecessary cartesian products. I'm guessing this query will never finish. If it did, you'd get the wrong answer anyway.
Split this into two separate joins and then bring the results together. Only use the tables you need for each set of joins:
select SUM(val) into paid_money
from (select sum(op.quantity * op.unit_amount) as val
from tableA op , tableB ssl
where (op.id = ssl.id and ssL.id = ss.id and ss.type='A')
union all
select sum(op.quantity * op.unit_amount) as val
from tableA op , tableD pl, tableD p
where (op.id = pl.id and pl.id = p.id and p.type='B')
) t
I haven't fixed your join syntax. But, you should learn to use the join keyword and to put the join conditions in an on clause rather than the where clause.
Are you sure that this query is returning the required data? To me it looks like it will be returning the cartesian product of op, ssl & ss for each op, pl, p match and vice versa.
I would advise that you split it into two seperate queries, union them together, and then sum over the top.

help with alias sql

i am trying to learn about alias in sql for my course however i do not fully understand the command. As part of the work i must do this:
Understanding Complex SQL Queries
(a) Select all columns / fields from master tracks and sound engineer joining the two based on the sound engineers ID and using an alias for each table.
i have got to here:
SELECT *
FROM SOUNDENGINEER AS s
INNER JOIN MASTERTRACK AS m ON m.EDITED_BY,s.SOUND_ENG_ID;
but now i am stuck please help
It's correct but ON clause should be like this On m.EDITED_BY=s.SOUND_ENG_ID;
SELECT * FROM SOUNDENGINEER AS s
INNER JOIN MASTERTRACK AS m ON m.EDITED_BY = s.SOUND_ENG_ID;
SELECT * FROM SOUNDENGINEER AS s INNER JOIN MASTERTRACK AS m ON m.EDITED_BY = s.SOUND_ENG_ID;
Almost right. Try:
SELECT *
FROM SOUNDENGINEER AS s
INNER JOIN MASTERTRACK AS m ON m.EDITED_BY = s.SOUND_ENG_ID;
Your use of the comma might suggest you were trying the alternate equi-join syntax, which would be:
SELECT *
FROM SOUNDENGINEER AS s, MASTERTRACK AS m
WHERE m.EDITED_BY = s.SOUND_ENG_ID;
I prefer the former though, as you are explicitly stating that you are joining the tables with an INNER JOIN statement.
SELECT * FROM SOUNDENGINEER s INNER JOIN MASTERTRACK m ON m.EDITED_BY = s.SOUND_ENG_ID;
this seems to work

SQL joining three tables, join precedence

I have three tables: R, S and P.
Table R Joins with S through a foreign key; there should be at least one record in S, so I can JOIN:
SELECT
*
FROM
R
JOIN S ON (S.id = R.fks)
If there's no record in S then I get no rows, that's fine.
Then table S joins with P, where records is P may or may not be present and joined with S.
So I do
SELECT
*
FROM
R
JOIN S ON (S.id = R.fks)
LEFT JOIN P ON (P.id = S.fkp)
What if I wanted the second JOIN to be tied to S not to R, like if I could use parentheses:
SELECT
*
FROM
R
JOIN (S ON (S.id = R.fks) JOIN P ON (P.id = S.fkp))
Or is that already a natural behaviour of the cartesian product between R, S and P?
All kinds of outer and normal joins are in the same precedence class and operators take effect left-to-right at a given nesting level of the query. You can put the join expression on the right side in parentheses to cause it to take effect first. Remember that you will have to move the ON clauses around so that they stay with their joins—the join in parentheses takes its ON clause with it into the parentheses, so it now comes textually before the other ON clause which will be after the parentheses in the outer join statement.
(PostgreSQL example)
In
SELECT * FROM a LEFT JOIN b ON (a.id = b.id) JOIN c ON (b.ref = c.id);
the a-b join takes effect first, but we can force the b-c join to take effect first by putting it in parentheses, which looks like:
SELECT * FROM a LEFT JOIN (b JOIN c ON (b.ref = c.id)) ON (a.id = b.id);
Often you can express the same thing without extra parentheses by moving the joins around and changing the direction of the outer joins, e.g.
SELECT * FROM b JOIN c ON (b.ref = c.id) RIGHT JOIN a ON (a.id = b.id);
When you join the third table, your first query
SELECT
*
FROM
R
JOIN S ON (S.id = R.fks)
is like a derived table to which you're joining the third table. So if R JOIN S produces no rows, then joining P will never yield any rows (because you're trying to join to an empty table).
So, if you're looking for precedence rules then in this case it's just set by using LEFT JOIN as opposed to JOIN.
However, I may be misunderstanding your question, because if I were writing the query, I would swap S and R around. eg.
SELECT
*
FROM
S
JOIN R ON (S.id = R.fks)
The second join is tied to S as you explicity state JOIN P ON (P.id = S.fkp) - no column from R is referenced in the join.
with a as (select 1 as test union select 2)
select * from a left join
a as b on a.test=b.test and b.test=1 inner join
a as c on b.test=c.test
go
with a as (select 1 as test union select 2)
select * from a inner join
a as b on a.test=b.test right join
a as c on b.test=c.test and b.test=1
Ideally, we would hope that the above two queries are the same. However, they are not - so anybody that says a right join can be replaced with a left join in all cases is wrong. Only by using the right join can we get the required result.