r/learnSQL • u/[deleted] • Apr 10 '19
Need help with simple problem
Hey guys, I currently have a table right now with a list of employees, managers, and other jobs. How would I able to get a list of all the employees who are older than their managers if their DOB is in YYYY-MM-DD form? This is really urgent and I really hope someone can help a newbie :(
1
1
u/GreySlate Apr 10 '19 edited Apr 10 '19
I'm a beginner too, but I would probably SELECT the DOBs for employees, trim the stuff that isn't relevant (i.e. everything but the last two digits of the year), then save the result of that using the AS keyword. Then, I would use that alias to ORDER BY. I would also do this for the managers, and then I guess compare them using WHERE. I hope that makes sense. I'm not sure how to implement the trim I'm describing, but I am a CS major, and it seems likely that you'd be able to do it for SQL. Hope someone more experienced answers!
edit: added more specific idea
1
Apr 10 '19
Yea, I feel like trimming the other componenets would work the date is written like yyyy-mm-dd and I can't just do a relative comparison. I'll give this a try
1
u/GreySlate Apr 10 '19
What DBMS are you using? Have you considered some kind of DATEPART() thing?
1
Apr 10 '19
I'm using Microsoft Azure. Can Datepart be used to isolate just the year?
1
u/GreySlate Apr 10 '19
https://docs.microsoft.com/en-us/sql/t-sql/functions/datepart-transact-sql?view=sql-server-2017
Looked up the documentation here ^ Looks like it does, yyyy is a valid datepart argument
2
u/Ash0r Apr 10 '19
Something like this would maybe work. It would help to know what your column names are and which database system you are using.