SQL Server/T-SQL/Subquery/Nested Subquery

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

Subqueries can be nested

   <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> 3> -- Subqueries can be nested. 4> 5> SELECT ID, Name 6> FROM Employee 7> WHERE ID IN 8> ( SELECT ID 9> FROM Job 10> WHERE title IN 11> ( SELECT title 12> FROM job ) 13> ) 14> GO ID Name


----------
         1 Jason
         2 Robert
         3 Celia
         4 Linda

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

      </source>
   
  


Using a Nested Subquery

   <source lang="sql">

4> CREATE TABLE employee( 5> id INTEGER NOT NULL PRIMARY KEY, 6> first_name VARCHAR(10), 7> last_name VARCHAR(10), 8> salary DECIMAL(10,2), 9> start_Date DATETIME, 10> region VARCHAR(10), 11> city VARCHAR(20), 12> managerid INTEGER 13> ); 14> GO 1> INSERT INTO employee VALUES (1, "Jason" , "Martin", 5890,"2005-03-22","North","Vancouver",3); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (2, "Alison", "Mathews",4789,"2003-07-21","South","Utown",4); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (3, "James" , "Smith", 6678,"2001-12-01","North","Paris",5); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (4, "Celia" , "Rice", 5567,"2006-03-03","South","London",6); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (5, "Robert", "Black", 4467,"2004-07-02","East","Newton",7); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (6, "Linda" , "Green" , 6456,"2002-05-19","East","Calgary",8); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (7, "David" , "Larry", 5345,"2008-03-18","West","New York",9); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (8, "James" , "Cat", 4234,"2007-07-17","West","Regina",9); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (9, "Joan" , "Act", 6123,"2001-04-16","North","Toronto",10); 2> GO (1 rows affected) 1> 2> select * from employee; 3> GO id first_name last_name salary start_Date region city managerid


---------- ---------- ------------ ----------------------- ---------- -------------------- -----------
         1 Jason      Martin          5890.00 2005-03-22 00:00:00.000 North      Vancouver                      3
         2 Alison     Mathews         4789.00 2003-07-21 00:00:00.000 South      Utown                          4
         3 James      Smith           6678.00 2001-12-01 00:00:00.000 North      Paris                          5
         4 Celia      Rice            5567.00 2006-03-03 00:00:00.000 South      London                         6
         5 Robert     Black           4467.00 2004-07-02 00:00:00.000 East       Newton                         7
         6 Linda      Green           6456.00 2002-05-19 00:00:00.000 East       Calgary                        8
         7 David      Larry           5345.00 2008-03-18 00:00:00.000 West       New York                       9
         8 James      Cat             4234.00 2007-07-17 00:00:00.000 West       Regina                         9
         9 Joan       Act             6123.00 2001-04-16 00:00:00.000 North      Toronto                       10

(9 rows affected) 1> 2> 3> 4> 5> SELECT 6> MIN (Start_Date), 7> (SELECT MAX (Start_Date) FROM Employee) 8> FROM 9> Employee 10> 11> 12> 13> 14> drop table employee; 15> GO


-----------------------

2001-04-16 00:00:00.000 2008-03-18 00:00:00.000 (1 rows affected)

</source>