0% found this document useful (0 votes)
8 views

Views and Nested Subqueries

Uploaded by

mohamedapdo1212
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Views and Nested Subqueries

Uploaded by

mohamedapdo1212
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

DBMS(I) Laboratory

Lab. 7: Views and Nested Subqueries


Depending on the tables (tblEmployee and tbl Department) from the previous lecture answer the following
queries:
1- Find the names of employees who have salary greater the salary of the employee Sam.

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.

select id,name 3000,4800


from tblemployee
where salary<any(select salary
from tblemployee as e,tbldepartment as d
where e.departmentid=d.id and departmentname='HR')
id name
1 tom
2 pam
3 john
4 sam
5 todd

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

ID Name Gender Salary DepartmentId


1 John Male 5000 3

2 Mike Male 3400 2

3 Pam Female 6000 1

4 Todd Male 4800 4

5 Sara Female 3200 1

6 Ben Male 4800 3

Table 2: Department

DeptId DeptName

1 IT

2 Payroll

3 HR

4 Admin

 Advantages of using views:


1- Views can be used to reduce the complexity of the database schema.
2- Views can be used as a mechanism to implement row and column level security.

 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:

Delete from EmployeesDataExceptSalary

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.

ID Name Gender Salary DepartmentId


1 John Male 5000 3

2 Mikey Male NULL 2

3 Pam Female 6000 1

4 Todd Male 4800 4

5 Sara Female 3200 1

6 Ben Male 4800 3

 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'

ID Name Salary DepartmentId


ID Name Salary DepartmentId
1 John 5000 IT
1 John 5000 HR
2 Mikey NULL Payroll
2 Mikey NULL Payroll update
3 Pam 6000 IT
3 Pam 6000 IT
4 Todd 4800 Admin
4 Todd 4800 Admin
5 Sara 3200 IT
5 Sara 3200 IT
6 Ben 4800 IT
6 Ben 4800 HR

 What happened?

The UPDATE statement, updated DeptName from HR to IT in Department table, instead of


updating DepartmentId column in Employee table.

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

 To modify a view, use the following:


Alter view view_Name
As
< modified view definition >
EX:
Modify the EmployeesByDepartmen view, so that the salary column is shown in the result?
alter view EmployeesByDepartment
as
Select Id, Name, Salary,gender,DeptName
from tblEmployee
join tblDepartment
on tblEmployee.DepartmentId = tblDepartment.DeptId ;

 To Drop a view use the following:


Drop view view_Name

drop view EmployeesByDepartment

You might also like