# Doing calculation with subquery : Subquery « Subquery « SQL Server / T-SQL Tutorial

Home
SQL Server / T-SQL Tutorial
 1 Query 2 Insert Delete Update 3 Table 4 Table Join 5 Data Types 6 Set Operations 7 Constraints 8 Subquery 9 Aggregate Functions 10 Date Functions 11 Math Functions 12 String Functions 13 Data Convert Functions 14 Analytical Functions 15 Sequence Indentity 16 View 17 Index 18 Cursor 19 Database 20 Transact SQL 21 Procedure Function 22 Trigger 23 Transaction 24 XML 25 System Functions 26 System Settings 27 System Tables Views 28 User Role 29 CLR
 SQL Server / T-SQL Tutorial » Subquery » Subquery
8.1.9.Doing calculation with subquery
 ```5> CREATE TABLE employee( 6>    id          INTEGER NOT NULL PRIMARY KEY, 7>    first_name  VARCHAR(10), 8>    last_name   VARCHAR(10), 9>    salary      DECIMAL(10,2), 10>    start_Date  DATETIME, 11>    region      VARCHAR(10), 12>    city        VARCHAR(20), 13>    managerid   INTEGER 14> ); 15> 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> SELECT ID, Salary, Salary - (SELECT Avg(Salary) FROM Employee) 4>   As AvgSalaryDifference 5> FROM Employee 6> GO ID          Salary       AvgSalaryDifference ----------- ------------ ----------------------------------------           1      5890.00                               384.555556           2      4789.00                              -716.444444           3      6678.00                              1172.555556           4      5567.00                                61.555556           5      4467.00                             -1038.444444           6      6456.00                               950.555556           7      5345.00                              -160.444444           8      4234.00                             -1271.444444           9      6123.00                               617.555556 (9 rows affected) 1> 2> 3> 4> drop table employee; 5> GO```
 8.1.Subquery 8.1.1. Building a Nested Subquery 8.1.2. Using Subqueries to Check for the Existence of Matches 8.1.3. Using Subqueries to Check for the Existence of Matches between two tables 8.1.4. Subqueries are SELECT statements that are nested within another T-SQL statement. 8.1.5. Subqueries and Comparison Operators: = 8.1.6. A subquery can be used with other comparison operators: < 8.1.7. Subqueries and IN Operator 8.1.8. A query that uses three subqueries 8.1.9. Doing calculation with subquery 8.1.10. Subqueries can be nested. 8.1.11. ID NOT IN (SELECT ID FROM Title) 8.1.12. Delete with subquery 8.1.13. Join and sub query 8.1.14. Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. 8.1.15. subquery with >= 8.1.16. <> ALL with subquery