Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Views and Subqueries

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

Views and Subqueries

Views
• A view is nothing more than a SQL statement
that is stored in the database with an
associated name. A view is actually a
composition of a table in the form of a
predefined SQL query.
• A view can contain all rows of a table or select
rows from a table. A view can be created from
one or many tables which depends on the
written SQL query to create a view.
Creating Views
• Database views are created using the CREATE
VIEW statement. Views can be created from a
single table, multiple tables or another view.

• CREATE VIEW view_name AS SELECT column1,


column2..... FROM table_name WHERE
[condition];
ID Name Age Address Salary
1 Ramesh 35 Delhi 2000
2 Kiran 34 Kota 1500
3 Karan 25 Mumbai 7000
4 Harsh 23 Bhopal 4500
5 Komal 21 Indore 6000
6 Mehak 27 Amritsar 10000
7 Daman 30 jalandhar 2000
• Following is an example to create a view from
the CUSTOMERS table. This view would be
used to have customer name and age from the
CUSTOMERS table.

• CREATE VIEW Employee_VIEW AS SELECT


name, age FROM employee;
• SELECT * FROM Employee_VIEW;
The WITH CHECK OPTION
• The WITH CHECK OPTION is a CREATE VIEW statement
option. The purpose of the WITH CHECK OPTION is to
ensure that all UPDATE and INSERTs satisfy the
condition(s) in the view definition.
• If they do not satisfy the condition(s), the UPDATE or
INSERT returns an error.

• CREATE VIEW CUSTOMERS_VIEW AS SELECT name,


age FROM CUSTOMERS WHERE age IS NOT NULL WITH
CHECK OPTION;
Updating a View
A view can be updated under certain conditions which are given
below −
• The SELECT clause may not contain the keyword DISTINCT.
• The SELECT clause may not contain summary functions.
• The SELECT clause may not contain set functions.
• The SELECT clause may not contain set operators.
• The SELECT clause may not contain an ORDER BY clause.
• The FROM clause may not contain multiple tables.
• The WHERE clause may not contain subqueries.
• The query may not contain GROUP BY or HAVING.
• Calculated columns may not be updated.
• All NOT NULL columns from the base table must be included in the
view in order for the INSERT query to function.
• UPDATE employee_VIEW SET AGE = 35
WHERE name = 'Ramesh';
Inserting Rows into a View
• Rows of data can be inserted into a view. The
same rules that apply to the UPDATE
command also apply to the INSERT command.
• Here, we cannot insert rows in the
CUSTOMERS_VIEW because we have not
included all the NOT NULL columns in this
view, otherwise you can insert rows in a view
in a similar way as you insert them in a table.
Deleting Rows into a View
• Rows of data can be deleted from a view. The
same rules that apply to the UPDATE and INSERT
commands apply to the DELETE command.

• DELETE FROM CUSTOMERS_VIEW WHERE age =


22;
• This would ultimately delete a row from the base
table CUSTOMERS and the same would reflect in
the view itself.
Dropping Views
• DROP VIEW view_name;
• DROP VIEW employee_VIEW;

• Rename view
• RENAME original_view_name TO
new_view_name;
Subquery
• A Subquery or Inner query or a Nested query is
a query within another SQL query and
embedded within the WHERE clause.
• A subquery is used to return data that will be
used in the main query as a condition to further
restrict the data to be retrieved.
• Subqueries can be used with the SELECT, INSERT,
UPDATE, and DELETE statements along with the
operators like =, <, >, >=, <=, IN, BETWEEN, etc.
There are a few rules that subqueries must
follow −
• Subqueries must be enclosed within parentheses.
• A subquery can have only one column in the SELECT clause,
unless multiple columns are in the main query for the
subquery to compare its selected columns.
• An ORDER BY command cannot be used in a subquery,
although the main query can use an ORDER BY. The GROUP
BY command can be used to perform the same function as
the ORDER BY in a subquery.
• Subqueries that return more than one row can only be used
with multiple value operators such as the IN operator.
• The BETWEEN operator cannot be used with a subquery.
However, the BETWEEN operator can be used within the
subquery.
Subqueries with the SELECT Statement

• SELECT column_name [, column_name ]


FROM table1 [, table2 ] WHERE column_name
OPERATOR (SELECT column_name [,
column_name ] FROM table1 [, table2 ]
[WHERE])
ID Name Age Address Salary
1 Ramesh 35 Delhi 2000
2 Kiran 34 Kota 1500
3 Karan 25 Mumbai 7000
4 Harsh 23 Bhopal 4500
5 Komal 21 Indore 6000
6 Mehak 27 Amritsar 10000
7 Daman 30 jalandhar 2000

SQL> SELECT * FROM CUSTOMERS WHERE ID IN


(SELECT ID FROM CUSTOMERS WHERE SALARY >
4500) ;
Subqueries with the INSERT Statement
• Subqueries also can be used with INSERT statements.
The INSERT statement uses the data returned from
the subquery to insert into another table. The
selected data in the subquery can be modified with
any of the character, date or number functions.
• INSERT INTO table_name [ (column1 [, column2 ]) ]
SELECT
[ *|column1 [, column2 ] FROM table1 [, table2 ]
[ WHERE VALUE OPERATOR ]
• INSERT INTO CUSTOMERS_BKP
SELECT * FROM CUSTOMERS WHERE ID IN
(SELECT ID FROM CUSTOMERS) ;
Subqueries with the UPDATE Statement

• The subquery can be used in conjunction with


the UPDATE statement. Either single or
multiple columns in a table can be updated
when using a subquery with the UPDATE
statement.
• UPDATE table SET column_name = new_value [
WHERE OPERATOR [ VALUE ] (SELECT
COLUMN_NAME FROM TABLE_NAME)
[ WHERE) ]
• The following example updates SALARY by
0.25 times in the CUSTOMERS table for all the
customers whose AGE is greater than or equal
to 27.

• UPDATE CUSTOMERS SET SALARY = SALARY *


0.25 WHERE AGE IN (SELECT AGE FROM
CUSTOMERS_BKP WHERE AGE >= 27 );
Subqueries with the DELETE Statement

• The subquery can be used in conjunction with


the DELETE statement like with any other
statements
• DELETE FROM TABLE_NAME [ WHERE
OPERATOR [ VALUE ] (SELECT COLUMN_NAME
FROM TABLE_NAME) [ WHERE) ]
• The following example deletes the records
from the CUSTOMERS table for all the
customers whose AGE is greater than or equal
to 27.
• DELETE FROM CUSTOMERS WHERE AGE IN
(SELECT AGE FROM CUSTOMERS_BKP WHERE
AGE >= 27 );

You might also like