SQL INNER JOIN statement: examples, syntax and features

Table of contents:

SQL INNER JOIN statement: examples, syntax and features
SQL INNER JOIN statement: examples, syntax and features
Anonim

The development of any database implies not only the creation and filling of tables with various information, but also further work with the data. To correctly perform various tasks of selecting data from tables and generating reports, the standard Select. construction is used.

sql inner join example
sql inner join example

Selecting data from tables

If we consider the task of selecting data or building some report, we can determine the level of complexity of this operation. As a rule, when working with serious (in terms of information volume) databases, which are formed, for example, in online stores or large companies, the data selection will not be limited to only one table. As a rule, selections can be made from a fairly large number of not only interconnected tables, but also nested queries / subqueries that the programmer himself makes, depending on the task assigned to him. To select from one table, you can use the simplest construction:

Selectfrom Person

where Person is the name of the table from which to select data.

If there is a need to select data from several tables, you can use one of the standard constructions to join several tables.

Methods for connecting additional tables

If we consider the use of such structures at the initial level, then we can distinguish the following mechanisms for connecting the required number of tables for sampling, namely:

  1. Inner Join operator.
  2. Left Join or, this is the second way of writing, Left Outer Join.
  3. Cross Join.
  4. Full Join.

The use of table join operators in practice can be learned by looking at the use of the SQL - Inner Join operator. An example of its use would look like this:

Selectfrom Person

Inner join Subdivision on Su_Person=Pe_ID

The SQL language and the Join Inner Join operator can be used not only to join two or more tables, but also to connect other subqueries, which greatly facilitates the work of database administrators and, as a rule, can significantly speed up the execution of certain complex programs. query structure.

Combining data in tables line by line

sql inner join statement examples
sql inner join statement examples

If we consider connecting a large number of subqueries and collecting data into a single table row by row, then you can also use the Union and Union All operators.

Using these designswill depend on the task set for the developer and the result he wants to achieve in the end.

Description of the Inner Join operator

In most cases, SQL uses the Inner Join operator to join multiple tables. The description of Inner Join in SQL is quite simple to understand for the average programmer who is just starting to understand databases. If we consider the description of the mechanism of operation of this design, we get the following picture. The logic of the operator as a whole is built on the possibility of crossing and selecting only the data that is in each of the tables included in the query.

If we consider such work from the point of view of graphical interpretation, we will get the structure of the SQL Inner Join operator, an example of which can be shown using the following diagram:

sql inner join syntax examples
sql inner join syntax examples

For example, we have two tables whose schema is shown in the figure. They, in turn, have a different number of entries. Each of the tables has fields that are interconnected. If you try to explain the operation of the operator based on the figure, then the returned result will be in the form of a set of records from two tables, where the numbers of the related fields match. Simply put, the query will return only those records (from table number two) that have data in table number one.

Inner Join operator syntax

As mentioned earlier, the Inner Join operator, namely its syntax, is extremely simple. To organize relationships between tables within the same sample, it will be enough to remember anduse the following principle for constructing an operator, which is written in one line of the program SQL code, namely:

Inner Join [Table name] on [key field from the table to which we are connecting]=[Key field of the table to be connected to]

This statement uses the master keys of the tables to link. As a rule, in the group of tables that store information about employees, the previously described Person and Subdivision have at least one similar record. So, let's take a closer look at the SQL Inner Join operator, an example of which was shown a little earlier.

Example and description of connecting to a single table selection

We have a Person table that stores information about all employees working in the company. Immediately, we note that the main key of this table is the field - Pe_ID. Just on it and the bunch will go.

The second Subdivision table will store information about the departments where employees work. She, in turn, is linked via the Su_Person field to the Person table. What does it say? Based on the data schema, we can say that in the table of departments for each record from the table "Employees" there will be information about the department in which they work. It is for this connection that the Inner Join operator will work.

For a more understandable use, consider the SQL Inner Join operator (examples of its use for one and two tables). If we consider an example for one table, then everything is quite simple:

Selectfrom Person

Inner join Subdivision on Su_Person=Pe_ID

Example of connecting two tables and a subquery

sql and join statement inner join
sql and join statement inner join

