SQL Server/T-SQL/Table Joins/INNER JOIN

Материал из SQL эксперт
Перейти к: навигация, поиск

Inner join with condition on joined field

   <source lang="sql">

1> create table employee( 2> ID int, 3> name nvarchar (10), 4> salary int ) 5> GO 1> 2> create table job( 3> ID int, 4> title nvarchar (10), 5> averageSalary int) 6> GO 1> 2> 3> insert into employee (ID, name, salary) values (1, "Jason", 1234) 4> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (2, "Robert", 4321) 2> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (3, "Celia", 5432) 2> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (4, "Linda", 3456) 2> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (5, "David", 7654) 2> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (6, "James", 4567) 2> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (7, "Alison", 8744) 2> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (8, "Chris", 9875) 2> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (9, "Mary", 2345) 2> GO (1 rows affected) 1> 2> insert into job(ID, title, averageSalary) values(1,"Developer",3000) 3> GO (1 rows affected) 1> insert into job(ID, title, averageSalary) values(2,"Tester", 4000) 2> GO (1 rows affected) 1> insert into job(ID, title, averageSalary) values(3,"Designer", 5000) 2> GO (1 rows affected) 1> insert into job(ID, title, averageSalary) values(4,"Programmer", 6000) 2> GO (1 rows affected) 1> 2> 3> select * from employee; 4> GO ID name salary


---------- -----------
         1 Jason             1234
         2 Robert            4321
         3 Celia             5432
         4 Linda             3456
         5 David             7654
         6 James             4567
         7 Alison            8744
         8 Chris             9875
         9 Mary              2345

(9 rows affected) 1> select * from job; 2> GO ID title averageSalary


---------- -------------
         1 Developer           3000
         2 Tester              4000
         3 Designer            5000
         4 Programmer          6000

(4 rows affected) 1> 2> 3> SELECT p.ID, s.title, p.Name 4> FROM Employee p 5> INNER JOIN Job s 6> ON p.ID = s.ID 7> WHERE p.ID < 4 8> GO ID title Name


---------- ----------
         1 Developer  Jason
         2 Tester     Robert
         3 Designer   Celia

(3 rows affected) 1> 2> 3> 4> drop table employee; 5> drop table job; 6> GO 1> 2>

      </source>
   
  


"INNER JOIN" with different syntax

   <source lang="sql">

1> 2> 3> create table employee( 4> ID int, 5> name nvarchar (10), 6> salary int ) 7> GO 1> 2> create table job( 3> ID int, 4> title nvarchar (10), 5> averageSalary int) 6> GO 1> 2> 3> insert into employee (ID, name, salary) values (1, "Jason", 1234) 4> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (2, "Robert", 4321) 2> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (3, "Celia", 5432) 2> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (4, "Linda", 3456) 2> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (5, "David", 7654) 2> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (6, "James", 4567) 2> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (7, "Alison", 8744) 2> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (8, "Chris", 9875) 2> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (9, "Mary", 2345) 2> GO (1 rows affected) 1> 2> insert into job(ID, title, averageSalary) values(1,"Developer",3000) 3> GO (1 rows affected) 1> insert into job(ID, title, averageSalary) values(2,"Tester", 4000) 2> GO (1 rows affected) 1> insert into job(ID, title, averageSalary) values(3,"Designer", 5000) 2> GO (1 rows affected) 1> insert into job(ID, title, averageSalary) values(4,"Programmer", 6000) 2> GO (1 rows affected) 1> 2> 3> select * from employee; 4> GO ID name salary


---------- -----------
         1 Jason             1234
         2 Robert            4321
         3 Celia             5432
         4 Linda             3456
         5 David             7654
         6 James             4567
         7 Alison            8744
         8 Chris             9875
         9 Mary              2345

(9 rows affected) 1> select * from job; 2> GO ID title averageSalary


---------- -------------
         1 Developer           3000
         2 Tester              4000
         3 Designer            5000
         4 Programmer          6000

(4 rows affected) 1> 2> 3> -- INNER JOINs 4> 5> SELECT * FROM Employee 6> INNER JOIN job 7> ON Employee.ID = Job.ID 8> 9> GO ID name salary ID title averageSalary


---------- ----------- ----------- ---------- -------------
         1 Jason             1234           1 Developer           3000
         2 Robert            4321           2 Tester              4000
         3 Celia             5432           3 Designer            5000
         4 Linda             3456           4 Programmer          6000

(4 rows affected) 1> 2> 3> 4> drop table employee; 5> drop table job; 6> GO 1>

      </source>
   
  


Using the * operator to reference entire row

   <source lang="sql">

1> 2> create table employee( 3> ID int, 4> name nvarchar (10), 5> salary int ) 6> GO 1> 2> create table job( 3> ID int, 4> title nvarchar (10), 5> averageSalary int) 6> GO 1> 2> 3> insert into employee (ID, name, salary) values (1, "Jason", 1234) 4> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (2, "Robert", 4321) 2> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (3, "Celia", 5432) 2> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (4, "Linda", 3456) 2> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (5, "David", 7654) 2> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (6, "James", 4567) 2> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (7, "Alison", 8744) 2> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (8, "Chris", 9875) 2> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (9, "Mary", 2345) 2> GO (1 rows affected) 1> 2> insert into job(ID, title, averageSalary) values(1,"Developer",3000) 3> GO (1 rows affected) 1> insert into job(ID, title, averageSalary) values(2,"Tester", 4000) 2> GO (1 rows affected) 1> insert into job(ID, title, averageSalary) values(3,"Designer", 5000) 2> GO (1 rows affected) 1> insert into job(ID, title, averageSalary) values(4,"Programmer", 6000) 2> GO (1 rows affected) 1> 2> 3> select * from employee; 4> GO ID name salary


---------- -----------
         1 Jason             1234
         2 Robert            4321
         3 Celia             5432
         4 Linda             3456
         5 David             7654
         6 James             4567
         7 Alison            8744
         8 Chris             9875
         9 Mary              2345

(9 rows affected) 1> select * from job; 2> GO ID title averageSalary


---------- -------------
         1 Developer           3000
         2 Tester              4000
         3 Designer            5000
         4 Programmer          6000

(4 rows affected) 1> 2> -- Using the * operator 3> 4> 5> SELECT Employee.* FROM Employee 6> INNER JOIN Job 7> ON Employee.ID = Job.ID 8> GO ID name salary


---------- -----------
         1 Jason             1234
         2 Robert            4321
         3 Celia             5432
         4 Linda             3456

(4 rows affected) 1> 2> 3> drop table employee; 4> drop table job; 5> GO 1>

      </source>
   
  


Working with "INNER JOIN"

   <source lang="sql">

1> create table employee( 2> ID int, 3> name nvarchar (10), 4> salary int ) 5> GO 1> 2> create table job( 3> ID int, 4> title nvarchar (10), 5> averageSalary int) 6> GO 1> 2> 3> insert into employee (ID, name, salary) values (1, "Jason", 1234) 4> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (2, "Robert", 4321) 2> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (3, "Celia", 5432) 2> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (4, "Linda", 3456) 2> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (5, "David", 7654) 2> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (6, "James", 4567) 2> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (7, "Alison", 8744) 2> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (8, "Chris", 9875) 2> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (9, "Mary", 2345) 2> GO (1 rows affected) 1> 2> 3> insert into job(ID, title, averageSalary) values(1,"Developer",3000) 4> GO (1 rows affected) 1> insert into job(ID, title, averageSalary) values(2,"Tester", 4000) 2> GO (1 rows affected) 1> insert into job(ID, title, averageSalary) values(3,"Designer", 5000) 2> GO (1 rows affected) 1> insert into job(ID, title, averageSalary) values(4,"Programmer", 6000) 2> GO (1 rows affected) 1> 2> select * from employee; 3> GO ID name salary


---------- -----------
         1 Jason             1234
         2 Robert            4321
         3 Celia             5432
         4 Linda             3456
         5 David             7654
         6 James             4567
         7 Alison            8744
         8 Chris             9875
         9 Mary              2345

(9 rows affected) 1> select * from job; 2> GO ID title averageSalary


---------- -------------
         1 Developer           3000
         2 Tester              4000
         3 Designer            5000
         4 Programmer          6000

(4 rows affected) 1> 2> 3> SELECT e.ID, e.Name, j.title 4> FROM Employee e INNER JOIN Job j 5> ON e.Id = j.ID 6> GO ID Name title


---------- ----------
         1 Jason      Developer
         2 Robert     Tester
         3 Celia      Designer
         4 Linda      Programmer

(4 rows affected) 1> 2> 3> 4> drop table employee; 5> drop table job; 6> GO 1>

      </source>