SQL Server/T-SQL/Subquery/Correlated subquery — различия между версиями

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

Текущая версия на 10:21, 26 мая 2010

Correlated subquery: the inner query depends on the outer query for any of its values

1> CREATE TABLE employee  (emp_no    INTEGER NOT NULL,
2>                         emp_fname CHAR(20) NOT NULL,
3>                         emp_lname CHAR(20) NOT NULL,
4>                         dept_no   CHAR(4) NULL)
5>
6> insert into employee values(1,  "Matthew", "Smith",    "d3")
7> insert into employee values(2,  "Ann",     "Jones",    "d3")
8> insert into employee values(3,  "John",    "Barrimore","d1")
9> insert into employee values(4,  "James",   "James",    "d2")
10> insert into employee values(5,  "Elsa",    "Bertoni",  "d2")
11> insert into employee values(6,  "Elke",    "Hansel",   "d2")
12> insert into employee values(7,  "Sybill",  "Moser",    "d1")
13>
14> select * from employee
15> GO
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
emp_no      emp_fname            emp_lname            dept_no
----------- -------------------- -------------------- -------
          1 Matthew              Smith                d3
          2 Ann                  Jones                d3
          3 John                 Barrimore            d1
          4 James                James                d2
          5 Elsa                 Bertoni              d2
          6 Elke                 Hansel               d2
          7 Sybill               Moser                d1
(7 rows affected)
1>
2> CREATE TABLE works_on        (emp_no       INTEGER NOT NULL,
3>                         project_no    CHAR(4) NOT NULL,
4>                         job CHAR (15) NULL,
5>                         enter_date    DATETIME NULL)
6>
7> insert into works_on values (1, "p1", "analyst", "1997.10.1")
8> insert into works_on values (1, "p3", "manager", "1999.1.1")
9> insert into works_on values (2, "p2", "clerk",   "1998.2.15")
10> insert into works_on values (2, "p2",  NULL,     "1998.6.1")
11> insert into works_on values (3, "p2",  NULL,     "1997.12.15")
12> insert into works_on values (4, "p3", "analyst", "1998.10.15")
13> insert into works_on values (5, "p1", "manager", "1998.4.15")
14> insert into works_on values (6, "p1",  NULL,     "1998.8.1")
15> insert into works_on values (7, "p2", "clerk",   "1999.2.1")
16> insert into works_on values (8, "p3", "clerk",   "1997.11.15")
17> insert into works_on values (7, "p1", "clerk",   "1998.1.4")
18>
19> select * from works_on
20> GO
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
emp_no      project_no job             enter_date
----------- ---------- --------------- -----------------------
          1 p1         analyst         1997-10-01 00:00:00.000
          1 p3         manager         1999-01-01 00:00:00.000
          2 p2         clerk           1998-02-15 00:00:00.000
          2 p2         NULL            1998-06-01 00:00:00.000
          3 p2         NULL            1997-12-15 00:00:00.000
          4 p3         analyst         1998-10-15 00:00:00.000
          5 p1         manager         1998-04-15 00:00:00.000
          6 p1         NULL            1998-08-01 00:00:00.000
          7 p2         clerk           1999-02-01 00:00:00.000
          8 p3         clerk           1997-11-15 00:00:00.000
          7 p1         clerk           1998-01-04 00:00:00.000
(11 rows affected)
1>
2>
3> -- Correlated Subqueries
4>
5> -- A subquery is said to be a correlated subquery if the inner query depends on the outer query for any of its values.
6>
7> SELECT emp_lname
8>        FROM employee
9>        WHERE emp_no IN
10>        (SELECT emp_no
11>          FROM works_on
12>          WHERE project_no="p3")
13> GO
emp_lname
--------------------
Smith
James
(2 rows affected)
1>
2> drop table works_on
3> drop table employee
4> GO
1>



