• Home
  • SQL Natural Join

    Natural Join:

    It selects the rows from the two tables that have equal value in all matched columns. If the columns have the same name but different data types then natural join will give an error. It is not possible to do a join without explicitly specifying the column in the corresponding tables. It is possible to let the join be completed automatically based on columns in the two tables which have matching data types and names using the natural join keyword.

    Example:

    Select emp-id, emp-name, dept-id, dept-name
        From emp
        Natural join dept ;
    

    In the above example dept table is joined to the emp table by the dept-id column which is the only column of the same name and data type in both the tables. The above example can also be written as an equijoin.

    Example:

    Select emp-id, emp-name, dept-id, dept-name
        From emp, dept
        Where emp. dept-id = dept. dept-id;
    

    Note:

    in a natural join it is also possible to add a where clause.


    Views:

    A view is a virtual table in the database defined by a query. A view does not exist in the database as a stored set of data values. The rows and columns of data visible through the view are produced by the query that defines the view.

    Create view view-name column-name1, column-name2,as query
        <the create view statement syntax>
    

    Horizontal view :

    Horizontal view restricts a user's access to selected rows of a table.

    Create view view_cust AS
        Select * From customer_details
        Where cust_id in(101,102,103) ;
    

    <horizontal view>


    Vertical view :

    Vertical view restricts a user's access to select columns of a table.

    Create view view_cust AS
        Select cust_id, account_no, account_type
        From customer_details ;
    

    <vertical view>


    About the Author



    Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

    We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





     PreviousNext