A concise tutorial of SQL statements for Linux---external connections

Source: Internet
Author: User

The LEFT join we saw earlier, also known as an internal join (inner join). In this case, the same value should be found in all two tables, and the information will be selected. What if we want to list each of the data in a table, regardless of whether its value appears in another table? At this point, we need to use the SQL OUTER join(external connection) instructions.

The syntax for an external connection differs depending on the database. For example, on Oracle, we would add a "(+)" to the table in the where clause to select all the data, and we would say all the information in the table.

Let's say we have the following two tables:

store_information Form

Store_name Sales Txn_date
Los Angeles 1500 05-jan-1999
San Diego 250 07-jan-1999
Los Angeles 300 08-jan-1999
Boston 700 08-jan-1999

Geography Form

Region_name Store_name
East Boston
East New York
West Los Angeles
West San Diego

We need to know the turnover of each store. If we use a normal connection, we will miss the ' New York ' store because it does not exist in the store_information form. So, in this case, we need to concatenate these two tables with an external connection:

SELECT A1. Store_name, SUM (A2. Sales) Sales
From Georgraphy A1, Store_information A2
WHERE A1. Store_name = A2. Store_name (+)
GROUP by A1. Store_name;

We are here to use the external connection syntax of Oracle.

Results:

Store_name SALES
Boston 700
New York
Los Angeles 1800
San Diego 250

Note: SQL returns a NULL value when the second table has no relative data. In this example, ' New York ' does not exist in the store_information table, so its "SALES" field is NULL.

Linux measured results:



Reprint please specify: Xiao Liu

A concise tutorial of SQL statements for Linux---external connections

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.