SQL syntax error Multiple inner join - sql

SELECT *
FROM tproducts
INNER JOIN torder ON tproducts.Product_ID=torder.Product_ID
INNER JOIN tcustomer ON torder.Customer_ID=tcustomer.Customer_ID
Can anyone see what is wrong with this as VB.net says that there is a missing operator and i cant spot it?

In MS Access, you need to use parentheses for multiple joins:
SELECT *
FROM (tproducts INNER JOIN
torder
ON tproducts.Product_ID = torder.Product_ID
) INNER JOIN
tcustomer
ON torder.Customer_ID = tcustomer.Customer_ID;
No other database requires this, and using parentheses like this looks awkward for any other database.

Related

I want to use a declared table name in inner select statement but cant

SELECT DISTINCT
W.AVG_WGHT
FROM
***[LOT] L***
INNER JOIN [DBO].[OWNR_MKUP] MK ON L.[OWNR_INFO_ID= MK.[OWNR_ID]
,(SELECT [AVG_WGHT]
FROM [DBO].[DLY_LOT_SUM] DLS
INNER JOIN [DBO].[LOT] L ON DLS.[LOT_ID] = L.[LOT_ID]
WHERE DLS.[COST_DTE] = (SELECT MAX(DTE) FROM [DBO].[DLY_HEAD_CNT] WHERE [LOT_ID] = L.[LOT_ID] AND [CUR_HEAD_CNT] > 0 AND [DTE] <= #COST_DTE
) W
In the inner SELECT statement I have used inner join on LOT L again but I don't want to do that I want to use the LOT L which was previously declared but SQL doesn't allow it to use.
Because of the inner join on LOT L once again I am getting multiple records and I don't want to use this inner SELECT statement in upper SELECT DISTINCT
You are using both explicit join syntax and implicit (comma separated) join syntax. When you implicitly join your subquery without conditions, you get a cartesian product. Comma separated joins have been replaced by explicit join syntax in ANSI-SQL in 92, the use of comma separated joins is discouraged.
What you probably need is:
SELECT DISTINCT
DLS.AVG_WGHT
FROM [LOT] L
INNER JOIN [DBO].[OWNR_MKUP] MK
ON L.[OWNR_INFO_ID= MK.[OWNR_ID]
INNER JOIN [DBO].[DLY_LOT_SUM] DLS
ON DLS.[LOT_ID] = L.[LOT_ID]
--WHERE DLS.[COST_DTE] = [DBO].[DLY_HEAD_CNT] I don't understand this line, what is `[DBO]`?

Left Outer Join with a subquery not executing oracle

I am trying to make a query for that contains left outer join and a query that contains a sub query. I tried the following code:
select c.CustomerName,l.Way,l.Travel
from (select l.HomeAdd from tbl_Location l
where l.HomeAdd like '%Colombo%') from
tbl_Customer c left outer join tbl_Location l
on (l.location_id=c.location_id);
I get the following error message
tbl_Customer c left outer join tbl_Location l
*
ERROR at line 4:
ORA-00933: SQL command not properly ended
Please help!!!
Very strange query. I assume you intend:
select c.CustomerName, l.Way, l.Travel
from tbl_Customer c left outer join
tbl_Location l
on l.location_id = c.location_id and l.HomeAdd like 'Colombo%';
The problem with your query is you have subquery in the where clause. There is no comparison -- not =, no exists, no in, nothing that represents a boolean expression.
It looks like you are missing EXISTS keyword in the query
select c.CustomerName,l.Way,l.Travel
from tbl_Customer c left outer join tbl_Location l
on (l.location_id=c.location_id)
where EXISTS (
select l.HomeAdd from tbl_Location l
where l.HomeAdd like 'Colombo%'
);
If you insist upon using a subquery I suggest you do something like:
select c.CustomerName,
l.Way,
l.Travel
from tbl_Customer c
left outer join (SELECT l.HOMEADD,
l.WAY,
l.TRAVEL
FROM tbl_Location
WHERE l.HOMEADD LIKE 'Colombo%') l
on l.location_id=c.location_id
Also, a comment: I strongly suggest that you should not prefix the names of tables etc, with something like 'TBL_'. When tables, etc, are used the context of the usage tells you what the object is. Oracle identifiers are limited to 30 characters - IMO sacrificing 13% of the available name length to a prefix is simply wasteful.
Best of luck.

Query with columns from 4 tables in SQL

Can anyone who knows SQL, specifically the flavor used in Microsoft Access 2013, tell me what I'm doing wrong here?
SELECT custid, custname, ordno, itemno, itemname
FROM cust
INNER JOIN order
ON cust.custid = order.custid
INNER JOIN orderitems
ON order.ordno = orderitems.ordno
INNER JOIN inv
ON orderitems.itemno = inv.itemno;
I've already read other, similar questions, and tried the methods they used in their solutions, but I'm getting a "Syntax error in FROM clause.", almost no matter what I try.
* * *
SOLUTION: Thanks for the replies! In addition to adding square brackets around "order" and using TableName.ColumnName syntax in SELECT, I had to use parentheses for my multiple INNER JOINs. Here is the fixed code:
SELECT cust.custid, cust.custname, [order].ordno, orderitems.itemno, inv.itemname
FROM ((cust
INNER JOIN [order]
ON cust.custid = [order].custid)
INNER JOIN orderitems
ON [order].ordno = orderitems.ordno)
INNER JOIN inv
ON orderitems.itemno = inv.itemno;
SELECT cust.custid --<-- Use two part name here
,cust.custname
,[order].ordno
,orderitems.itemno --<-- Only guessing here use the correct table name
,inv.itemname --<-- Only guessing here use the correct table name
FROM cust
INNER JOIN [order]
ON cust.custid = [order].custid --<-- used square brackets [] around ORDER as it is
INNER JOIN orderitems -- a key word.
ON [order].ordno = orderitems.ordno
INNER JOIN inv
ON orderitems.itemno = inv.itemno;
In your Select Statament you need to use Two Part name i.e TableName.ColumnName since these column can exist in more than one Tables in your FROM clause you need to tell sql server that columns in your select coming from which table in your from clause.

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

query for inner join of table 4

I'm facing the problem of inner join of table 4
following is query given plz see & give me solution
select INSURED.FNAME + ' ' + INSURED.LNAME AS MNAME
,INSURED.MEMBCODE as MEMBERCODE
,INSURED.POLICYNO AS POLICYNO
,INSURED.POLICYFRMDATE AS POLICYFROMDATE
,INSURED.POLICYTODATE AS POLICYTODATE
, MEMBERSHIP.MRKEXTNAME AS MARKETINGEXECUTIVE
,MEMBERSHIP.EMPLOYEECOUNT AS EMPLOYEECOUNT
,INSURED.CLAIMID AS CLAIMID
,POLICY.POLICYTYPE
,POLICY.COVAMTHOSPITAL as SUMINSURED
,ORGANIZATION.ORGANIZATIONNAME
from ((INSURED
inner join MEMBERSHIP on MEMBERSHIP.MEMBERSHIPID=INSURED.MEMBERSHIPID)
inner join POLICY on MEMBERSHIP.POLICYNAME=POLICY.POLICYNAME)
inner join ORGANIZATION on ORGANIZATION.ORGANIZATIONID=MEMBERSHIP.ORGANIZATIONID
WHERE INSUREDID=427
After making it a bit more readable there doesn't seem to be much wrong. I removed the brackets as they aren't needed. I would also put the INNER JOINS the other way round as its normal to put them in this order.
SELECT column_name(s)
FROM table_name1
INNER JOIN table_name2
ON table_name1.column_name=table_name2.column_name
So it should look something like
select INSURED.FNAME + ' ' + INSURED.LNAME AS MNAME,
INSURED.MEMBCODE as MEMBERCODE,
INSURED.POLICYNO AS POLICYNO,
INSURED.POLICYFRMDATE AS POLICYFROMDATE,
INSURED.POLICYTODATE AS POLICYTODATE,
MEMBERSHIP.MRKEXTNAME AS MARKETINGEXECUTIVE,
MEMBERSHIP.EMPLOYEECOUNT AS EMPLOYEECOUNT,
INSURED.CLAIMID AS CLAIMID,
POLICY.POLICYTYPE,
POLICY.COVAMTHOSPITAL as SUMINSURED,
ORGANIZATION.ORGANIZATIONNAME
FROM INSURED
INNER JOIN MEMBERSHIP on INSURED.MEMBERSHIPID=MEMBERSHIP.MEMBERSHIPID
INNER JOIN POLICY on POLICY.POLICYNAME=MEMBERSHIP.POLICYNAME
INNER JOIN ORGANIZATION on MEMBERSHIP.ORGANIZATIONID=ORGANIZATION.ORGANIZATIONID
WHERE INSUREDID=427
You need to tell us what the problem is. Does it return results different to what you are expecting or does it fail with an error.
You should also tell us which database you are using.
Also there appears to be something odd about your table structure. You would not normally store PolicyName in your Membership Table rather you should be linking with ID Fields of some kind rather than PolicyName Strings...Something Like
INNER JOIN POLICY on POLICY.POLICYID=MEMBERSHIP.POLICYID
You got it right for with OrganizationID and MEMBERSHIPID
In short you need to tell us more about it.
Replace INNER JOIN with LEFT OUTER JOIN.
Get rid of the brackets around your inner join statements.