SQL Joining

Embed Size (px)

Citation preview

  • 8/6/2019 SQL Joining

    1/13

    SQL JOINING

  • 8/6/2019 SQL Joining

    2/13

    The JOIN keyword is used in an SQLstatement to query data from two or moretables, based on a relationship between

    certain columns in these tables.

    In joining A primary key is most importantarea of concern which is a column with a

    unique value for each row. Each primary keyvalue must be unique within the table. Thepurpose is to bind data together, acrosstables, without repeating all of the data in

    every table.

  • 8/6/2019 SQL Joining

    3/13

    SQL JOIN

    P_Id LastName FirstName Address City

    P1 ADITY GANGWAR C.G. RAIPUR

    P2 BASANT KUMAR C.G. RAIPUR

    P3 CHAITANYA GUPTA C.G. BHILAI

    P4 DEEPIKA KESHARWANI M.P. BHOPAL

    P5 GURNIT KAUR DELHI RAJENDRA NAGAR

    P6 JYOTI BAMNELE A.P. HYDRABAD

    P7 LAO MANNA MAHARASHTRA MUMBAI

    P8 MAHENDRA KALIHARI RAJASTHAN JAIPUR

    The "P_Id" column is the primary key in the"Persons" table. This means that no two rowscan have the same P_Id. The P_Id distinguishestwo persons even if they have the same name.

  • 8/6/2019 SQL Joining

    4/13

    SQL JOIN

    O_Id OrderNo P_Id1 77895 P1

    2 44678 P3

    3 22456 P4

    4 24562 P1

    5 34764 P6

    6 21543 P8

    The "O_Id" column is the primary key in the"Orders" table and the "P_Id" column refers to thepersons in the "Persons" table without using theirnames. The relationship between the two tables

    above is the "P_Id" column.

  • 8/6/2019 SQL Joining

    5/13

    SQL JOIN Different types of joins

    INNER JOIN: Return rows when there isat least one match in both tables.

    LEFT JOIN: Return all rows from the left

    table, even if there are no matches inthe right table.

    RIGHT JOIN: Return all rows from the

    right table, even if there are nomatches in the left table.

    FULL JOIN: Return rows when there is a

    match in one of the tables.

  • 8/6/2019 SQL Joining

    6/13

    SQL JOIN

    SQL INNER JOIN Syntax:

    SELECT column_name(s) FROM table_name1 INNER JOIN table_name2 ONtable_name1.column_name = table_name2.column_name;

    Example:SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo FROM

    Persons INNER JOIN Orders ON Persons.P_Id = Orders.P_Id;

    SQL LEFT JOIN Syntax:SELECT column_name(s) FROM table_name1 LEFT JOIN table_name2 ONtable_name1.column_name = table_name2.column_name;

    Example:SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo FROMPersonsLEFT JOIN Orders ON Persons.P_Id = Orders.P_Id ORDER BYPersons.LastName

  • 8/6/2019 SQL Joining

    7/13

    SQL JOIN

    SQL RIGHT JOIN Syntax:SELECT column_name(s) FROM table_name1 RIGHT JOINtable_name2 ONtable_name1.column_name=table_name2.column_nameSELECT Persons.LastName, Persons.FirstName,

    Orders.OrderNo FROM Persons RIGHT JOIN Orders ONPersons.P_Id=Orders.P_Id ORDER BY Persons.LastName;SQL FULL JOIN SyntaxSELECT column_name(s) FROM table_name1 FULL JOIN

    table_name2 ONtable_name1.column_name=table_name2.column_name;Example:SELECT Persons.LastName, Persons.FirstName,Orders.OrderNo FROM Persons FULL JOIN Orders ON

    Persons.P_Id=Orders.P_Id ORDER BY Persons.LastName;

  • 8/6/2019 SQL Joining

    8/13

    SQL UNION

    The SQL UNION Operator: The UNION operator is used tocombine the result-set of two or more SELECT statements.Each SELECT statement within the UNION must have thesame number of columns. The columns must also havesimilar data types. Also, the columns in each SELECT

    statement must be in the same order.

    SQL UNION SyntaxSELECT column_name(s) FROM table_name1 UNION SELECT column_name(s)FROM table_name2

    Note: The UNION operator selects only distinct values by default. To allowduplicate values, use UNION ALL.

    SQL UNION ALL SyntaxSELECT column_name(s) FROM table_name1 UNION ALL SELECTcolumn_name(s) FROM table_name2

  • 8/6/2019 SQL Joining

    9/13

    Table Name: "Employees_Norway":

    E_IDE_Name01Hansen, Ola

    02Svendson, Tove03

    Svendson, Stephen04Pettersen, KariTable Name: "Employees_USA":

    Table Name: "Employees_Norway":

    E_ID E_Name

    01 Hansen, Ola

    02 Svendson, Tove

    03 Svendson, Stephen

    04 Pettersen, Kari

    Table Name: "Employees_USA":

    E_ID E_Name

    01 Turner, Sally

    02 Kent, Clark

    03 Svendson, Stephen

    04 Scott, Stephen

    SELECT E_Name FROM

    Employees_Norway UNION

    SELECT E_Name FROM

    Employees_USA.

    If we want to list all the different employees in Norway and USA.

    Hansen, Ola

    Svendson, Tove

    Svendson, Stephen

    Pettersen, Kari

    Turner, Sally

    Kent, Clark

    Scott, Stephen

    Note: This command cannotbe used to list all employeesin Norway and USA. In theexample above we have two

    employees with equal names,and only one of them will belisted. The UNIONcommand selects onlydistinct values.

  • 8/6/2019 SQL Joining

    10/13

    SQL UNION ALL

    Now we want to list all employees in Norway and USA:

    SELECT E_Name FROM Employees_Norway UNION ALL SELECT E_NameFROM Employees_USA

    Result:

    E_Name

    Hansen, Ola

    Svendson, Tove

    Svendson, Stephen

    Pettersen, Kari

    Turner, Sally

    Kent, Clark

    Svendson, Stephen

    Scott, Stephen

  • 8/6/2019 SQL Joining

    11/13

    The SQL SELECT INTO Statement

    The SELECT INTO statement selects data fromone table and inserts it into a different table.The SELECT INTO statement is most often usedto create backup copies of tables.

    SELECT * INTO new_table_name [INexternaldatabase] FROM old_tablename

  • 8/6/2019 SQL Joining

    12/13

    The SQL SELECT INTO Statement

    Example: Make a Backup Copy.SELECT * INTO Persons_Backup FROM Persons

    We can also copy only a few fields into the newtable:

    SELECT LastName,FirstName INTO

    Persons_Backup FROM Persons

  • 8/6/2019 SQL Joining

    13/13

    SQl SELECT INTO statement

    SQL SELECT INTO - With a WHERE ClauseThe following SQL statement creates a"Persons_Backup" table with only the personswho lives in the city "Sandnes":

    SELECT LastName,Firstname INTOPersons_Backup FROM Persons WHERECity='Sandnes'

    SQL SELECT INTO - Joined Tables

    The following example creates a"Persons_Order_Backup" table contains datafrom the two tables "Persons" and "Orders":