sql join

SQL Advance

homeBack

Next

SQL JOIN

  • The JOIN keyword is used in an SQL statement to query data from two or more tables, based on a relationship between certain columns in these tables. 
    Tables in a database are often related to each other with keys. 
    A primary key is a column (or a combination of columns) with a unique value for each row. Each primary key value must be unique within the table. The purpose is to bind data together, across tables, without repeating all of the data in every table. 
    Table Name ” emp”

    S.no Name1 Name2 City
    2458 Peter Jhon Hyderabad
    4857 Faran Khan New York
    5874 Raj Kiran London
    8578 Sameer Ali Mumbai

    Note that the “S.no” column is the primary key in the “emp” table. This means that no two rows can have the same S.no. The S.no distinguishes two persons even if they have the same name. 

  • Next, we have the “job” table: 
    Table Name “job”

    J.Id Job_no S.no
    001 12345 2458
    002 11121 4857
    003 22211 5874
    004 85741 8578

    Note that the “J.Id” column is the primary key in the “job” table and that the “S.no” column refers to the persons in the “emp” table without using their names. 
    Notice that the relationship between the two tables above is the “S.no” column.

  • Different SQL JOINs

    we will list the types of JOIN you can use, and the differences between them.

    JOIN: Return rows when there is at least one match in both tables 
    LEFT JOIN: Return all rows from the left table, even if there are no matches in the right table 
    RIGHT JOIN: Return all rows from the right table, even if there are no matches in the left table 
    FULL JOIN: Return rows when there is a match in one of the tables 

  • Advertisements

    Leave a Reply

    Fill in your details below or click an icon to log in:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out / Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out / Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out / Change )

    Google+ photo

    You are commenting using your Google+ account. Log Out / Change )

    Connecting to %s