SQL Server/T-SQL/Table Joins/Table Join — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 10:19, 26 мая 2010
Содержание
- 1 Column alias in table join
- 2 Joining a Table with Itself
- 3 Joins are advantageous over subqueries if the SELECT list in a query contains columns from more than one table
- 4 NOT EXISTS function with table join
- 5 Select columns from all tables
- 6 SET SHOWPLAN_TEXT ON for a table join
- 7 SQL Server join syntax
- 8 Table join with data returned from function
- 9 Use Count function in a left join
- 10 Using TOP in table join
Column alias in table join
1> create table employee(
2> ID int,
3> name nvarchar (10),
4> salary int,
5> start_date datetime,
6> city nvarchar (10),
7> region char (1))
8> GO
1>
2> insert into employee (ID, name, salary, start_date, city, region)
3> values (1, "Jason", 40420, "02/01/94", "New York", "W")
4> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (2, "Robert",14420, "01/02/95", "Vancouver","N")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (3, "Celia", 24020, "12/03/96", "Toronto", "W")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (4, "Linda", 40620, "11/04/97", "New York", "N")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (5, "David", 80026, "10/05/98", "Vancouver","W")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (6, "James", 70060, "09/06/99", "Toronto", "N")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (7, "Alison",90620, "08/07/00", "New York", "W")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (8, "Chris", 26020, "07/08/01", "Vancouver","N")
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (9, "Mary", 60020, "06/09/02", "Toronto", "W")
3> GO
(1 rows affected)
1>
2> select * from employee
3> GO
ID name salary start_date city region
----------- ---------- ----------- ----------------------- ---------- ------
1 Jason 40420 1994-02-01 00:00:00.000 New York W
2 Robert 14420 1995-01-02 00:00:00.000 Vancouver N
3 Celia 24020 1996-12-03 00:00:00.000 Toronto W
4 Linda 40620 1997-11-04 00:00:00.000 New York N
5 David 80026 1998-10-05 00:00:00.000 Vancouver W
6 James 70060 1999-09-06 00:00:00.000 Toronto N
7 Alison 90620 2000-08-07 00:00:00.000 New York W
8 Chris 26020 2001-07-08 00:00:00.000 Vancouver N
9 Mary 60020 2002-06-09 00:00:00.000 Toronto W
(9 rows affected)
1>
2> SELECT c.name "Column name", t.name "Data type name",
3> c.max_length, c.precision, c.scale,
4> c.is_nullable "nullable", c.is_identity "identity"
5> FROM sys.columns c
6> JOIN sys.types t
7> ON c.user_type_id = t.user_type_id
8> WHERE object_id = OBJECT_ID("dbo.Employee")
9> GO
Column name Data type name
max_length precision scale nullable identity
-------------------------------------------------------------------------------------------------------------------------------- ------------------
----------------------------------------------------------------------------- ---------- --------- ----- -------- --------
ID int
4 10 0 1 0
name nvarchar
20 0 0 1 0
salary int
4 10 0 1 0
start_date datetime
8 23 3 1 0
city nvarchar
20 0 0 1 0
region char
1 0 0 1 0
(6 rows affected)
1>
2>
3> drop table employee
4> GO
1>
Joining a Table with Itself
14>
15> CREATE TABLE department(dept_no CHAR(4) NOT NULL,
16> dept_name CHAR(25) NOT NULL,
17> location CHAR(30) NULL)
18>
19> insert into department values ("d1", "developer", "Dallas")
20> insert into department values ("d2", "tester", "Seattle")
21> insert into department values ("d3", "marketing", "Dallas")
22>
23> select * from department
24> 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> -- Joining a Table with Itself
2>
3> -- ANSI join syntax:
4>
5> SELECT t1.dept_no, t1.dept_name, t1.location
6> FROM department t1 JOIN department t2
7> ON t1.location=t2.location
8> WHERE t1.dept_no <> t2.dept_no
9> GO
dept_no dept_name location
------- ------------------------- ------------------------------
d3 marketing Dallas
d1 developer Dallas
(2 rows affected)
1> -- SQL Server join syntax:
2>
3> SELECT DISTINCT t1.dept_no, t1.dept_name, t1.location
4> FROM department t1, department t2
5> WHERE t1. location = t2.location
6> AND t1.dept_no <> t2.dept_no
7> GO
dept_no dept_name location
------- ------------------------- ------------------------------
d1 developer Dallas
d3 marketing Dallas
(2 rows affected)
1>
2> drop table department
3> GO
1>
2>
Joins are advantageous over subqueries if the SELECT list in a query contains columns from more than one table
33>
34> CREATE TABLE works_on (emp_no INTEGER NOT NULL,
35> project_no CHAR(4) NOT NULL,
36> job CHAR (15) NULL,
37> enter_date DATETIME NULL)
38>
39> insert into works_on values (1, "p1", "analyst", "1997.10.1")
40> insert into works_on values (1, "p3", "manager", "1999.1.1")
41> insert into works_on values (2, "p2", "clerk", "1998.2.15")
42> insert into works_on values (2, "p2", NULL, "1998.6.1")
43> insert into works_on values (3, "p2", NULL, "1997.12.15")
44> insert into works_on values (4, "p3", "analyst", "1998.10.15")
45> insert into works_on values (5, "p1", "manager", "1998.4.15")
46> insert into works_on values (6, "p1", NULL, "1998.8.1")
47> insert into works_on values (7, "p2", "clerk", "1999.2.1")
48> insert into works_on values (8, "p3", "clerk", "1997.11.15")
49> insert into works_on values (7, "p1", "clerk", "1998.1.4")
50>
51> select * from works_on
52> 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> CREATE TABLE employee (emp_no INTEGER NOT NULL,
3> emp_fname CHAR(20) NOT NULL,
4> emp_lname CHAR(20) NOT NULL,
5> dept_no CHAR(4) NULL)
6>
7> insert into employee values(1, "Matthew", "Smith", "d3")
8> insert into employee values(2, "Ann", "Jones", "d3")
9> insert into employee values(3, "John", "Barrimore","d1")
10> insert into employee values(4, "James", "James", "d2")
11> insert into employee values(5, "Elsa", "Bertoni", "d2")
12> insert into employee values(6, "Elke", "Hansel", "d2")
13> insert into employee values(7, "Sybill", "Moser", "d1")
14>
15> select * from employee
16> 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> -- Join Advantages
3>
4> -- Joins are advantageous over subqueries if the SELECT list in a query contains columns from more than one table
5>
6> SELECT employee.emp_no, emp_lname, job
7> FROM employee, works_on
8> WHERE employee.emp_no = works_on.emp_no
9> AND enter_date = "10.15.1998"
10> GO
emp_no emp_lname job
----------- -------------------- ---------------
4 James analyst
(1 rows affected)
1>
2> drop table employee
3> drop table works_on
4> GO
1>
2>
NOT EXISTS function with table join
1>
2> CREATE TABLE employee (emp_no INTEGER NOT NULL,
3> emp_fname CHAR(20) NOT NULL,
4> emp_lname CHAR(20) NOT NULL,
5> dept_no CHAR(4) NULL)
6>
7> insert into employee values(1, "Matthew", "Smith", "d3")
8> insert into employee values(2, "Ann", "Jones", "d3")
9> insert into employee values(3, "John", "Barrimore","d1")
10> insert into employee values(4, "James", "James", "d2")
11> insert into employee values(5, "Elsa", "Bertoni", "d2")
12> insert into employee values(6, "Elke", "Hansel", "d2")
13> insert into employee values(7, "Sybill", "Moser", "d1")
14>
15> select * from employee
16> 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>
3> CREATE TABLE department(dept_no CHAR(4) NOT NULL,
4> dept_name CHAR(25) NOT NULL,
5> location CHAR(30) NULL)
6>
7> insert into department values ("d1", "developer", "Dallas")
8> insert into department values ("d2", "tester", "Seattle")
9> insert into department values ("d3", "marketing", "Dallas")
10>
11> select * from department
12> 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>
2> -- NOT EXISTS function.
3>
4> SELECT emp_lname FROM employee WHERE NOT EXISTS
5> (SELECT * FROM department WHERE employee.dept_no = department.dept_no
6> AND location = "Seattle")
7>
8> drop table department
9> drop table employee
10> GO
emp_lname
--------------------
Smith
Jones
Barrimore
Moser
(4 rows affected)
1>
2>
Select columns from all tables
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> -- SQL Server syntax:
4>
5> SELECT * FROM works_on, project
6> WHERE works_on.project_no = "p3"
7> GO
emp_no project_no job enter_date project_no project_name budget
----------- ---------- --------------- ----------------------- ---------- --------------- ------------------------
1 p3 manager 1999-01-01 00:00:00.000 p1 Search Engine 120000
1 p3 manager 1999-01-01 00:00:00.000 p3 SQL 186500
4 p3 analyst 1998-10-15 00:00:00.000 p1 Search Engine 120000
4 p3 analyst 1998-10-15 00:00:00.000 p3 SQL 186500
8 p3 clerk 1997-11-15 00:00:00.000 p1 Search Engine 120000
8 p3 clerk 1997-11-15 00:00:00.000 p3 SQL 186500
(6 rows affected)
1>
2> drop table works_on
3> drop table project
4> GO
1>
SET SHOWPLAN_TEXT ON for a table join
20>
21> CREATE TABLE works_on (emp_no INTEGER NOT NULL,
22> project_no CHAR(4) NOT NULL,
23> job CHAR (15) NULL,
24> enter_date DATETIME NULL)
25>
26> insert into works_on values (1, "p1", "analyst", "1997.10.1")
27> insert into works_on values (1, "p3", "manager", "1999.1.1")
28> insert into works_on values (2, "p2", "clerk", "1998.2.15")
29> insert into works_on values (2, "p2", NULL, "1998.6.1")
30> insert into works_on values (3, "p2", NULL, "1997.12.15")
31> insert into works_on values (4, "p3", "analyst", "1998.10.15")
32> insert into works_on values (5, "p1", "manager", "1998.4.15")
33> insert into works_on values (6, "p1", NULL, "1998.8.1")
34> insert into works_on values (7, "p2", "clerk", "1999.2.1")
35> insert into works_on values (8, "p3", "clerk", "1997.11.15")
36> insert into works_on values (7, "p1", "clerk", "1998.1.4")
37>
38> select * from works_on
39> 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> 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>
3>
4> SET SHOWPLAN_TEXT ON
5> GO
1>
2> SELECT employee.dept_no
3> FROM employee, works_on
4> WHERE employee.emp_no = works_on.emp_no
5> AND works_on.project_no = "p1"
6>
7>
8> drop table works_on
9> drop table employee
10> GO
StmtText
----------------------------------------------------------------------------------------------------------------------
SELECT employee.dept_no
FROM employee, works_on
WHERE employee.emp_no = works_on.emp_no
AND works_on.project_no = "p1"
(1 rows affected)
StmtText
--------------------------------------------------------------------------------------------------------------
|--Hash Match(Inner Join, HASH:([master].[dbo].[works_on].[emp_no])=([master].[dbo].[employee].[emp_no]))
|--Table Scan(OBJECT:([master].[dbo].[works_on]), WHERE:([master].[dbo].[works_on].[project_no]="p1"))
|--Table Scan(OBJECT:([master].[dbo].[employee]))
(3 rows affected)
StmtText
-----------------------
drop table works_on
drop table employee
(2 rows affected)
1>
SQL Server join syntax
26> CREATE TABLE employee (emp_no INTEGER NOT NULL,
27> emp_fname CHAR(20) NOT NULL,
28> emp_lname CHAR(20) NOT NULL,
29> dept_no CHAR(4) NULL)
30>
31> insert into employee values(1, "Matthew", "Smith", "d3")
32> insert into employee values(2, "Ann", "Jones", "d3")
33> insert into employee values(3, "John", "Barrimore","d1")
34> insert into employee values(4, "James", "James", "d2")
35> insert into employee values(5, "Elsa", "Bertoni", "d2")
36> insert into employee values(6, "Elke", "Hansel", "d2")
37> insert into employee values(7, "Sybill", "Moser", "d1")
38>
39> select * from employee
40> 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>
3> CREATE TABLE department(dept_no CHAR(4) NOT NULL,
4> dept_name CHAR(25) NOT NULL,
5> location CHAR(30) NULL)
6>
7> insert into department values ("d1", "developer", "Dallas")
8> insert into department values ("d2", "tester", "Seattle")
9> insert into department values ("d3", "marketing", "Dallas")
10>
11> select * from department
12> 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>
2> CREATE TABLE project (project_no CHAR(4) NOT NULL,
3> project_name CHAR(15) NOT NULL,
4> budget FLOAT NULL)
5>
6> insert into project values ("p1", "Search Engine", 120000.00)
7> insert into project values ("p2", "Programming", 95000.00)
8> insert into project values ("p3", "SQL", 186500.00)
9>
10> select * from project
11> GO
(1 rows affected)
(1 rows affected)
(1 rows affected)
project_no project_name budget
---------- --------------- ------------------------
p1 Search Engine 120000
p2 Programming 95000
p3 SQL 186500
(3 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> -- ANSI join syntax:
2>
3> SELECT DISTINCT project_name
4> FROM project JOIN works_on
5> ON project.project_no = works_on.project_no
6> JOIN employee ON works_on.emp_no = employee.emp_no
7> JOIN department ON employee.dept_no = department.dept_no
8> WHERE dept_name = "marketing"
9> GO
project_name
---------------
Programming
Search Engine
SQL
(3 rows affected)
1> -- SQL Server join syntax:
2>
3> SELECT DISTINCT project_name
4> FROM project, works_on, employee, department
5> WHERE project.project_no = works_on.project_no
6> AND works_on.emp_no = employee.emp_no
7> AND employee.dept_no = department.dept_no
8> AND dept_name = "marketing"
9> GO
project_name
---------------
Programming
Search Engine
SQL
(3 rows affected)
1>
2> drop table project
3> drop table works_on
4> drop table employee
5> drop table department
6> GO
1>
Table join with data returned from function
28> create table employee(
29> ID int,
30> name nvarchar (10),
31> salary int )
32> 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> insert into employee (ID, name, salary) values (2, "Robert", 4321)
2> GO
1> insert into employee (ID, name, salary) values (3, "Celia", 5432)
2> GO
1> insert into employee (ID, name, salary) values (4, "Linda", 3456)
2> GO
1> insert into employee (ID, name, salary) values (5, "David", 7654)
2> GO
1> insert into employee (ID, name, salary) values (6, "James", 4567)
2> GO
1> insert into employee (ID, name, salary) values (7, "Alison", 8744)
2> GO
1> insert into employee (ID, name, salary) values (8, "Chris", 9875)
2> GO
1> insert into employee (ID, name, salary) values (9, "Mary", 2345)
2> GO
1>
2> insert into job(ID, title, averageSalary) values(1,"Developer",3000)
3> GO
1> insert into job(ID, title, averageSalary) values(2,"Tester", 4000)
2> GO
1> insert into job(ID, title, averageSalary) values(3,"Designer", 5000)
2> GO
1> insert into job(ID, title, averageSalary) values(4,"Programmer", 6000)
2> GO
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
1> select * from job;
2> GO
ID title averageSalary
----------- ---------- -------------
1 Developer 3000
2 Tester 4000
3 Designer 5000
4 Programmer 6000
1>
2> CREATE FUNCTION dbo.fnSalaryCount(@SalaryQty bigint)
3> RETURNS TABLE
4> AS
5> RETURN (SELECT *
6> FROM Employee where
7> salary > @SalaryQty
8> )
9> GO
1>
2>
3>
4> SELECT DISTINCT name
5> FROM dbo.fnSalaryCount(25) AS a
6> JOIN job AS j
7> ON a.id = j.id
8> GO
name
----------
Celia
Jason
Linda
Robert
1>
2> drop function fnSalaryCount
3> drop table employee;
4> drop table job;
5> GO
1>
2>
Use Count function in a left join
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> insert into employee (ID, name, salary) values (2, "Robert", 4321)
2> GO
1> insert into employee (ID, name, salary) values (3, "Celia", 5432)
2> GO
1> insert into employee (ID, name, salary) values (4, "Linda", 3456)
2> GO
1> insert into employee (ID, name, salary) values (5, "David", 7654)
2> GO
1> insert into employee (ID, name, salary) values (6, "James", 4567)
2> GO
1> insert into employee (ID, name, salary) values (7, "Alison", 8744)
2> GO
1> insert into employee (ID, name, salary) values (8, "Chris", 9875)
2> GO
1> insert into employee (ID, name, salary) values (9, "Mary", 2345)
2> GO
1>
2> insert into job(ID, title, averageSalary) values(1,"Developer",3000)
3> GO
1> insert into job(ID, title, averageSalary) values(2,"Tester", 4000)
2> GO
1> insert into job(ID, title, averageSalary) values(3,"Designer", 5000)
2> GO
1> insert into job(ID, title, averageSalary) values(4,"Programmer", 6000)
2> GO
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
1> select * from job;
2> GO
ID title averageSalary
----------- ---------- -------------
1 Developer 3000
2 Tester 4000
3 Designer 5000
4 Programmer 6000
1>
2> SELECT count(e.id), j.title
3> FROM Employee e LEFT JOIN job j
4> ON e.ID = j.ID
5> group by j.title
6> ORDER BY j.title
7> GO
title
----------- ----------
5 NULL
1 Designer
1 Developer
1 Programmer
1 Tester
1>
2>
3>
4> drop table employee;
5> drop table job;
6> GO
1>
2>
Using TOP in table join
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> SELECT TOP 5 e.ID, e.Name, j.title
3> FROM employee e JOIN job j
4> ON e.ID = j.ID
5> ORDER BY j.title
6> GO
ID Name title
----------- ---------- ----------
3 Celia Designer
1 Jason Developer
4 Linda Programmer
2 Robert Tester
(4 rows affected)
1>
2> drop table employee;
3> drop table job;
4> GO
1>
2>