How to use the SQL JOIN keyword

Source: Internet
Author: User

How to use the SQL JOIN keyword

Join keyword to query the data from two or more than two tables based on some of the relationships listed in these tables.


Database Join
keyword in a connection using SQL statements to query the data from two or more than two tables based on some of the relationships listed in these tables.

Tables often involve each other's keys in the database.

A major key is the column (or combination of columns) of a unique value for each row. Each primary key value must be unique on the table. The purpose of this is to bind the data together, the entire table, and not duplicate all the data on each table.

Look at the "person" table:

Address City
p_id LastName FirstName
1 Hansen Ola TIMOTEIVN 10 Sandnes
2 Svendson Tove BORGVN 23 Sandnes
3 Pettersen Kari STORGT 20 Stavanger

Please note that the "p_id" column is the main key in "people" seating. This means that no two rows can have the same p_id. P_ID distinguishes two people, even if they have the same name.

Next, we have the Orders table:

o_id OrderNo p_id
1 77895 3
2 44678 3
3 22456 1
4 24562 1
5 34764 15

Note that the "o_id" column is the primary key "order" table, and that the "p_id" column refers to people in the "People" table and does not use their names.

Note that the relationship table is above the "p_id" column.

Different SQL joins
as we continue with examples, we will list types of connections that can be used, and the differences between them.

Join: A row returns with at least one match in the table
Left connection: Returns all rows from the left table, even if there is no matching correct table
Right connection: Returns all rows from the right table, even if there is no match in the left table
Full connection: When returned, the row has a table in a race

Note: Reprint please indicate from www.111cn.net/database/database.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.