Correlated subquery using Distinct

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> -- Correlated subquery using Distinct:
4> SELECT ID
5>     , Name
6> FROM Employee AS e
7> WHERE 1 =
8>     (SELECT DISTINCT ID
9>      FROM Job As j
10>      WHERE j.ID = e.ID)
11>
12>
13>
14> drop table employee;
15> drop table job;
16> GO
ID          Name
----------- ----------
          1 Jason
(1 rows affected)
1>
2>



Correlated subquery using the department table in both inner and outer queries

1>
2> CREATE TABLE department(dept_no   CHAR(4) NOT NULL,
3>                         dept_name CHAR(25) NOT NULL,
4>                         location  CHAR(30) NULL)
5>
6> insert into department values ("d1", "developer",   "Dallas")
7> insert into department values ("d2", "tester",      "Seattle")
8> insert into department values ("d3", "marketing",  "Dallas")
9>
10> select * from department
11> GO
(1 rows affected)
(1 rows affected)
(1 rows affected)
dept_no dept_name                 location
------- ------------------------- ------------------------------
d1      developer                 Dallas
d2      tester                    Seattle
d3      marketing                 Dallas
(3 rows affected)
1> -- Correlated subquery using the department table in both inner and outer queries
2>
3> SELECT t1.*
4>        FROM department t1
5>        WHERE t1.location IN
6>        (SELECT t2.location
7>          FROM department t2
8>          WHERE t1.dept_no <> t2.dept_no)
9> GO
dept_no dept_name                 location
------- ------------------------- ------------------------------
d1      developer                 Dallas
d3      marketing                 Dallas
(2 rows affected)
1>
2> drop table department
3> GO
1>



Leaf-Level Employees (Employees with No Subordinates), Correlated Subquery Syntax

 
5>
6>
7>
8> CREATE TABLE Employees
9> (
10>   empid   int         NOT NULL,
11>   mgrid   int         NULL,
12>   empname varchar(25) NOT NULL,
13>   salary money        NOT NULL)
14> GO
1> INSERT INTO employees(empid, mgrid, empname, salary) VALUES(1, NULL, "Nancy", $10000.00)
2> INSERT INTO employees(empid, mgrid, empname, salary) VALUES(2, 1, "Andrew", $5000.00)
3> INSERT INTO employees(empid, mgrid, empname, salary) VALUES(3, 1, "Janet", $5000.00)
4> INSERT INTO employees(empid, mgrid, empname, salary) VALUES(4, 1, "Margaret", $5000.00)
5> INSERT INTO employees(empid, mgrid, empname, salary) VALUES(5, 2, "Steven", $2500.00)
6> INSERT INTO employees(empid, mgrid, empname, salary) VALUES(6, 2, "Michael", $2500.00)
7> INSERT INTO employees(empid, mgrid, empname, salary) VALUES(7, 3, "Robert", $2500.00)
8> INSERT INTO employees(empid, mgrid, empname, salary) VALUES(8, 3, "Laura", $2500.00)
9> INSERT INTO employees(empid, mgrid, empname, salary) VALUES(9, 3, "Ann", $2500.00)
10> INSERT INTO employees(empid, mgrid, empname, salary) VALUES(10, 4, "Ina", $2500.00)
11> INSERT INTO employees(empid, mgrid, empname, salary) VALUES(11, 7, "David", $2000.00)
12> INSERT INTO employees(empid, mgrid, empname, salary) VALUES(12, 7, "Ron", $2000.00)
13> INSERT INTO employees(empid, mgrid, empname, salary) VALUES(13, 7, "Dan", $2000.00)
14> INSERT INTO employees(empid, mgrid, empname, salary) VALUES(14, 11, "James", $1500.00)
15>
16>
17> SELECT *
18> FROM
19>   Employees AS M
20> WHERE
21>   NOT EXISTS
22>              (
23>              SELECT
24>                empid
25>               FROM
26>                Employees AS E
27>               WHERE
28>                 E.mgrid = M.empid
29>              )
30> GO
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
empid       mgrid       empname                   salary
----------- ----------- ------------------------- ---------------------
          5           2 Steven                                2500.0000
          6           2 Michael                               2500.0000
          8           3 Laura                                 2500.0000
          9           3 Ann                                   2500.0000
         10           4 Ina                                   2500.0000
         12           7 Ron                                   2000.0000
         13           7 Dan                                   2000.0000
         14          11 James                                 1500.0000
