Subqueries and Comparison Operators : Subqueries « Subquery « SQL Server / T-SQL






Subqueries and Comparison Operators


15>
16> CREATE TABLE employee  (emp_no    INTEGER NOT NULL,
17>                         emp_fname CHAR(20) NOT NULL,
18>                         emp_lname CHAR(20) NOT NULL,
19>                         dept_no   CHAR(4) NULL)
20> GO
1> insert into employee values(1,  'Matthew', 'Smith',    'd3')
2> insert into employee values(2,  'Ann',     'Jones',    'd3')
3> insert into employee values(3,  'John',    'Barrimore','d1')
4> insert into employee values(4,  'James',   'James',    'd2')
5> insert into employee values(5,  'Elsa',    'Bertoni',  'd2')
6> insert into employee values(6,  'Elke',    'Hansel',   'd2')
7> insert into employee values(7,  'Sybill',  'Moser',    'd1')
8> GO

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)
1> select * from employee
2> GO
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 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>
2> -- Subqueries and Comparison Operators
3>
4> SELECT emp_fname, emp_lname FROM employee
5>        WHERE dept_no = (SELECT dept_no FROM department WHERE dept_name = 'marketing')
6> GO
emp_fname            emp_lname
-------------------- --------------------
Matthew              Smith
Ann                  Jones

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








Related examples in the same category

1.A subquery is simply a SELECT query within a SELECT query
2.A subquery can be used with comparison operators
3.Return a single value from subquery