Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

ASSIGNMENT-8

SUB-QUERY
 A subquery is a SELECT query that is nested within another SELECT, INSERT, UPDATE,
or DELETE statement.
 A subquery can also be nested inside another subquery.
 Subqueries can often be re-written into regular JOINs.
 Sometimes an existence subquery can perform better than equivalent non-subquery methods.
 A correlated subquery is a subquery whose results depend on the values of the outer query.

SYNTAX:-

SELECT <SELECT list>


FROM <SomeTable>
WHERE <SomeColumn> = (
SELECT <single column>
FROM <SomeTable>
WHERE <condition that results in only one row returned>)
Or:

SELECT <SELECT list>


FROM <SomeTable>
WHERE <SomeColumn> IN (
SELECT <single column>
FROM <SomeTable>
[WHERE <condition>])

1) Create a table called employee with following fields.


id INT
first_name VARCHAR(10)
last_name VARCHAR(10)
salary INT
start_Date DATE
region VARCHAR(10)
city VARCHAR(20)

2) Insert the following data


id first_name last_name salary start_Date region city
----------- ---------- ---------- ------------ ----------------------- ---------- --------------------
1 Jason Martin 5890 2005-03-22 North Vancouver
2 Alison Mathews 4789 2003-07-21 South Utown
3 James Smith 6678 2001-12-01 North Paris
4 Celia Rice 5567 2006-03-03 South London
5 Robert Black 4467 2004-07-02 East Newton
6 Linda Green 6456 2002-05-19 East Calgary
7 David Larry 5345 2008-03-18 West New York
8 James Cat 4234 2007-07-17 West Regina
9 Joan Act 6123 2001-04-16 North Toronto
3) Select region from employee where salary belongs to 5000 and 6000 using sub query.
4) Select ID, Frist name and salary from employee where region belongs to North.
5) Create a table called title with following fields.
id INTEGER
job_title VARCHAR(20)

6) Insert the following data


1, 'developer'
1, 'manager'
2, 'tester'
2, 'programmer'
3, 'boss'
4, 'sales'
5, 'market'
6, 'coder'
7, 'tester'
8, 'developer'
9, 'manager'

7) Select ID, Frist name from employee where ID belongs to title table.
8) Show all details of employee(s) whose job title is Developer.
9) Show ID, Frist name for those employee(s) whose ID belongs to title table and start date
is greater than '3-1-2003’.

VIEWS
Views are virtual tables that are compiled at run time. The data associated with views are not
physically stored in the view, but it is stored in the base tables of the view. A view can be made
over one or more database tables. Generally we put those columns in view that we need to
retrieve/query again and again. Once you have created the view, you can query view like as table.
We can make index, trigger on view.
In Sql Server we make views for security purpose since it restricts the user to view some
columns/fields of the table(s). Views show only those columns that are present in the query which
is used to make view. One more advantage of Views is data abstraction since the end user is not
aware of all the data present in database table.

Syntax for View


CREATE VIEW view_name
AS
select_statement[ ]
Types of Views
In Sql Server we have two types of views.

1) System Defined Views


System defined Views are predefined Views that already exist in the Master database of Sql
Server.

2) User Defined Views


These types of view are defined by users. We have two types of user defined views.

i) Simple View
When we create a view on a single table, it is called simple view.

ii) Complex View


When we create a view on more than one table, it is called complex view.
KEY NOTE: -

 In simple view we can insert, update, delete data. We can only insert data in simple view
if we have primary key and all not null fields in the view.
 Syntax for inserting data into a view is same as insert into statement.
 Syntax for updating data into a view is same as update statement.
 Syntax for deleting data from a view is same as delete statement.
 To delete a view syntax is drop view view_name.
 To display all the data from a view Query view like as table.
Example: - Select * from view_name;
 You can modify the definition of a VIEW in SQL Server without dropping it by using the
ALTER VIEW Statement.
SYNTAX:-
ALTER VIEW view_name AS
SELECT expressions
FROM tables
WHERE conditions;
10) Create a view called emp_data from the table employee and title having column id,
11) first_name, last_name, salary and job_title.
12) Create a view called emp_data2 from the table employee and title having column id,
13) first_name, last_name, region and job_title where region in west and North.
14) Retrieve all the records from emp_data and emp_data2 view.
15) Try to insert a record in emp_data view.
16) Update the region to SOUTH in the view called emp_data2 where region is North.
17) Delete the record form emp_data where between 2 and 5.
18) Add a new column in emp_data view called city.
19) Delete the view called emp_data2.

You might also like