syntax error at or near "JOIN" for postgresql - sql

I am trying to join two tables, and for some reason I keeping getting:
ERROR: syntax error at or near "JOIN"
My code is:
SELECT
c.visit,
d.cake
FROM
customer c,
INNER JOIN desert d
ON c.visit = d.visit
LIMIT 10;

The comma is unnecessary:
SELECT
c.visit,
d.cake
FROM
customer c -- here
INNER JOIN desert d
ON c.visit = d.visit
LIMIT 10;

Related

ERROR: 'Incorrect syntax near the keyword 'ORDER'.'

I'm getting an error from my SQL query:
SELECT TOP 20 * FROM
(
SELECT DISTINCT
p.ItemGroupName, p.Varenummer, s.EAN, s.inventoryQuantity
FROM
ShopInventory s, ProductData p
WHERE s.EAN = p.EAN
)
ORDER BY cast(inventoryQuantity AS int) DESC
ERROR: 'Incorrect syntax near the keyword 'ORDER'.'
Probably, you just need to give the subquery an alias:
SELECT TOP 20 * FROM
(
SELECT DISTINCT
p.ItemGroupName, p.Varenummer, s.EAN, s.inventoryQuantity
FROM
ShopInventory s, ProductData p
WHERE s.EAN = p.EAN
) mytable
ORDER BY cast(inventoryQuantity AS int) DESC
Some would say you are using the old join syntax instead of the recommended JOIN clause but for the purposes of solving your question I think thats a bit of a distraction. If you're interested in INNER JOIN , OUTER JOIN and all that you can read up here: What is the difference between "INNER JOIN" and "OUTER JOIN"?

Unknown SQL Syntax Error near Keyword 'left'

I am getting a syntax error "incorrect syntax near the keyword 'left'," but I don't know what I am doing wrong. I am trying to run an update query to set French addresses to 5. What am I missing?
UPDATE
Persons p
left join States s on p.StateID = p.pkState
SET
p.International = 5
WHERE
s.CountryRegionCodeID = 'FR';
The correct syntax in SQL Server uses a FROM clause:
UPDATE p
SET p.International = 5
FROM Persons p JOIN
States s
ON p.StateID = p.pkState
WHERE s.CountryRegionCodeID = 'FR';
Note: I changed the LEFT JOIN to a JOIN. The WHERE clause is turning the outer join into an inner join anyway.

External data from file throws error while using where clause

I am trying to read a text file with 2,000 values, but my where clause throws error.
SELECT mac.mac_id,mac.mac,mac.mac_type,record.soc_id
from mso_charter.mac
where record.soc_id in ('C:\Users\xyz\worldbox2_Prod_09-17-2017.txt')
join mso_charter.record on mac.record_id = record.header_id;
ERROR: syntax error at or near "join"
LINE 4: join mso_charter.record on mac.record_id = record.header_id;
^
********** Error **********
ERROR: syntax error at or near "join"
SQL state: 42601
Character: 155
This:
SELECT mac.mac_id,mac.mac,mac.mac_type,record.soc_id
from mso_charter.mac
join mso_charter.record on mac.record_id = record.header_id
where record.soc_id in ('C:\Users\xyz\worldbox2_Prod_09-17-2017.txt');
and unless you have multiple soc_Id's
where record.soc_id = ('C:\Users\xyz\worldbox2_Prod_09-17-2017.txt');
Not this:
SELECT mac.mac_id,mac.mac,mac.mac_type,record.soc_id
from mso_charter.mac
where record.soc_id in ('C:\Users\xyz\worldbox2_Prod_09-17-2017.txt')
join mso_charter.record on mac.record_id = record.header_id
Compilers are pretty picky about the order
SELECT
FROM
JOIN
WHERE
GROUP BY
HAVING
ORDER BY
Though order of execution is
FROM
JOIN
WHERE
GROUP BY
SELECT
HAVING
ORDER BY

Using "With" SQL on Oracle 10g causing errors

First ill tell you what the logic in my code is, after my last post it was pointed out to me that my procedure was inefficient and that i should think about logic of my approach..
To put it simply, i want to join a bunch of tables and filter them out to reflect a certain scheme, the process in "Z" in the bellow code
And then parse through that data using y on z...
Looking at the examples online i cant see why this code dosnt work, i have read in a few places that it may be a oracle 10g issue but note sure.. any recommendations would be great
The error i get is "ORA-00904: "Z"."COMMENTS": invalid identifier"
with
z as
(
Select *
FROM
(
iacd_note c
inner join iacd_ncr_note e on C.NOTE_ID=E.NOTE_ID
inner join iacd_ncr f on E.NCR_ID=F.NCR_ID
inner join iacd_ncr_iac g on F.NCR_ID=G.NCR_ID
)
WHERE c.create_date >= date'2014-01-01'
AND c.create_date < date'2014-12-31'
AND G.SCHEME_ID in (36,37,38,25,26,27,28,29,30,31,32,33,34,35,39,40,44,42,43,45, 48,49,50,51,52,55,56,57,58,68,69,70,71)
),
y as
(
Select *
From iacd_asset
)
SELECT y.bridge_no, COUNT(*) AS comment_cnt
FROM y INNER JOIN z
ON REGEXP_LIKE(z.comments, '(^|\W)BN' || y.bridge_no || '(\W|$)', 'i')
GROUP BY y.bridge_no
ORDER BY comment_cnt;
Z.COMMENTS should be part of the merges happening in z
This subquery is invalid SQL syntax:
Select *
FROM
(
iacd_note c
inner join iacd_ncr_note e on C.NOTE_ID=E.NOTE_ID
inner join iacd_ncr f on E.NCR_ID=F.NCR_ID
inner join iacd_ncr_iac g on F.NCR_ID=G.NCR_ID
)
WHERE ...
You can't put parentheses around the FROM clause. Instead:
Select *
FROM iacd_note c
inner join iacd_ncr_note e on C.NOTE_ID=E.NOTE_ID
inner join iacd_ncr f on E.NCR_ID=F.NCR_ID
inner join iacd_ncr_iac g on F.NCR_ID=G.NCR_ID
WHERE ...
Looks like the output of a with clause may not have the same column names as the original tables, so i selected all of z and noticed there was an odd automated name for the row i was after...

Can Derby handle scalar subqueries in SELECT clause?

I'm having trouble getting my query working. Could someone cast an experienced eye on it please? The table structure is simple (2 one-to-many relationships). The query is trying to work out for each sign, how many contributions there are at each unique "PositionLocation".
Sign <- Signifier (f_key sign_oid) <- Contribution (f_key signifier_oid)
I'm getting the following error:
Error: An ON clause associated with a JOIN operator is not valid.
SQLState: 42972
ErrorCode: -1
My query is:
select s.NAME, c.POSITIONLOCATION, count(*) as num_per_locn,
(
select count(*) from APP.CONTRIBUTION c2
inner join APP.SIGNIFIER si2 on si2.OID = c2.SIGNIFIER_OID
inner join APP.SIGN s2 on s2.OID = si2.SIGN_OID
and s2.OID = s.OID
) as num_per_sign
from APP.CONTRIBUTION c
inner join APP.SIGNIFIER si on si.OID = c.SIGNIFIER_OID
inner join APP.SIGN s on s.OID = si.SIGN_OID
group by s.NAME, c.POSITIONLOCATION