Sql: Selecting from other tables

66 views Asked by At

I have 2 tables. Player and Stats. Player has the fields: Name, Age, DOB and SSN. Stats has the fields: Tackles, Goals, Assists, and SSN. SSN is a foreign key. How can I write a query to find the stats of players with DOB >= '1994'. DOB is not a foreign key, I was wondering how that could work.

3

There are 3 answers

1
Hysteria86 On BEST ANSWER
SELECT * FROM Player INNER JOIN Stats ON Player.SSN = Stats.SSN WHERE Player.DOB >= '1994-01-01'

If you just want specific fields then specify these instead of SELECT *

0
A Lombardo On
select player.name, stats.tackles, stats.goals, stats.assists
from player inner join stats on player.ssn = status.ssn
where year(player.dob) >= 1994
0
Ryan On

this is a simple Join commmand.

SELECT * FROM Stats 
INNER JOIN Player
On Player.SSN = Stats.SSN
WHERE DOB > 1994

there is an issue from here however as you imply that DOB is a string, not a datetime or Integer with those quotes. you cant compare Strings as numbers like you are trying to do. if DOB is just year e.g. 1994, then DOB >= 1994 would work, if its a date or datetime, you need to use Datetime equivalents.