Getting started with SQL

Source: Internet
Author: User

Getting started with SQL

If you want to query two or more tables, you need to join the tables ).

0. flute Product

The simplest join method is to directly add two tables to the from clause and separate them with the join operator. The format is Table1 join Table2. The following is an example:

select e.fname, e.lname, d.namefrom employee e join department d;

108 rows of data are generated, because only join operations are performed on the Cartesian product. For two tables, m and n tuples are generated after Cartesian product. However, this is not the result we need. It contains a lot of unnecessary data. To obtain the correct result, you need to establish an internal connection.

1. Internal Connection

The internal connection is connected based on the same attribute value. You only need to add the on and all required attributes in the preceding table.

select e.fname, e.lname, d.namefrom employee e join department don e.dept_id = d.dept_id;

The result is exactly what you want. In fact, it is the default situation to add inner before join. But for good habits, it should be added to indicate what the connection is, which helps to read the code. On e. dept_id = d. dept_id can be replaced by using (dept_id.

The above results can also be operated using the SQL92 standard.

 

select e.fname, e.lname, d.namefrom employee e, department dwhere e.dept_id = d.dept_id;

 

The two standards have their own advantages and disadvantages.

2. Self-connection

Not only does a query contain the same table multiple times, but it can also be connected to the table itself. You only need to take different aliases.

Summary: The Connection operation method is easy to understand. For more than three connections, the connection subquery and unequal connections are similar.

 

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.