results of 1st select statement to drive 'where' part of 2nd select statement

  • I must confess while ive dabbled in front end SQL, Im trying to improve my elementary skills. This is probably a really stupid question, however your help would be really appreciated.

    I want to build a statement where the results of the first query I run, can be used to drive the 'where' part of a second select statement.

    So for instance, lets say im looking at football data.

    My first select might be something like:

    Select

    footballer_name

    mins_played

    From football.table

    Where mins_played > 300

    I want to use the results of the above to drive a second query such as:

    Select

    footballer_name

    goals_scored

    From another.table

    Where footballer_name in (the results above!)

    I hope that makes sense? Sorry if Im being stupid! I assume I need to declare the results of a column in the first query or something?

  • Hello,

    Try Like this

    Select

    footballer_name,

    goals_scored

    From another.table

    Where footballer_name in (Select footballer_name

    From football.table

    Where mins_played > 300 )

    Hope this helps

  • Or

    SELECT f.footballer_name

    ,a.goals_scored

    FROM football.table f

    JOIN another.table a

    ON f.footballer_name = a.footballer_name

    WHERE f.mins_played > 300

    The problem you defined is a classic and simple JOIN between two tables.

    ----------------------------------------------------The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood... Theodore RooseveltThe Scary DBAAuthor of: SQL Server 2017 Query Performance Tuning, 5th Edition and SQL Server Execution Plans, 3rd EditionProduct Evangelist for Red Gate Software

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply