0% found this document useful (0 votes)
5 views18 pages

Views

Uploaded by

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

Views

Uploaded by

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

JOINS

Different Types of SQL


JOINs
• (INNER) JOIN: Returns records that have matching
values in both tables
• LEFT (OUTER) JOIN: Return all records from the left
table, and the matched records from the right table
• RIGHT (OUTER) JOIN: Return all records from the
right table, and the matched records from the left
table
• FULL (OUTER) JOIN: Return all records when there
is a match in either left or right table
INNER JOIN
SELECT column_name(s)
FROM table1
INNER JOIN table2 ON table1.column_n
ame = table2.column_name;
LEFT OUTER JOIN
SELECT column_name(s)
FROM table1
LEFT JOIN table2 ON table1.column_na
me = table2.column_name;
RIGHT OUTER JOIN
SELECT column_name(s)
FROM table1
RIGHT JOIN table2 ON table1.column_name =
table2.column_name;
FULL OUTER JOIN
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2 ON
table1.column_name = table2.column_name;
VIEWS
• is a virtual table based on the result-set of an SQL
statement
• A view contains rows and columns, just like a real
table. The fields in a view are fields from one or more
real tables in the database.
• You can add SQL functions, WHERE, and JOIN
statements to a view and present the data as if the
data were coming from one single table.
SYNTAX
CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition
Example
Create View Employee_View2 as
select Emp_Id,Emp_Name,Emp_City
from Employee_Details
Retrieve Data from view
Select * from Employee_View2

OR

Select Emp_Id,Emp_Name,Emp_City
from Employee_View2
Drop a view
Drop View Employee_View2
DML Query In View
• In a view we can implement many types of DML query
like insert, update and delete.
• But for a successful implementation of a DML query
we should use some conditions like:
• View should not contain multiple tables
• View should not contain set function.
• View should not use the Distinct keyword
• View should not contain Group By, having clauses
• View should not contain Sub query
• View should not use Set Operators
• All NOT NULL columns from the base table must be included
in the view in order for the INSERT query to function.
create view data AS
select m.movie_no,m.title, m.type,i.cust_id,
i.issue_date ,i.return_date
from invoice i,movie m
where i.movie_no=m.movie_no

insert into data values(100,'ABC','Comedy','A100','23-sep-2018','24-


sep-2018')
View or function 'data' is not updatable because the modification
affects multiple base tables.
create view data1 as
select type, avg(price) as AVG
from movie
group by type

insert into data1 values ('Thriller',150)


Update or insert of view or function 'data1' failed
because it contains a derived or constant field.
create table student
(sid int not null,
sname varchar(10),
stel int not null,
saddr varchar(20) not null)

insert into student values(1,‘A',123,'Bandra’)


insert into student values(2,'B',123,'Bandra’)
create view s1 as
select sid, sname
from student

insert into s1 values(3,'c')


Cannot insert the value NULL into column 'stel', table
'neha.dbo.student'; column does not allow nulls. INSERT
fails.

You might also like