Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
tojo
Starting Member
5 Posts |
Posted - 2015-01-30 : 11:41:45
|
I practiceing sql question by using Kihlman's SQL webbsitepk = primary keyfk = forign keyI have these four tablesFirm(FirKey, FirName) pk is FirKeySalary(SalWages, SalTaxYear, FirKey, EmpId) pk is (FirKey, EmpId, SalTaxYear) fk is FirKey and EmpIdEmployee(EmpId, EmpFirstName, EmpLastName, EmpPartner,TaxId, EmpLastTaxRaid) pk is EmpId fk is TaxId and EmpPartnerTaxDistrict(TaxId, TaxName, TaxPercentage) pk is TaxIdThe relation is many to many between Firm and Employee so we get an extra relation table in between that we have named SalaryWe have relation one to many from TaxDistrict to EmployeeWe have also a self join in table Employee for column EmpPartner so EmpPartner will become a forign key here I supposeFirm (1) ........(N)Salary(N) .............. (1) Employee(N) ................(1)TaxDistrictHere we have a self joinEmployee.EmpId (1) .................(N) Employee.EmpPartnerI want to write a SQL question that will give a result that satisfy this question. The tax authority suspect person with id number 3 to be a bigamist! Find all information in the Employee table about all his wifes?I tries with thisSELECT * FROM Employee a1 Employee a2 WHERE a1.EmpId = a2.EmpPartner AND a2.EmpId = 3;I get this error when I try the SQL question above.Tables are wrong: The number of tables is correct but at least one table is not correctConditions are wrong: Conditions are expected but too many have been written.So can somebody tell me how I should write the SQL question to satisfy this. "The tax authority suspect person with id number 3 to be a bigamist! Find all information in the Employee table about all his wifes".Obviously my SQL question is not correct according to how Kihlman's SQL want to have it.//Tony |
|
jleitao
Posting Yak Master
100 Posts |
Posted - 2015-01-30 : 12:58:14
|
i think you forgot the "," between the tablesSELECT * FROM Employee a1, Employee a2 WHERE a1.EmpId = a2.EmpPartner AND a2.EmpId = 3However i suspect you can discover "the bigamist problem" with a LEFT JOIN------------------------PS - Sorry my bad english |
|
|
|
|
|
|
|