Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

VIEWS and

Subqueries
What is a View
• In SQL, a view is a virtual table based on the result-set of an SQL
statement.
• It is called virtual table because a view doesn’t store data, you can
query a view like you can a table.
• A view can combine data from two or more table, using joins, and just
contain a subset of information.
• This makes them convenient to abstract, or hide, complicated queries.
• Create a view
Operations • Update a view
on View • Drop a view
Create a view
Syntax
CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example
create view s as select ROLL_NO, S_NAME from s_details;

Note: A view always shows up-to-date data! The database engine


recreates the data, using the view's SQL statement, every time a user
queries a view.
How to check the view
Syntax
SELECT * FROM viewname
Example
select * from n;
Updating a View
A view can be updated with the CREATE OR REPLACE VIEW command.
Syntax:
CREATE OR REPLACE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example
create or replace view s as select ROLL_NO, S_NAME from s_details
where marks>80
update s set marks=80 where roll_no=1;
Dropping a view
• A view is deleted with the DROP VIEW command.
Syntax
DROP VIEW view_name;
Example
DROP VIEW s;
Benefits of a View
• There are many benefits to using views.  Some of them are listed
below
• Enforce Business Rules 
• Consistency 
• Security  
• Simplicity 
• Space 
Disadvantages of View
• Performance – What may seem like a simple query against a view
could turn out to be a hugely complex job for the database engine. 
That is because each time a view is referenced, the query used to
define it, is rerun.
• Modifications – Not all views support INSERT, UPDATE,
or DELETE operations.  In general, in order to support these
operations, the primary key and required fields must be present in the
view.  Complex multi-table views are generally read-only.
Subqueries/Nested Queries
• Subqueries (also known as inner queries or nested queries) are used for
performing operations in multiple steps. 
• A subquery is also called an inner query or inner select, while the statement
containing a subquery is also called an outer query or outer select.
• The inner query executes first before its parent query so that the results of
an inner query can be passed to the outer query.
• A subquery may occur in :
• A SELECT clause
• A FROM clause
• A WHERE clause
Important Rules
• Subqueries can be used with SELECT, UPDATE, INSERT, DELETE
statements along with expression operator. It could be equality
operator or comparison operator such as =, >, =, <= and Like operator.
• A subquery is a query within another query. The outer query is called
as main query and inner query is called as subquery.
• The inner query executes first before its parent query so that the
results of an inner query can be passed to the outer query.
• Subquery must be enclosed in parentheses.
• Subqueries are on the right side of the comparison operator.
Syntax

SELECT column_name [, column_name ]


FROM table1 [, table2 ]
WHERE column_name OPERATOR
(SELECT column_name [, column_name ]
FROM table1 [, table2 ]
[WHERE])
example

SELECT *
FROM CUSTOMERS
WHERE ID IN (SELECT ID
FROM CUSTOMERS
WHERE SALARY > 4500) ;
Example
Stu_marks(stu_ID, marks)
Stu_details(student_ID, Name)
Example

SELECT * FROM Stu_marks WHERE stu_id = 300;

SELECT a.student_id, name, marks FROM


Stu_details a, Stu_marks  b WHERE a.student_id =
b.stu_id AND marks >80;
Combining the queries
• Two queries identified students who get the better number than the
student who's StudentID is 300.
SELECT a.student_id, name, marks FROM Stu_details a, Stu_marks  b
WHERE a.student_id = b.stu_id AND marks >( select marks from
Stu_marks where stu_ID=300);
Questions
• If a subquery (inner query) returns a null value to the outer query, the
outer query will not return any rows
Question
• Can we use order by clause in the inner query
DCL

You might also like