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
Related
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;
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.
I have the following query:
SELECT MS.idReg, MS.dsMotivo, A.contrato FROM MS
INNER JOIN S
ON S.motivoSiniestro = MS.idReg
INNER JOIN C
ON C.n__contrat = S.n__contrat
INNER JOIN A
ON A.n__article = C.n__article
Table MS has only 12 records, the ones I need and others have many more entries.
My problem is that I only want the 12 records from MS and their contrato column but I'm getting much more that that. Have tried many combinations of INNER, OUTER, LEFT and RIGHT joins. Any help?
You get too many records because there are several A.contrato values for each row in the MS table. Sql server does not know which one of all the A.contrato values to take so it returns all of them. First you need to decide which one you want.
If any will do you can simply write your query like this:
SELECT MS.idReg, MS.dsMotivo, MAX(A.contrato)
FROM MS
INNER JOIN S
ON S.motivoSiniestro = MS.idReg
INNER JOIN C
ON C.n__contrat = S.n__contrat
INNER JOIN A
ON A.n__article = C.n__article
GROUP BY MS.idReg, MS.dsMotivo
Try this one -
SELECT MS.idReg, MS.dsMotivo, A.contrato
FROM dbo.MS
OUTER APPLY (
SELECT TOP 1 A.contrato
FROM dbo.S
JOIN dbo.C ON C.n__contrat = S.n__contrat
JOIN dbo.A ON A.n__article = C.n__article
WHERE S.motivoSiniestro = MS.idReg
) s
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.
I am getting the following error when trying to run this query in SQL 2005:
SELECT tb.*
FROM (
SELECT *
FROM vCodesWithPEs INNER JOIN vDeriveAvailabilityFromPE
ON vCodesWithPEs.PROD_PERM = vDeriveAvailabilityFromPE.PEID
INNER JOIN PE_PDP ON vCodesWithPEs.PROD_PERM = PE_PDP.PEID
) AS tb;
Error: The column 'PEID' was specified multiple times for 'tb'.
I am new to SQL.
The problem, as mentioned, is that you are selecting PEID from two tables, the solution is to specify which PEID do you want, for example
SELECT tb.*
FROM (
SELECT tb1.PEID,tb2.col1,tb2.col2,tb3.col3 --, and so on
FROM vCodesWithPEs as tb1 INNER JOIN vDeriveAvailabilityFromPE as tb2
ON tb1.PROD_PERM = tb2.PEID
INNER JOIN PE_PDP tb3 ON tb1.PROD_PERM = tb3.PEID
) AS tb;
That aside, as Chris Lively cleverly points out in a comment the outer SELECT is totally superfluous. The following is totally equivalent to the first.
SELECT tb1.PEID,tb2.col1,tb2.col2,tb3.col3 --, and so on
FROM vCodesWithPEs as tb1 INNER JOIN vDeriveAvailabilityFromPE as tb2
ON tb1.PROD_PERM = tb2.PEID
INNER JOIN PE_PDP tb3 ON tb1.PROD_PERM = tb3.PEID
or even
SELECT *
FROM vCodesWithPEs as tb1 INNER JOIN vDeriveAvailabilityFromPE as tb2
ON tb1.PROD_PERM = tb2.PEID
INNER JOIN PE_PDP tb3 ON tb1.PROD_PERM = tb3.PEID
but please avoid using SELECT * whenever possible. It may work while you are doing interactive queries to save typing, but in production code never use it.
Looks like you have the column PEID in both tables: vDeriveAvailabilityFromPE and PE_PDP. The SELECT statement tries to select both, and gives an error about duplicate column name.
You're joining three tables, and looking at all columns in the output (*).
It looks like the tables have a common column name PEID, which you're going to have to alias as something else.
Solution: don't use * in the subquery, but explicitly select each column you wish to see, aliasing any column name that appears more than once.
Instead of using * to identify collecting all of the fields, rewrite your query to explicitly name the columns you want. That way there will be no confusion.
just give new alias name for the column that repeats,it worked for me.....