Leaf-Level Employees (Employees with No Subordinates), Correlated Subquery Syntax : Correlated subquery « Subquery « SQL Server / T-SQL

SQL Server / T-SQL
1. Aggregate Functions
2. Analytical Functions
3. Constraints
4. Cursor
5. Data Set
6. Data Type
7. Database
8. Date Timezone
9. Index
10. Insert Delete Update
11. Math Functions
12. Select Query
13. Sequence
14. Store Procedure Function
15. String Functions
16. Subquery
17. System
18. Table
19. Table Joins
20. Transact SQL
21. Transaction
22. Trigger
23. View
24. XML
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
SQL Server / T-SQL » Subquery » Correlated subquery 
Leaf-Level Employees (Employees with No Subordinates), Correlated Subquery Syntax

 

5>
6>
7>
8CREATE TABLE Employees
9(
10>   empid   int         NOT NULL,
11>   mgrid   int         NULL,
12>   empname varchar(25NOT NULL,
13>   salary money        NOT NULL)
14> GO
1INSERT INTO employees(empid, mgrid, empname, salaryVALUES(1, NULL, 'Nancy', $10000.00)
2INSERT INTO employees(empid, mgrid, empname, salaryVALUES(21'Andrew', $5000.00)
3INSERT INTO employees(empid, mgrid, empname, salaryVALUES(31'Janet', $5000.00)
4INSERT INTO employees(empid, mgrid, empname, salaryVALUES(41'Margaret', $5000.00)
5INSERT INTO employees(empid, mgrid, empname, salaryVALUES(52'Steven', $2500.00)
6INSERT INTO employees(empid, mgrid, empname, salaryVALUES(62'Michael', $2500.00)
7INSERT INTO employees(empid, mgrid, empname, salaryVALUES(73'Robert', $2500.00)
8INSERT INTO employees(empid, mgrid, empname, salaryVALUES(83'Laura', $2500.00)
9INSERT INTO employees(empid, mgrid, empname, salaryVALUES(93'Ann', $2500.00)
10INSERT INTO employees(empid, mgrid, empname, salaryVALUES(104'Ina', $2500.00)
11INSERT INTO employees(empid, mgrid, empname, salaryVALUES(117'David', $2000.00)
12INSERT INTO employees(empid, mgrid, empname, salaryVALUES(127'Ron', $2000.00)
13INSERT INTO employees(empid, mgrid, empname, salaryVALUES(137'Dan', $2000.00)
14INSERT INTO employees(empid, mgrid, empname, salaryVALUES(1411'James', $1500.00)
15>
16>
17SELECT *
18FROM
19>   Employees AS M
20WHERE
21>   NOT EXISTS
22>              (
23>              SELECT
24>                empid
25>               FROM
26>                Employees AS E
27>               WHERE
28>                 E.mgrid = M.empid
29>              )
30> GO

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(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

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

 
Related examples in the same category
1. WHERE clause in the subquery's SELECT statement links the inner query to the outer query
2. Correlated subquery using the department table in both inner and outer queries
3. Correlated subquery: the inner query depends on the outer query for any of its values
4. Correlated subquery using Distinct
5. Leaf-Level Employees (Employees with No Subordinates), Join Syntax
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.