user3078335
user3078335

Reputation: 781

Sql: Selecting from other tables

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.

Upvotes: 0

Views: 70

Answers (3)

Ryan
Ryan

Reputation: 271

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.

Upvotes: 3

A Lombardo
A Lombardo

Reputation: 774

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

Upvotes: 1

Hysteria86
Hysteria86

Reputation: 377

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 *

Upvotes: 1

Related Questions