Common ways to combine multiple table queries into a single declaration

Source: Internet
Author: User
Tags comparison join one table resource

When you combine data across multiple tables, it is sometimes difficult to figure out which SQL syntax to use. I'll be here to illustrate the common way to combine queries from multiple tables into a single declaration.

The sample query in this article conforms to the SQL92 ISO standard. Not all database manufacturers follow this standard, and some of the improvements that many vendors take will bring some unintended consequences. If you are unsure whether your database supports these standards, you can refer to the manufacturer for information.

SELECT

A simple select declaration is the most basic way to query multiple tables. You can call multiple tables in the FROM clause to combine the results from multiple tables. Here is an example of how it works:

SELECT table1.column1, table2.column2 FROM table1,
table2 WHERE table1.column1 = table2.column1;

In this instance, I use the dot number (TABLE1.COLUMN1) to specify which table the column comes from. If the columns involved are only present in a reference table, you do not need to add the full name, but adding the full name will help with readability.

The tables are separated by commas in the FROM clause, and you can add as many tables as you need, although some databases have a limitation on what they can effectively handle before introducing a formal join declaration, which is discussed below.

This syntax is a simple inner JOIN. Some databases regard it as equivalent to an external join. The WHERE clause tells the database which area to associate, and it returns the result, just as the table listed is grouped together into a separate table under given conditions. It is worth noting that your comparison requirements do not need to be the same as the columns you returned as a result group. In the example above, Table1.column1 and table2.column1 are used to assemble the table, but the return is table2.column2.

You can use the and keyword in the WHERE clause to extend this functionality to more than two tables. You can also use this combination of tables to limit your results without actually returning columns from each table. In the following example, Table3 matches table1, but I do not return anything from Table3 to display. I'm just making sure that the columns from table1 are in Table3. Note that table3 in this example needs to be referenced in the FROM clause.

SELECT table1.column1, table2.column2 FROM table1,
table2, table3 WHERE table1.column1 =
table2.column1 AND table1.column1 = table3.column1;

However, it should be noted that the way to query multiple tables is a shadow-pointing join. Your database may handle things differently, depending on the tuning engine it uses. Also, ignoring the definition of the relevant attributes of the WHERE clause will give you a result that you don't want to see, such as returning the Rogue field of a column related to each possible result from the rest of the query, as in the cross join.

If you are accustomed to the way your database handles this type of declaration, and you combine only two or a few tables, a simple SELECT statement can achieve the goal.

JOIN

A join works the same way as a select declaration, and it returns a result group with columns from a different table. The advantage of using an external join on the implied join is better control of your result group, and it may also improve performance in the case of many tables involved.

There are several types of join: Left,right,full Outer,inner and Cross. The type you are using is determined by the results you want to see. For example, using the left OUTER join will return all the related rows from the first table listed, and potentially add rows from the second table listed if there is no information associated with the first table.

Here INNER join and implied join are different, INNER join will return only those rows that have data in two tables.

Use the following join declaration for the first select query:

SELECT table1.column1, table2.column2 FROM table1 INNER JOIN table2
ON table1.column1 = table2.column1;

Child query

A subquery, or a child selection declaration, is a way to use a result group as a resource in a query. He is often used to limit or define results, rather than running multiple queries or manipulating data in the application software. With a subquery, you can refer to the table to determine the content of the data or, in some cases, return a column, which is the result of a child selection.

Two tables are used in the following example. One table contains the data I want to return, and the other table gives a comparison point to determine what data I am really interested in.

SELECT column1 FROM table1 WHERE EXISTS
( SELECT column1 FROM table2
WHERE table1.column1 = table2.column1 );

One important aspect of subqueries is performance. Convenience comes at a price, depending on the size, volume, and complexity of the forms and declarations you use, and you may be able to allow your application to do the processing work. Each query is handled completely separately before being used as a resource by the primary query. Creative use of join declarations, if possible, can provide the same information with less lag time.

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.