Share via

Newbie question , get data from table

chan patrick 6 Reputation points
2022-07-24T01:32:51.55+00:00

Sorry to ask such simple question here , as I am new to database , the below is example , in this example , there are primary key and foreign key , If I want to extract the data of what subject the student ( ID ) taken , how to make it ? thanks a lot

SQL Table 1

==========

student ID student Name

0001 Peter

0002 Mary

0003 John

SQL Table 2

=====

Student Name Subject

Peter English

Mary Maths

Join Biology

What I want to get is as below

=============

0001 English

0002 Maths

0003 Biology

Azure SQL Database
SQL Server Reporting Services
SQL Server Reporting Services

A SQL Server technology that supports the creation, management, and delivery of both traditional, paper-oriented reports and interactive, web-based reports.

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL

A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.


2 answers

Sort by: Most helpful
  1. LiHong-MSFT 10,061 Reputation points
    2022-07-25T01:40:33.22+00:00

    Hi @chan patrick
    We usually use JOIN statement to combine data or rows from two or more tables based on a common field between them.
    Different types of Joins are as follows: INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN, CROSS JOIN
    If you only want to display Student IDs that have corresponding Subjects in Table2, then use INNER JOIN:

    SELECT [Student ID],[Subject]  
    FROM Table1 T1 INNER JOIN Table2 T2 ON T1.[Student Name] = T2.[Student Name]  
    

    If you want to display all the Student IDs, even if there is no corresponding Subject in Table2, then you can use LEFT JOIN:

    SELECT [Student ID],[Subject]  
    FROM Table1 T1 LEFT JOIN Table2 T2 ON T1.[Student Name] = T2.[Student Name]  
    

    You can refer this blog for more details and examples: SQL | Join (Inner, Left, Right and Full Joins)

    Best regards,
    LiHong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments

  2. Alberto Morillo 35,506 Reputation points MVP Volunteer Moderator
    2022-07-24T02:26:02.84+00:00

    Welome to Microsoft QA forums!

    About your question, please try the following query:

    SELECT [Student ID], [Subject]  
    FROM [SQL Table 1] t1 INNER JOIN [SQL Table 2] t2  
    ON t1.[Student Name] = t2.[Student Name]  
    

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.