Dms Unit III
Dms Unit III
UNIT-3 ( 18 Marks)
MSBTE Question bank
27
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
i) Lower(char)-
Returns the input string with all letters in lower case.
Example: SQL>Select lower (‘RAJESH’) from dual;
Output: rajesh
ii) Upper(char)-
Returns the input string with all letters in upper case.
Example: SQL>Select upper (‘rajesh’) from dual;
Output: RAJESH
iii) Ltrim(char,set)-
It removes or trims from left of character string
. Example: SQL>Select Ltrim(‘university’,’univ’) from dual;
Output: ersity
iv) Rtrim(char,set)-
It removes or trims from right of character string.
Example: SQL>Select Rtrim(‘university’,’sity’) from dual;
Output: univer
v) Length(char)-
It returns length of character string.
Example: SQL> Select length(‘University’) from dual;
Output:10
vi) Concat(str1,str2,...)-
Returns the string that result from concatenating the arguments.
Example: Select Concat(‘employee’, ‘name’) from dual;
Output: employee name
vii) Lpad(str, len, padstr)-
Returns the string str, left-padded with the string padstr to a
length of len characters.
Example: Select lpad(ename,10.’*’) from emp where
empno=7782;
viii) Rpad(str,len,padstr)-
Returns the string str, right-padded with the string padstr to a
length of len characters.
Example: Select rpad(ename,10.’*’) from emp where
empno=7782
viii) Substr(Char,m,n)-
It returns a portion of char, beginning at a character m, n
character long.
Example: Select substr(‘College’,3,4) from dual;
Output: lleg
4 Explain joins in SQL with examples. W-18, 4
S-24
28
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
JOIN:
A SQL join is an instruction to combine data from two sets of
data (i.e. two tables). A
JOIN clause is used to combine rows from two or more tables,
based on a related column
between them. SQL Join types are as follows:
1) INNER JOIN or EQUI JOIN:
A join which is based on equalities is called equi join. In equi
join comparison
operator “=” is used to perform a Join.
Syntax:
SELECT
tablename.column1_name,tablename.column1_name
FROM table_name1,table_name2
where
table_name1.column_name=table_name2.column_name;
Example:
Select stud_info.stud_name, stud_info.branch_code,
branch_details.location
From stud_info, branch_details
Where Stud_info.branch_code=branch_details.branch_code;
2) SELF JOIN:
The SQL SELF JOIN is used to join a table to itself, as if the
table were two
tables, temporarily renaming at least one table in the SQL
statement.
Syntax:
SELECT a.column_name, b.column_name
FROM table1 a, table1 b
WHERE a.common_filed = b.common_field;
Example:
Select x.stud_name, y.stud_name
from stud_info x, stud_info y
Where x.leader= y.stud_id;
29
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
3) LEFT OUTER JOIN:
A left outer join retains all of the rows of the “left” table,
regardless of whether there is a
row that matches on the “right” table.
Syntax:
Select column1name,column2name
from table1name any_alias1 ,table2name any_alias2
on any_alias1.columnname(+) = any_alias2.columnname;
OR
Select column1name,column2name
from table1name left outer join table2name
on table1name.columnname= table2name.columnname;
Example:
select last_name, department_name
from employees e, departments d
on e.department_id(+) = d.department_id;
OR
select last_name, department_name
from employees left outer join departments
on employees.department_id = departments.department_id;
30
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
For example:
In emp table and salgrade table. The salgrade table contains
grade and their low
salary and high salary. Suppose you want to find the grade of
employees based on
their salaries then you can use NON EQUI join.
Select e.empno, e.ename, e.sal, s.grade
from emp e, salgrade s
where e.sal between s.lowsal and s.hisal;
4 Explain set operators with example W-18 4
Set operators combine the results of two component queries
into a single result. Queries
containing set operators are called as compound queries. Set
operators in SQL are
represented with following special keywords as: Union, Union
all, intersection & minus.
Consider data from two tables emp and employee as
32
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
Name char (20)); iii) Change the seq_ 1 max value 20 to 50. iv)
Drop the sequence.
i) create sequence Seq_1 start with 1 increment by 1 minvalue Query 1: 2M,
1 maxvalue Query 2: 2M,
20; Query 3 : 1M,
ii) insert into student values(Seq_1.nextval,’ABC’) Query 4 : 1M
iii) Alter sequence Seq_1 maxvalue 50;
iv) Drop sequence Seq_1
33
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
3 List the SQL ope S-19 4
rations and explain range searching operation ‘between’ and
pattern matching operator ‘like’ with example.
Types of SQL operators :
1) SQL Arithmetic Operators
2) SQL Comparison Operators
3) SQL Logical Operators
Arithmetic operators are used to perform arithmetic operations List of
on operators :
numbers. They are +,-,*, / and %. 2M,
Comparison operators are used in between two variables to between
compare operator :
their values. They are <,>,<=,>=,=,!=or <>,!< and !>.' 1M,
Logical operators are used for the Boolean results in sql queries Like
for operator :
comparison of values from the attributes of the tables. Eg: Any, 1M
Exists,
All, Like, Between, In etc.
Between operator: The BETWEEN operator is used to search
for
values that are within a set of values, given the minimum value
and the maximum value inclusive of both the limits.
Eg: select * from emp where salary between 40000 and 50000;
34
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
This will results in rows from emp table where salary falls in
the range of 40000 to 50000.
Like operator :
The LIKE operator is used to compare a value to similar values
using
wildcard operators. It uses two wild characters as ‘%’ and ‘_’
where ‘%’
represents all characters of the pattern and ‘_’ represents one
single character from pattern.
Eg :
Select ename from emp where ename like ‘S%’;
This will return all employee names starting with ‘S’.
Select ename from emp where ename like ‘_a%;
This will return all employee names whose second character is
‘a’
4 Write syntax for creating synonyms with example. S-19 2
Syntax to create synonym: 1 M for
CREATE SYNONYM SYNONYM_name correct
FOR Table_name; syntax, 1 M
Example to create synonym: for
CREATE SYNONYM offices correct
FOR locations example
5 Write and explain syntax for creating view with example. S-19 4
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.
View has two types:
1. Simple view: The fields in a view are fields from one table
in the
database.
2. Complex view: The fields in a view are fields from more
than one table 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 different table.
CREATE VIEW Syntax
Create view view_name As
Select column1, column2…
From table_name
Where condition ;
Example
Create view mumbai_customers AS
Select customer_name,contact_name
From customers
Where city=’Mumbai’;
6 Explain any four aggregate functions with example. S-19 4
W-23
35
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
An aggregate function is a function where the values of
multiple rows are grouped together as input on certain criteria
to form a single value of
more significant meaning.
Aggregate functions are :
1) Count()
2) Sum()
3) Avg()
4) Min()
5) Max()
1. Count () - 1) It returns number of rows from the given table
if no attribute is mentioned.
2) If some attribute is mentioned, it gives total number of not
null valfor that attribute.
Eg :Select count(*) from emp;
Returns total number of records from emp table.
1) Select count(telephone) from emp;
Returns total number of employees having telephone numbers.
2. Sum() - It give total of all values from a numeric attribute of
the given table,
Eg :Select sum(salary) from emp;
Returns total salary drawn of all employees from the emp table.
3. Avg () - It gives average of all the numeric values of the
given attribute from the table.
Eg :Select Avg(salary) from emp;
Returns average salary of employees from emp table.
4. Min () - It gives minimum of all the values of the numeric
given attribute from the table.
Eg :Select Min(salary) from emp;
Returns minimum salary value from emp table,
5. Max () - It gives maximum of all the values of the numeric
given attribute from the table.
Eg :Select Max(salary) from emp;
retunes maximum salary value from emp tableues
36
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
Syntax: Drop index <index_name>; allowed.)
(OR)
E.g. (Assuming idx_empno created on employee table)
Drop index idx_empno;
8 Consider the table Student (name, marks, dept, age, place, S-19 6
phone, birthdate) Write SQL query for following : (i) To list
students having place as ‘Pune’ or ‘Jalgaon’. (ii) To list
students having same department (dept) as that of ‘Rachana’.
(iii) To change marks of ‘Rahul’ from 81 to 96. (iv) To list
student name and marks from ‘Computer’ dept. (v) To list
student name who have marks less than 40. (vi) To list students
who are not from ‘Mumbai’.
select name from Student where place= ‘Pune’ or Each Correct
place=’Jalgaon’; Query : 1M
(OR)
select name from Students where place in(‘Pune’,‘Jalgaon’);
ii) select name from Student where dept=(select dept from
student where
name=’Rachana’);
iii) update Student set marks=96 where name= ‘Rahul’;
v)select name,marks from Student where dept=‘Computer’;
iv) select name from Student where marks<40;
v) select * from Student where place != ‘Mumbai’;
9 i) Write a command to create table student (RNo., name, S-19 6
marks, dept.) with proper datatype and RNo as primary key.
(ii) Write a command to create and drop sequence.
i) create table student
(
RNO number(5) constraint student _RNOpk primary key,
name varchar2(20),
marks number(4),
dept varchar2(20)
);
(OR)
create table student
(
RNO number(5) ,
name varchar2(20),
marks number(4),
dept varchar2(20),
constraint student_RNO_pk primary key(RNO),
);
ii) Create Sequence:
Create sequence <seq_name>
Start with [initial value]
Increment by [value]
Minvalue [minimum value]
Maxvalue [maximum value]
[cycle/no cycle]
[{cache value / No cache}]
37
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
[{order / No order}];
(OR)
(Creating sequence for Employee number of emp table.)
Create sequence emp_eno_seq
start with 1
increment by 1
maxvalue 100
no cycle
no cache;
Drop sequence:
Drop sequence<Sequence Name>;
(OR)
Drop sequence emp_eno_seq
10 Write SQL command for following : (i) Create user (ii) Grant S-19 6
privileges to user (iii) Remove privileges from user
i) Create user Each correct
CREATE USER <username> IDENTIFIED BY <password>; command:
(OR) 2M
CREATE USER RAJ IDENTIFIED BY RAJ123; (Note: Either
ii) Grant privileges to user. syntax or
GRANT <privilege list> ON <relation name or view name> example can
TO<user list>; be
(OR) considered.
(assuming table Employee for granting permissions to user Any other
‘RAJ’ example
for select, insert, update and delete privilege) allowed
GRANT SELECT, INSERT,UPDATE,DELETE ON
EMPLOYEE TO RAJ;
Iii) Remove privileges from user.
REVOKE <privilege list> ON <relation name or view name >
FROM <user list>;
(OR)
(assuming table Employee for revoking permissions to user
‘RAJ)
REVOKE SELECT, INSERT,UPDATE,DELETE ON
EMPLOYEE FROM RAJ;
38
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
character specified to a total of length specified. Ltrim(String)
-removes white space or other specified characters from the left
end of the string Rtrim(String)--removes white space or other
specified characters from the right end of the string
Replace(String, char,char) – replace all occurrence of a
substring by another substring Substring(String,number) –
extracts substring from the string Translate(String,char,char) –
replace all occurrence of characters by other characters
2 Enlist DML commands. W-19 2
- used to insert new row into table Any 2
- used to delete a row from the table comman ds
– used to modify data in the table. 1M each
Select – used to view data from a table.
3 Explain any four set operators in SQL with example. W-19 4
Set operators combine the results of two component queries 1M each
into a for
single result. Queries containing set operators are called as explanat
compound queries. Set operators in SQL are represented with ion of
following special operator
keywords as: Union, Union all, intersection & minus. s with
1) Union: The Union of two or more sets contains all elements, example
which are present in either or both. Union works as or. The
duplicates of both the tables will appear only once.
E.g. select ename from emp1 union select ename from emp2;
2) Union all: The Union of 2 or more sets contains all elements,
which are present in both, including duplicates.
E.g. select ename from emp1 union all select ename from
emp2;
3) Intersection: The intersection of two sets includes elements
which are present in both.
E.g. select ename from emp1 intersect select ename from
emp2;
4) Minus: The minus of two sets includes elements from set1
minus
elements of set2.
E.g. select ename from emp1 minus select ename from emp2;
4 Describe create & alter command with syntax & example. W-19 4
1) The SQL CREATE TABLE statement is used to create a Each
new comman
table. d 2M
Syntax
The basic syntax of the CREATE TABLE statement is as
follows –
CREATE TABLE table_name
(
column1 datatype (size),
column2 datatype(size),
column3 datatype(size),
....
39
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
);
Example:
CREATE TABLE Persons
(
PersonIDnumber(10),
LastNamevarchar2(20),
FirstNamevarchar2(20),
Address varchar2(20),
City varchar2(20)
);
2)The ALTER TABLE statement is used to add, delete, or
modify columns in an existing table.
The ALTER TABLE statement is also used to add and drop
various constraints on an existing table.
i) To add Columns in a table
Syntax:ALTER TABLE table_name
ADD column_name datatype;
Example
ALTER TABLE Customers
ADD Email varchar2(20);
ii) To delete a column in a table
ALTER TABLE table_name
DROP COLUMN column_name;
Example
ALTER TABLE Customers
DROP COLUMN Email;
iii) To modify a column in a table
Syntax:
ALTER TABLE table_name
MODIFY COLUMN column_name datatype;
Example
ALTER TABLE Customers
MODIFY COLUMN customeridnumeric(10);
iv) To add Constraints in A table
Syntax:
ALTER TABLE table_name
ADD constraint constraintname (column_name);
Example:
ALTER TABLE Customers
ADD constraint primary key(CustomerID);
5 Describe Views and write a command to create view. W-19 4
A view is a virtual table based on the result set of the SQL Explana
statement. tion 3M
The fields in a view are fields from one or more than one table General
in the database. SQL functions, where, join statements can be syntax/
added to a view and the data in it can be presented as if it were example
from one table. The database engine recreates the data, using 1M
the view’s SQL statement, every time a user queries a view. A
view can be updated using the create or replace view command.
For deleting a view, drop query can be used.
40
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
General syntax to create a view:
create view viewname as select query.
Eg:
create view vw_student as select stud_id, stud_name,ssc_per
from
student;
6 Define index. Explain it’s types. W-19 4
An Index is a schema object. It is used by the oracle server to Definitio
improve the speed of retrieval of the rows from a table .Indexes n 1M
are of two types based on number of columns included in the Each
index. type
The types of index are: 1½M
1) Simple index: An index created on a single column of table
is
called as simple index
Syntax:
SQL>Create Index index_name on tablename(attribute);
Example:Create index emp_index on emp(empno);
2) Composite Index: An index created on more than one
column is called composite index.
Syntax:
SQL>Create Index index_name on
tablename(attribute1,attribute2);
Example: Create index emp_index on emp(empno,ename);
41
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
SQL>Insert into student
values(2,‟Raj‟,‟CO‟,‟FirstYear‟,‟22-
Sep2002‟,‟Mumbai‟,98896863)
(iv)Undo the insertion of second row:
SQL> rollback;
( OR)
SQL> rollback work;
(v)Create savepoint s1:
SQL>Savepoint s1;
(vi) insert one row into the table:
SQL>Insert into student
values(3,‟Beena‟,‟CO‟,‟FirstYear‟,‟30-
Dec-2002‟,‟Mumbai‟,97846455)
42
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
9 Consider the structure for book table as Book-Master (bookid, W-19 6
bookname, author, no_of copies, price) Write down SQL
queries for following (i) Write a command to create
Book_master table. (ii) Get authorwise list of all books. (iii)
Display all books whose price is between ` 500 & ` 800. (iv)
Display all books with details whose name start with ‘D’. (v)
Display all books whose price is above ` 700. (vi) Display all
books whose number of copies are less than 10.
(i) Write a command to create Book_Master table table. Each
SQL>Create table Book-Master( correct
bookid number(5), Query
bookname char(10), 1M
authorvarchar(20),
no_of_copiesnumber(10),
price number(10,2)
);
(ii) Get authorwise list of all books.
SQL>Select sum(no_of copies) from Book_Master group by
author;
(iii) Display all books whose price is between Rs.500 & Rs. 800
SQL> Select * from Book_Master where price between 500
and
800;
OR
SQL> Select * from Book_Master where price >=500 and
price<=800;
(iv) Display all books with details whose name start with ‘D’
SQL> Select bookname from Book_Master where bookname
like
„D%‟;
(v) Display all books whose price is above Rs. 700
SQL>Select * from Book_Master where price >700;
(vi) Display all books whose number of copies are less than 10
SQL>Select * from Book_Master where no_of_copies<10;
SELECT AVG(Price)
FROM Products;
2 List any four data types in sql S-22 2
Numeric data types such as: INT , TINYINT , BIGINT ,
FLOAT , REAL , etc.
Date and Time data types such as: DATE , TIME , DATETIME
, etc.
Character and String data types such as: CHAR , VARCHAR
, TEXT , etc.
43
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
Unicode character string data types such as: NCHAR ,
NVARCHAR , NTEXT , etc
3 Explain any two DDL commands along with example. S-22 4
Syntax –
Syntax to add a column to an existing table.
44
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
ADD column_name datatype;
Example –
In our Student_info table, we want to add a new column for
CGPA. The syntax would be as below as follows.
Syntax –
Syntax to remove an existing table.
Syntax –
Syntax to drop an existing table.
Syntax –
RENAME TABLE <Table Name> To <New_Table_Name>;
Example:
If you want to change the name of the table from Employee to
Emp we can use rename command as
RENAME TABLE Employee To EMP; syntax for i) Create
Index ii) Drop Index
4 Describe the concept of view with example. State its purpose. S-22 4
45
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
A view is a virtual table based on the result set of the SQL
statement.
The fields in a view are fields from one or more than one table
in the
database. SQL functions, where, join statements can be added
to a
view and the data in it can be presented as if it were from one
table.
The database engine recreates the data, using the view’s SQL
statement, every time a user queries a view. A view can be
updated
using the create or replace view command. For deleting a view,
drop
query can be used.
General syntax to create a view:
create view viewname as select query.
Eg:
create view vw_student as select stud_id, stud_name,ssc_per
from
student;
5 i) Create user ‘RAM’. ii) Grant create, select, insert, update, S-22 6
delete privileges to user ‘RAM’. iii) Remove update privilege
from user ‘RAM’
I)CREATE USER RAM IDENTIFIED BY RAM123;
ii) CREATE USER RAJ IDENTIFIED BY RAJ123;
ii) Grant privileges to user.
GRANT <privilege list> ON <relation name or view name>
TO<user list>;
(OR)
(assuming table Employee for granting permissions to user
‘RAM’
for select, insert, update and delete privilege)
GRANT SELECT, INSERT,UPDATE,DELETE ON
EMPLOYEE TO RAM;
Iii) Remove privileges from user.
REVOKE <privilege list> ON <relation name or view name >
FROM <user list>;
(OR)
(assuming table Employee for revoking permissions to user
‘RAM)
REVOKE SELECT, INSERT,UPDATE,DELETE ON
EMPLOYEE FROM RAM;
49
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
DROP INDEX
The DROP INDEX statement is used to delete an index in a
table.
Syntax:
DROP INDEX index_name ON table_name;
Example:
DROP INDEX id_firstname ON Student;
8 State the use of group by and order by clauses. W-22 4
Group by Clause: 2 M=
Group by clause is used to collect the data as multiple records group by
and group them to produce the result. 2 M=
Syntax: order by
1. SELECT column_name, function(column_name)
2. FROM table_name
3. WHERE condition
4. GROUP BY column_name;
Ex: select avg(sal) from emp_details group by deptno;
Order by Clause:
To view the data in sorted order, the order by clause is used.
By default, the data is sorted in ascending order.
Syntax:
5. SELECT expressions
6. FROM tables
7. [WHERE conditions]
8. ORDER BY expression [ ASC | DESC ];
Ex: select deptno from emp_details order by deptno;
Ex: select deptno from emp_details order by deptno desc; (for
descending order
9 Write the SQL queries for following EMP table. Emp (empno, W-22 6
deptno, ename, salary, designation, city.) i) Display average
salary of all employees. ii) Display names of employees who
stay in Mumbai or Pune. iii) Set the salary of employee
'Ramesh' to 50000. iv)Display names of employees whose
salaries are less than 50000. v) Remove the Record of
employees whose deptno is 10. vi) Remove the column deptno
from EMP table.
i. select avg(salary) from emp; 1 M each
ii. select ename from emp where city=’Mumbai’ or
city=’Pune’;
iii. update emp set salary=50000 where ename=’Ramesh’;
iv. select ename from emp where salary<50000;
v. delete from emp where deptno=10;
vi. alter table emp drop column deptno;
10 Write SQL queries for following. Consider table stud (roll no, W-22 6
name, subl, sub2, sub3) i) Display names of student who got
minimum mark in subl. ii) Display names of students who got
above 40 marks in sub2. iii) Display count of Students failed in
sub2. iv) Display average marks of subl of all students. v)
Display names of students whose name start with 'A' by
50
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
arranging them in ascending order of subl marks. vi) Display
student name whose name ends with h' and subject 2 marks ar6
i. select name from stud where sub1= (select min(sub1) from 1 M each
stud);
ii. select name from stud where sub2>40;
iii. select count(*) from stud where sub2<40;
iv. select avg(sub1) from stud;
v. select name from stud where name like 'A%' order by sub1;
vi. select name from stud where name like '%h' and sub2
between 60 and 75;
11 Write SQL queries for following. 1) Create user named 'user1' W-22 6
having Password '1234 ii) Assign 'insert' and update' Privilege
to 'userl". ii) Remove update Privilege assigned to the userl. iv)
Assign the resource Pemission to userl.
i. create user user1 identified by 1234; 1½M
ii. grant insert, update on system.emp to user1; each
iii. revoke update on system.emp from user1;
iv. grant create session, unlimited tablespace, create table to
user1;
DML commands:
- used to insert new row into table
- used to delete a row from the table
– used to modify data in the table.
– used to view data from a table.
52
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
1) Insert : For adding records in table:
We use Insert into command for adding/inserting data into
table.
Syntax:
Insert into <table name> values (value1, value2, value3…);
Ex:
insert into Student values(1,‟xyz‟,‟12-Jan-1990‟,88,10);
OR
Ex:Insert into
Studentvalues(&R_No,‟&Name‟,‟&DOB‟,&Percentage,&D
_No);
2) Update : For modifying records in table
We use update command for modifying data of a table.
Syntax:
Update <table name>set columnname1=expression where
<condition>;
Ex:
update Student set DOB=‟22-feb-1995‟ where R_No=3;
3) Delete : For deleting records from table:
We use delete command for deleting data of a table.
Syntax:
Delete from <table name>[where <condition>];
Ex:
delete from Student where R_No=2;
4) Select : For viewing the records from table
We use select command for displaying or viewing the records
from a table or
tables.
Syntax:
Select <*/ list of columns> from <table name> [where
<condition> ] ;
Ex :
Select * from Student;
Select R_N0,Name,DOB from Student where Percentage > 80;
5 Explain any two types of join with example. S-23 6
JOIN: Explanati
A SQL join is an instruction to combine data from two sets of on of any
data (i.e. two two types
tables). A with
JOIN clause is used to combine rows from two or more tables, example
based on a 3M each
related column between them. SQL Join types are as follows:
1) EQUI JOIN:
A join which is based on equalities is called equi join. In equi
join comparison
operator “=” is used to perform a Join.
Syntax:
SELECT
tablename.column1_name,tablename.column2_name
53
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
FROM table_name1,table_name2
where
table_name1.column_name=table_name2.column_name;
Example:
Select stud_info.stud_name, stud_info.branch_code,
branch_details.location
From stud_info, branch_details
Where Stud_info.branch_code=branch_details.branch_code;
2) SELF JOIN:
The SQL SELF JOIN is used to join a table to itself, as if the
table were two
tables,temporarily renaming at least one table in the SQL
statement.
Syntax:
SELECT a.column_name, b.column_name
FROM table1 a, table1 b
WHERE a.common_filed = b.common_field;
Example:
Select x.stud_name, y.stud_name
from stud_info x, stud_info y
Where x.leader= y.stud_id;
3) INNER JOIN:The INNER JOIN command returns rows that
have
matching values in both tables.
Syntax:
Select column1_name,column2_name
from table1name INNER JOIN table2name
on table1name.columnname= table2name.columnname;
Example:
Select last_name, department_name
from employees inner join departments
on employees.department_id = departments.department_id;
4) LEFT OUTER JOIN:
A left outer join retains all of the rows of the “left” table,
regardless of whether there is a row that matches on the “right”
table.
Syntax:
Select column1_name,column2_name
from table1name any_alias1 ,table2name any_alias2
on any_alias1.columnname(+) = any_alias2.columnname;
OR
Select column1_name,column2_name from table1name left
outer join table2name
on table1name.columnname= table2name.columnname;
Example:
select last_name, department_name
from employees e, departments d
on e.department_id(+) = d.department_id;
OR
select last_name, department_name
54
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
from employees left outer join departments
on employees.department_id = departments.department_id;
5) RIGHT OUTER JOIN:
A right outer join retains all of the rows of the “right” table,
regardless of whether there is a row that matches on the “left”
table.
Syntax:
Select column1name, column2name from table1name
any_alias1, table2name any_alias2
on any_alias1.columnname =any_alias2.columnname (+);
OR
Select column1name, column2name from table1name
any_alias1 right outer join table2 name any_alias2
on any_alias1.columnname =any_alias2.columnname;
6 Explain view with example S-23 4
A view is a logical table based on another table.View logically Explanati
represents subsets of data from one or more tables .A view on
contains no data of it‟s own but is like a window through which 2M
data from tables can be viewed or changed .The tables on which Example
the view is based are called base tables.SQL functions, where, 2M
join statements can be added to a view and the data in it
can be presented as if it were from one table.The database
engine recreates the data, using the view‟s SQL statement,
every time a user queries a view. A view can be updated using
the create or replace view command. For deleting a view,
dropquery can be used.General syntax to create a view:
create view viewname as select query.
Example:
create view vw_student as select stud_id, stud_name,ssc_per
from
student;
56
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
to the database.
The ROLLBACK command can only be used to undo
transactions since the last
COMMIT or ROLLBACK command was issued.
The syntax for ROLLBACK is:
ROLLBACK TO SAVEPOINT_NAME;
OR
ROLLBACK;
OR
ROLLBACK WORK;
Example:
SQL>ROLLBACK;
5 Write and explain create, update and drop view commands W-23 4
A view is a Virtual Table that is based on the result set of an Create
SQL query. command
Create View: 2M (syntax
A view is created with the CREATE VIEW statement. 1M,
Syntax: explanation
Create view view_name As ½ M,
Select column1, column2… example ½
From table_name M)
Where condition; Update
Example command
Create view mumbai_customers AS 1M (syntax
Select customer_name, contact_name ½ M,
From customers example ½
Where city=’Mumbai’; M)
Update View: Drop1M
A view can be updated with the CREATE OR REPLACE (syntax
VIEW statement. ½ M,
Syntax: example ½
CREATE OR REPLACE VIEW view_name AS M)
SELECT column1, column2, ...
FROM table_name WHERE condition;
Example:
The following SQL adds the "City" column to the "USA
Customers" view:
CREATE VIEW [USA Customers] AS
SELECT CustomerID, CustomerName, City
FROM Customers WHERE Country = ‘USA’;
Drop view:
A view can be deleted with the DROP VIEW statement.
Syntax:
DROP VIEW view_name;
Example:
DROP VIEW [USA Customers];
6 Explain cursor with syntax for declaring ,opening and closing W-23 4
a cursor
57
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
A cursor is a temporary work area created in system memory Cursor
when an SQL statement is executed. A cursor is a set of rows Explanation
together with a pointer that identifies a current row. It 1M
is a database object to retrieve data from result set on row at a Declaration
time. Implicit cursor: these types of cursors are generated and of cursor
used by the system during the manipulation of a DML query. 1M
An implicit cursor is also generated by the system when a Open cursor
single row is selected by a SELECT command. Programmers 1M
cannot control the implicit cursors. Closing
Explicit cursor: this type of cursor is created by the user when cursor 1M
the select command returns more than one row, and only one
row is to be processed at a time. An explicit
cursor can move from one row to another in a result set. An
explicit cursor uses a pointer that holds the record of a row.
1) Declaring cursor:
It defines the cursor with a name and the associated SELECT
statement.
Syntax
CURSOR name IS SELECT statement;
2) Open the cursor: It is used to allocate memory for the cursor
and make it easy to fetchthe rows returned by the SQL
statements into it.
Syntax
OPEN cursor_name;
3) Close the cursor:
It is used to release the allocated memory. The following
syntax is used to close the
above-opened cursors.
Syntax:
Close cursor_name;
7 Write the SQL queries for following EMP table. Emp (empno, W-23 6
deptno, ename, salary, designation, city.) i)list employees with
having alphabate ‘A’ as second letter in their name
II) set salary of all project leader to 50,000
III) display average salary of all employess
Iv) display employee-id of employees who live in city ‘pune’
or nagpur
V) Display employee name ,salary and city from ‘hr’
department who having salary greater than 70,000
Vi)display details of all employee who’s salary same as that of
salary of shashank
1. select * from Emp where emp_name like ‘_A%’; 1M each
2. update Emp set salary=50000 where designation=’project
leader’;
3. select avg(salary) from Emp;
4. select emp_id from Emp where city= ‘Pune’ or
city=’Nagpur’;
5. select emp_name,salary,city from Emp where dept=’HR’
and salary > 70000;
58
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
6. select * from Emp where salary in (select salary from Emp
where
emp_name=’Shashank’);
8 Consider following schema: W-23 6
Bookmaster (bookid,bookname,author,no _of copies,price)
write down sql queries for following :
I) Write a query to create table Book _master table
II) Write a command to create composite Index on book master
table
III) Write a command to drop above index
IV) Display all books whose number of copies are less than 10
V) Display all books whose price is above rs 700
VI) Create synoym for relation book master as book
information
1. create table Book_Master(book_id number(4,0), book_name 1M each
varchar(50), author
varchar(50), no_of_copies(2,0), price number(6,2));
2. create index composite_book_master on
Book_Master(book_id, book_name);
3. drop index composite_book_master;
4. select book_name,author from Book_Master order by price
desc;
5. select * from Book_Master where no_of_copies >50;
6. create or replace public synonym Book_Information for
Book_Master;
9 Write a command to create table Customer (Custo_id, W-23 6
Cust_name, address, Contact_no, City). And write down the
queries for the following: i) Insert one row into table. ii) Save
the data. iii) Insert Second row into the table. iv) Create save
point S1. v) Insert one row into the table
1. create table Customer(Custo_id number(2,0), Cust_name 1 M each
varchar(30), address
varchar(50), Contact_no varchar(10), city varchar(30));
2. insert into Customer values(1,’Ramesh’,’Ganesh Peth’,
‘9123456780’,’Pune’);
3. commit;
4. insert into Customer values (2,’Suresh’,’Sadashiv Peth’,
‘8123456790’,’Pune’);
5. savepoint s1;
6. insert into Customer values (3,’Rajesh’,’Shukrawar Peth’,
‘8912345670’,’Pune’);
Features
GROUP BY clause is used with the SELECT statement.
In the query, the GROUP BY clause is placed after the
WHERE clause.
In the query, the GROUP BY clause is placed before the
ORDER BY clause if used.
In the query, the Group BY clause is placed before the Having
clause.
Place condition in the having clause.
Syntax:
FROM table_name
WHERE condition
60
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
CREATE TABLE student (
name VARCHAR(50),
year INT,
subject VARCHAR(50)
);
INSERT INTO student (name, year, subject) VALUES
('Alice', 1, 'Mathematics'),
('Bob', 2, 'English'),
('Charlie', 3, 'Science'),
('David', 1, 'Mathematics'),
('Emily', 2, 'English'),
('Frank', 3, 'Science')
4 Write and explain syntax for creating and dropping synonyms S-24 4
with example
A synonym is an alternative name for objects such as tables,
views, sequences, stored procedures, and other database
objects.
You generally use synonyms when you are granting access to
an object from another schema and you don't want the users to
have to worry about knowing which schema owns the object.
Create Synonym (or Replace)
You may wish to create a synonym so that users do not have to
prefix the table name with the schema name when using the
table in a query.
Syntax
The syntax to create a synonym in Oracle is:
CREATE [OR REPLACE] [PUBLIC] SYNONYM [schema .]
synonym_name
FOR [schema .] object_name [@ dblink];
OR REPLACE
Allows you to recreate the synonym (if it already exists)
without having to issue a DROP synonym command.
PUBLIC
It means that the synonym is a public synonym and is
accessible to all users. Remember though that the user must
first have the appropriate privileges to the object to use the
synonym.
schema
The appropriate schema. If this phrase is omitted, Oracle
assumes that you are referring to your own schema.
object_name
The name of the object for which you are creating the synonym.
It can be one of the following:
table
view
sequence
stored procedure
function
61
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
package
materialized view
java class schema object
user-defined object
synonym
Example
Let's look at an example of how to create a synonym in Oracle.
For example:
SELECT *
FROM suppliers;
If this synonym already existed and you wanted to redefine it,
you could always use the OR REPLACE phrase as follows:
Syntax
The syntax to drop a synonym in Oracle is:
62
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
6 Explain any four joins if SQL Example S-24 4
JOIN:
A SQL join is an instruction to combine data from two sets of
data (i.e. two
tables). A JOIN clause is used to combine rows from two or
more tables, based on a related column between them. SQL
Join types are as follows:
1) EQUI JOIN:
A join which is based on equalities is called equi join. In equi
join comparison
operator “=” is used to perform a Join.
Syntax:
SELECTtablename.column1_name,tablename.column2_name
FROM table_name1,table_name2
Where
table_name1.column_name=table_name2.column_name;
Example:
Select stud_info.stud_name, stud_info.branch_code,
branch_details.location
From stud_info, branch_details
Where Stud_info.branch_code=branch_details.branch_code;
2) SELF JOIN:
The SQL SELF JOIN is used to join a table to itself, as if the
table were two
63
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
tables,temporarily renaming at least one table in the SQL
statement.
Syntax:
SELECT a.column_name, b.column_name
FROM table1 a, table1 b
WHERE a.common_filed = b.common_field;
Example:
Select x.stud_name, y.stud_name
from stud_info x, stud_info y
Where x.leader= y.stud_id;
3) INNER JOIN:The INNER JOIN command returns rows that
have
matching values in both tables.
Syntax:
Select column1_name,column2_name
from table1name INNER JOIN table2name
on table1name.columnname= table2name.columnname;
Example:
Select last_name, department_name
from employees inner join departments
on employees.department_id = departments.department_id;
4) LEFT OUTER JOIN:
A left outer join retains all of the rows of the “left” table,
regardless of whether
there is a row that matches on the “right” table.
Syntax:
Select column1_name,column2_name
from table1name any_alias1 ,table2name any_alias2
on any_alias1.columnname(+) = any_alias2.columnname;
OR
Select column1_name,column2_name
from table1name left outer join table2name
on table1name.columnname= table2name.columnname;
Example:
select last_name, department_name
from employees e, departments d
on e.department_id(+) = d.department_id;
OR
select last_name, department_name
from employees left outer join departments
on employees.department_id = departments.department_id;
5) RIGHT OUTER JOIN:
A right outer join retains all of the rows of the “right” table,
regardless of
whether there is a row that matches on the “left” table.
Syntax:
Select column1name, column2name
from table1name any_alias1, table2name any_alias2
on any_alias1.columnname =any_alias2.columnname (+);
OR
64
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
Select column1name, column2name
from table1name any_alias1 right outer join table2 name
any_alias2
on any_alias1.columnname =any_alias2.columnname;
7 I) Write a query to create table emp- S-24 6
details(EMP_ID,name,dept) with proper data types and
Emp_ID as primary key and name as NOT NULL constrain
II) Perform all DML command on above created table
10 S-24 6
WRITE SQL COMMAND FOR Following
I) Create user
II) Grant privileges to user
III) Remove privileges to user
i) Create user
CREATE USER <username> IDENTIFIED BY <password>;
(OR)
CREATE USER RAJ IDENTIFIED BY RAJ123;
ii) Grant privileges to user.
GRANT <privilege list> ON <relation name or view name>
TO<user list>;
(OR)
(assuming table Employee for granting permissions to user
‘RAJ’
for select, insert, update and delete privilege)
GRANT SELECT, INSERT,UPDATE,DELETE ON
EMPLOYEE TO RAJ;
Iii) Remove privileges from user.
REVOKE <privilege list> ON <relation name or view name >
FROM <user list>;
(OR)
66
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE