NoobsDeSroobs

SQL puzzles

Nov 5th, 2013
670
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. I was asked what "=" and "IN" really do. Let us say we have two tables,
  2. Positions(EmployeeID, JobID, Season, Year). Those who got a job.
  3. Applications(EmployeeID, Year, Season, JobID) The applications for jobs.
  4.  
  5. If I say SELECT Positions.EmployeeID FROM Positions, Applications WHERE Positions.JobID = Applications.JobID; what does it do?
  6. Does it select only the ones where there is, for every JobID in the Positions table, an entry with the same JobID in Applications?
  7. If Applications had only one entry and that entry had an JobID = 6, then every entry with the same ID in Positions show.
  8. How is IN any different?
  9.  
  10. What if we nest them?
  11.  
  12. SELECT Positions.EID
  13. FROM Positions, Applications
  14. WHERE Positions.JobID NOT IN --Filter out those who have the same JobID as in the temporary table created below
  15. (SELECT Applications.JobID FROM Applications WHERE Applications.EID=Positions.EID) --the IDs where we have a match
  16. AND Positions.EID=Applications.EID --Remove all entries that also do not have the same EID
  17. GROUP BY Applications.EID
  18. HAVING 3<count(Applications.EID);
  19.  
  20. Will it change if I change IN into =?
  21.  
  22. SELECT Positions.EID
  23. FROM Positions, Applications
  24. WHERE Positions.JobID != --Filter out those who have the same JobID as in the temporary table created below
  25. (SELECT Applications.JobID FROM Applications WHERE Applications.EID=Positions.EID) --the IDs where we have a match
  26. AND Positions.EID=Applications.EID --Remove all entries that also do not have the same EID
  27. GROUP BY Applications.EID
  28. HAVING 3<count(Applications.EID);
Advertisement
Add Comment
Please, Sign In to add comment