The SQL Inner Join operator, examples of which can be organized in the above way to select data from several tables, works according to a slightly more complicated principle. For two tables, let's complicate the task. Let's say we have a Depart table that stores information about all the departments in each of the divisions. In this table, the number of the department and the number of the employee are recorded, and it is necessary to supplement the data selection with the name of each department. Looking ahead, it is worth saying that two methods can be used to solve this problem.

The first way is to connect the departments table to the selection. In this case, you can organize the request in this way:

Select Pe_ID, Pe_Name, Su_Id, Su_Name, Dep_ID, Dep_Name from Person

Inner join Subdivision on Su_Person=Pe_ID

Inner join Depart on Su_Depart=Dep_ID and Pe_Depart=Dep_ID

The second method of solving the problem is to use a subquery, in which not all data will be selected from the departments table, but only the necessary ones. This, unlike the first method, will reduce the query execution time.

Select Pe_ID, Pe_Name, Su_Id, Su_Name, Dep_ID, Dep_Name from Person

Inner join Subdivision on Su_Person=Pe_ID

Inner join (Select Dep_ID, Dep_Name, Pe_Depart from Depart) as T on Su_Depart=Dep_ID and Pe_Depart=Dep_ID

It is worth noting that such a construction may not always speed up the query. Sometimes there are cases when you have to use an additional data selection in a temporary table (if their volume is too large), and then combine it with the main selection.

An example of using the Inner Join operator to select from a large number of tables

Building complex queries involves using a significant number of interconnected tables and subqueries to retrieve data. The SQL Inner Join syntax can satisfy these requirements. Examples of using the operator in this case can be complicated not only by selections from many data storage locations, but also from a large number of nested subqueries. For a specific example, you can take a selection of data from system tables (Inner Join SQL statement). Example - 3 tables - in this case it will have a rather complex structure.

inner join sql example 3 tables
inner join sql example 3 tables

In this case, three more are connected (to the main table) and several data selection conditions are entered.

When using the Inner Join operator, remember that the more complex the query, the longer it will take to complete, so you should look for ways to complete and solve the task faster.

inner join sql example 3 tables
inner join sql example 3 tables

Conclusion

In the end, I would like to say one thing: working with databases is not the most difficult thing in programming, therefore, if desired, absolutely everyone can master the knowledge of building databasesdata, and over time, having gained experience, it will be possible to work with them at a professional level.

Popular topic

Editor's choice

  • Windows 7 password bypass: possible methods and recommendations from experts
    Windows 7 password bypass: possible methods and recommendations from experts

    The fact that users (most often computer administrators) protect Windows operating systems from unauthorized use, for example, in their absence at the computer, does not surprise anyone. However, often the administrator may not give the registered user enough rights to perform any actions or set personal settings, and therefore it may be necessary to log in with an administrator registration

  • How to remove a virus from a flash drive without losing data?
    How to remove a virus from a flash drive without losing data?

    Removable USB storage devices in the form of the most common flash drives are susceptible to viruses no less than hard drives with operating systems installed on them. And it is often quite problematic to identify the presence of such a threat or neutralize it. How to remove a virus from a flash drive and restore files (hidden or infected) will be discussed further

  • File viruses are computer code to achieve goals bypassing security systems
    File viruses are computer code to achieve goals bypassing security systems

    File viruses are computer code to achieve goals bypassing security systems. This is what inspired people to create antiviruses, what allows hackers to learn and steal millions of dollars every day

  • PC prevention for stable operation - expert advice. Computer help
    PC prevention for stable operation - expert advice. Computer help

    The fact that almost all modern computer systems require constant care, apparently, all users know. But not everyone has a clear enough idea of what this should manifest itself in. To keep the computer, operating system and installed applications in the most efficient condition, it is necessary to carry out preventive maintenance of the PC software and monitor the hardware components (installed equipment)

  • Detector software: what is it in the field of anti-virus protection?
    Detector software: what is it in the field of anti-virus protection?

    Apparently, many users of modern computer systems have heard or know that there are so-called detector programs. What they are is easy to understand if you just turn to the translation or interpretation of the English word detect, which literally means “to detect”