Views and Nested Subqueries
Views and Nested Subqueries
select name
from tblemployee
where salary>(select salary
from tblemployee
where name='sam')
Name
ben
sara
valarie
james
russell
2- Find names and salary of employees who are in the same department as the employee john
select name,salary
from tblemployee
where departmentid=(select departmentid
from tblemployee
where name='john')
name salary
tom 4000
john 3500
ben 7000
valarie 5500
Q: Find name and department names of employees who work in the department of the employee john and have
salaries greater than the than the employee with id 3.
3- Find id, names and salaries of employees who have salary equal to any of minimum of salaries in each
department.
select id,name,salary
from tblemployee
where salary in (select min(salary)
from tblemployee as e,tbldepartment as d
where e.departmentid=d.id
1
group by departmentname)
or:
select id,name,salary
from tblemployee
where salary =any (select min(salary)
from tblemployee as e,tbldepartment as d
where e.departmentid=d.id
group by departmentname)
id name salary
2 pam 3000
3 john 3500
5 todd 2800
4- Find id and names of employees who have salaries smaller than any of the salaries in the department HR.
5- Find the names of employees with salaries greater than all the averages of salaries in each department.
select id,name
from tblemployee 3900,5000, 3650
where salary>all(select avg(salary)
from tblemployee as e,tbldepartment as d
where e.departmentid=d.id
group by departmentname)
name
ben
valarie
james
russell
2
views
A view is nothing more than a saved SQL query. The View itself does not store any data by default. A view
can also be considered as a virtual table.
Table 1: Employee
Table 2: Department
DeptId DeptName
1 IT
2 Payroll
3 HR
4 Admin
View Creation:
General Syntax:
create view view_name
as
< query expression >
EX. 1:
If we need to write a view to get the ID, name, salary of all employees along with their department names,
then the view is created as follow:
Create View EmployeesByDepartment
as
3
Select Id, Name, Salary, DeptName
from Employee
join Department
on Employee.DepartmentId = Department.DeptId ;
EX. 2:
Find the name and salary of all employees who are in IT or HR departments using the above view?
select ID,salary
from EmployeesByDepartment
where DeptName='IT' or DeptName='HR';
Id salary
1 5000
3 6000
5 3200
6 4800
Updatable Views:
Let's create a view, which returns all the columns from the tblEmployees table, except Salary column.
Create view EmployeesDataExceptSalary
as
Select Id, Name, Gender, DepartmentId
from Employee
Is it possible to Insert, Update and delete rows, from the underlying Employee table, using view
EmployeesDataExceptSalary?
Yes, SQL server views are updateable.
EX:
Update Name column from Mike to Mikey in the Employee table using the above view.
Update EmployeesDataExceptSalary
Set Name = 'Mikey' Where Id = 2
It is also possible to insert and delete rows from the base table using views as follow:
where Id = 2
4
Insert into vWEmployeesDataExceptSalary
values (2, 'Mikey', 'Male', 2) NULL because the column is not
found in the select clause of the
view definition.
Problem:
If we have the above view (EmployeesByDepartment) that joints the two tables and we want update
John's department, from HR to IT. At the moment, there are 2 employees (Ben, and John) in the HR
department.
Update EmployeesByDepartment
set DeptName='IT' where Name= 'John'
What happened?
DeptId DeptName
1 IT
2 Payroll
5
3 IT
4 Admin
So, if a view is based on multiple tables, and if you update the view, it may not update the underlying base tables
correctly.
Important Notes:
To look at view definition, use the following syntax:
sp_helptext view_Name
Sp_helptext EmployeesByDepartment