YouTip LogoYouTip

Sql Join Inner

INNER JOIN is one of the most commonly used join methods in SQL, used to extract matching records from multiple tables based on their relationships. The INNER JOIN keyword returns rows when there is at least one match in the tables. It returns the intersection of two tables that satisfy the join condition, i.e., data that exists simultaneously in both tables. ### SQL INNER JOIN Syntax SELECT column_name(s) FROM table1 INNER JOIN table2 ON table1.column_name=table2.column_name; or: SELECT column_name(s) FROM table1 JOIN table2 ON table1.column_name=table2.column_name; **Parameter Description:** * columns: The names of the columns to be displayed. * table1: The name of Table 1. * table2: The name of Table 2. * column_name: The column name used for joining in the tables. **Note:** INNER JOIN is the same as JOIN. ![Image 1: SQL INNER JOIN]( Suppose we have two tables: Students and Enrollments. **Students Table**: | StudentID | Name | Age | | --- | --- | --- | | 1 | Alice | 22 | | 2 | Bob | 23 | | 3 | Charlie | 24 | **Enrollments Table**: | EnrollmentID | StudentID | Course | | --- | --- | --- | | 101 | 1 | Math | | 102 | 2 | Science | | 103 | 4 | History | #### Query Using INNER JOIN SELECT Students.Name, Enrollments.Course FROM Students INNER JOIN Enrollments ON Students.StudentID = Enrollments.StudentID; The result is as follows: | Name | Course | | --- | --- | | Alice | Math | | Bob | Science | **Explanation**: `INNER JOIN` returns data where the `StudentID` matches between the `Students` and `Enrollments` tables. Records without a match (such as Charlie and the data with EnrollmentID 103) are excluded. * * * ## Demo Database In this tutorial, we will use the sample database. Below is the data selected from the "Websites" table: +----+--------------+---------------------------+-------+---------+ | id | name | url | alexa | country | +----+--------------+---------------------------+-------+---------+ | 1 | Google | https://www.google.cm/ | 1 | USA | | 2 | Taobao | https://www.taobao.com/ | 13 | CN | | 3 | | | 4689 | CN | | 4 | Weibo | http://weibo.com/ | 20 | CN | | 5 | Facebook | https://www.facebook.com/ | 3 | USA | | 7 | Stack Overflow | http://stackoverflow.com/ | 0 | IND | +----+---------------+---------------------------+-------+---------+ Below is the data from the "access_log" website access record table: mysql> SELECT * FROM access_log; +-----+---------+-------+------------+ | aid | site_id | count | date | +-----+---------+-------+------------+ | 1 | 1 | 45 | 2016-05-10 | | 2 | 3 | 100 | 2016-05-13 | | 3 | 1 | 230 | 2016-05-14 | | 4 | 2 | 10 | 2016-05-14 | | 5 | 5 | 205 | 2016-05-14 | | 6 | 4 | 13 | 2016-05-15 | | 7 | 3 | 220 | 2016-05-15 | | 8 | 5 | 545 | 2016-05-16 | | 9 | 3 | 201 | 2016-05-17 | +-----+---------+-------+------------+ 9 rows in set (0.00 sec) * * * ## SQL INNER JOIN Example The following SQL statement will return the access records for all websites: ## Example SELECT Websites.name, access_log.count, access_log.date FROM Websites INNER JOIN access_log ON Websites.id=access_log.site_id ORDER BY access_log.count; Executing the above SQL produces the following output: !( **Note:** The INNER JOIN keyword returns rows when there is at least one match in the tables. If a row in the "Websites" table does not have a match in the "access_log," that row will not be listed.
← Sql Join LeftSql Join β†’