MYSQL Tutorial Part 7

 JOINS:

    A JOIN clause is used to combine rows from two or more tables, based on a related column between them.

  • INNER JOIN: Returns records that have matching values in both tables.

    SELECT column_names FROM table1 INNER JOIN table2 ON table1.column_name = table2.column_name;

  • LEFT JOIN: Returns all records from the left table, and the matched records from the right table.

    SELECT column_names FROM table1 LEFT JOIN table2 ON table1.column_name = table2.column_name;


  • RIGHT JOIN: Returns all records from the right table and the matched records from the left table.

    SELECT column_names FROM table1 RIGHT JOIN table2 ON table1.column_name = table2.column_name;


  • CROSS JOIN: Returns all records from both tables.

    SELECT column_names FROM table1 CROSS JOIN table2;
    Example
         SELECT Students.name, Departments.DeptHOD, Departments.name FROM Students INNER     JOIN Departments ON Students.department=Departments.name;

SELF JOIN:

  • A SELF JOIN is a regular join, but the table is joined with itself.
  • Using INNER JOIN, LEFT JOIN, RIGHT JOIN, and CROSS JOIN combines the table with another table but SELF JOIN is used to combine the table with the same table.

    Syntax

        SELECT column_names FROM table1 T1, table1 T2 WHERE condition;


    Example
        SELECT stud1.name AS Student1, stud2.name AS Student2, stud1.district FROM Students stud1, Students stud2 WHERE stud1.rollno <> stud2.rollno AND stud1.district = stud2.district ORDER BY stud1.district;

Note: [<> operator is used for not equal condition]

UNION:

    The UNION operator is used to combine the result set of two or more SELECT statements.

  • Every SELECT statement within UNION must have the same number of columns
  • The columns must also have similar data types
  • The columns in every SELECT statement must also be in the same order

    Syntax

        SELECT column_names FROM table1

        UNION

        SELECT column_names FROM table2;

    Example

        SELECT department FROM Students

        UNION

        SELECT department FROM Students2

        ORDER BY department;

UNION ALL:

  • The UNION operator selects only distinct values by default. 
  • To allow duplicate values, use UNION ALL.

    Syntax

        SELECT column_names FROM table1

        UNION ALL

        SELECT column_names FROM table2;

    Example

        SELECT department FROM Students

        UNION ALL

        SELECT department FROM Students2

        ORDER BY department;




                                                Thanks for choosing our blog 😊. 
                                        Will work with an example in the next part.

Tutorial Topics:
Introduction  -  Part-1
Database Creation  -  Part-2
Aggregate Functions  - Part-3
Conditions  - Part-4
Constraints  - Part-5
Clauses  -  Part-6
Joins  -  Part-7


Tutorial Topics for examples:
Database Creation -  Part-1
Table Modifications  -  Part-2
Clauses   - Part-3
Operators  - Part-4
Joins  - Part-5

Comments

Popular posts from this blog

MySQL Tutorial Part 1

Pandas DataFrame empty

Introduction to Java