(8 rows affected)
1>
2>
3> drop table Employees;
4> GO



Leaf-Level Employees (Employees with No Subordinates), Join Syntax

 
3>
4>
5>
6> CREATE TABLE Employees
7> (
8>   empid   int         NOT NULL,
9>   mgrid   int         NULL,
10>   empname varchar(25) NOT NULL,
11>   salary money        NOT NULL)
12> GO
1> INSERT INTO employees(empid, mgrid, empname, salary) VALUES(1, NULL, "Nancy", $10000.00)
2> INSERT INTO employees(empid, mgrid, empname, salary) VALUES(2, 1, "Andrew", $5000.00)
3> INSERT INTO employees(empid, mgrid, empname, salary) VALUES(3, 1, "Janet", $5000.00)
4> INSERT INTO employees(empid, mgrid, empname, salary) VALUES(4, 1, "Margaret", $5000.00)
5> INSERT INTO employees(empid, mgrid, empname, salary) VALUES(5, 2, "Steven", $2500.00)
6> INSERT INTO employees(empid, mgrid, empname, salary) VALUES(6, 2, "Michael", $2500.00)
7> INSERT INTO employees(empid, mgrid, empname, salary) VALUES(7, 3, "Robert", $2500.00)
8> INSERT INTO employees(empid, mgrid, empname, salary) VALUES(8, 3, "Laura", $2500.00)
9> INSERT INTO employees(empid, mgrid, empname, salary) VALUES(9, 3, "Ann", $2500.00)
10> INSERT INTO employees(empid, mgrid, empname, salary) VALUES(10, 4, "Ina", $2500.00)
11> INSERT INTO employees(empid, mgrid, empname, salary) VALUES(11, 7, "David", $2000.00)
12> INSERT INTO employees(empid, mgrid, empname, salary) VALUES(12, 7, "Ron", $2000.00)
13> INSERT INTO employees(empid, mgrid, empname, salary) VALUES(13, 7, "Dan", $2000.00)
14> INSERT INTO employees(empid, mgrid, empname, salary) VALUES(14, 11, "James", $1500.00)
15>
16>
17> SELECT
18>   M.*
19> FROM
20>     Employees AS M
21>   LEFT OUTER JOIN
22>     Employees AS E ON M.empid = E.mgrid
23> WHERE
24>   E.mgrid IS NULL
25> GO
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
empid       mgrid       empname                   salary
----------- ----------- ------------------------- ---------------------
          5           2 Steven                                2500.0000
          6           2 Michael                               2500.0000
          8           3 Laura                                 2500.0000
          9           3 Ann                                   2500.0000
         10           4 Ina                                   2500.0000
         12           7 Ron                                   2000.0000
         13           7 Dan                                   2000.0000
         14          11 James                                 1500.0000
(8 rows affected)
1>
2>
3> drop table Employees;
4> GO
1>



WHERE clause in the subquery"s SELECT statement links the inner query to the outer query

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> -- The WHERE clause in the subquery"s SELECT statement links the inner query to the outer query.
4> -- This makes the inner query a correlated subquery
5>
6> SELECT TOP 29 e.ID, e.name, (SELECT j.title FROM job j WHERE j.ID = e.ID) "title"
7> FROM Employee e
8> GO
ID          name       title
----------- ---------- ----------
          1 Jason      Developer
          2 Robert     Tester
          3 Celia      Designer
          4 Linda      Programmer
          5 David      NULL
          6 James      NULL
          7 Alison     NULL
          8 Chris      NULL
          9 Mary       NULL
(9 rows affected)
1>
2> drop table employee;
3> drop table job;
4> GO
1>