0% found this document useful (0 votes)
56 views21 pages

DBMS HIMANSHU MANDAL - Removed

The document discusses different SQL commands categorized into DDL, DQL, DML, DCL and TCL. It provides the theory and lists of commands for DDL, DQL and DML. DDL commands are used to define the database schema and structure. DQL commands are used to query and retrieve data. DML commands are used to manipulate and manage the data.

Uploaded by

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

DBMS HIMANSHU MANDAL - Removed

The document discusses different SQL commands categorized into DDL, DQL, DML, DCL and TCL. It provides the theory and lists of commands for DDL, DQL and DML. DDL commands are used to define the database schema and structure. DQL commands are used to query and retrieve data. DML commands are used to manipulate and manage the data.

Uploaded by

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

PRACTICAL 3

Aim=Apply Following Compound condition and use relational


operators (IN, BETWEEN, LIKE, NULL, NOT NULL etc) in SQL statements
on “Employees” and“Department” table.

1. Display the last name and salary for any employee whose salary is not in the
range of 5,000 to 1,000?
Query=select ename,sal from emp where sal not between 1500 and 10000;

2. Display the last name and department number of all employees in departments 60 or 100 in
ascending alphabetical order by name.
Query=select ename,deptno from emp where deptno=10 or deptno=40 order by ename;

3. To display the last name and salary of employees who earn between 5,000 and 12,000 and are in
department 60 or 90. Label the columns Employee and Monthly Salary.
Query=select ename,sal from emp where sal between 2000 and 10000 and deptno=10 or
deptno=30;

4. Create a report to display the last name, salary, and commission of all employees who earn
commissions. Sort data in descending order of salary and commissions.
Query=select ename,sal comm from emp where comm is not null order by sal,comm desc;

EN21CS301318 HIMANSHU MANDAL 11


5. Display the last name, job id, and salary for all employees whose job is IT-programmer or
finance manager and whose salary is not equal to 2,500, 3,500, or 7,000.
Query=

6. Display the last name, salary, and commission for all employees whose commission amount is
20%.
Query=select ename,sal comm from emp where comm =(0.1875*sal);

7. Display the employee number, last name, salary, and salary increased by 15.5% (expressed as a
whole number) for each employee. Label the column New Salary.
Query=select ename,sal,empno,(sal+sal*.155) as newsal from emp ;

8. Modify above query to add a column that subtracts the old salary from the new salary. Label the
column Increase.
Query=select ename,sal,empno,round(sal+sal*.155-sal) as increase from emp;

9. Create a query that produces the following for each employee: employee last name and salary is
3 times. Label the column Dream Salaries.
Query=select ename,(sal*3) as DreamSalary from emp;

EN21CS301318 HIMANSHU MANDAL 12


10. Display the last name and manager id of those employees whose manager id is not 103
Query=.select ename,mgr from emp where mgr!=7566;

11. Create a query to display the contents of those employees whose manager id is in the range of
100 and 103.
Query=select * from emp where mgr between 7600 and 7700;

12. Create a query to display the contents of those employees whose commission pct is null.
Query=select * from emp where comm is null;

13. Create a query to display the contents of those employees whose manager id is not null from
the department table.
Query=select * from emp where mgr is not null ;

14. Create a query to see a listing of all rows for which manager _id is not 200. Aggregate f
un cti ons.
Query=select * from emp where mgr !=7698 ;

EN21CS301318 HIMANSHU MANDAL 13


15. Display sum of salary of employees working in the department 60.
Query=select sum(sal) from emp where deptno=20 ;

16. Compute the difference between minimum and maximum salary.


Query=select max(sal)-min(sal) from emp;

17. How many different job id are stored in the relation employees.
Query=select count(distinct(empno)) from emp;

18. How many tuples are stored in the relation employees? Also find the no. of records in
manager_id attribute. String matching queries.
Query=select count(*),count(mgr) from emp;

19. Display the content of the employee from the employees table whose first name ends with
letter n.
Query=select * from emp where ename like '%N';

20. Display the content of the employee from the employees table whose first name starts with
letter A.
Query=select * from emp where ename like 'A%';

EN21CS301318 HIMANSHU MANDAL 14


21 . Display the content of the employee from the employees table whose first name starts with A
and ends with letter r.
Query=select * from emp where ename like 'A%N';

22. Display the content of the employee from the employees table whose first name starts with
letter A and it is 5 characters long.
Query=select * from emp where ename like 'A ';

23. Display the content of the employee from the employees table whose first name has substring
‘a r’.
Query=select * from emp where ename like '%AR%';

24. Display the content of the employee from the employees table whose first name end with the
letter ‘ara’.
Query=select * from emp where ename like '%ARD';

25. Display the content of the employee from the department table whose first name has
substring ‘tr’ and manager id is either 200 or 1700.
Query=select * from emp where ename like 'AR%'and mgr=7600 or mgr=7839;

26. Display the content like employee_id,length of the last_name,manager_id,department_id,and


first letter if the first name should be capital.),hire_date from employees.

Query=select empno,length(ename),mgr,deptno,hiredate from emp;

EN21CS301318 HIMANSHU MANDAL 15


27. Display the last name, first name, manager id and email of those employees whose email
contains exactly one character that appears between E and S.
Query= select ename,mgr,deptno,hiredate from emp where job like '%L_S%';

28. Display all employee last names in which the third letter of the name is a.

Query=select * from emp where ename like ' A%';

29. Display the last name of all employees who have both an a and an e in their last name.

Query=select * from emp where ename like '%A_E%';

30. Write a query that displays the last name (with the first letter uppercase and all other letters
lowercase) and the length of the last name for all employees whose name starts with the letters J,
A, or M.. Sort the results by the employees’ last names

Query=select initcap(ename),length(ename) from emp where ename like 'B%' or ename like 'A%'
order by ename;

EN21CS301318 HIMANSHU MANDAL 16


Practical 4

Aim=Study of different commands used in Data Definion and Data Manipulaon


Languages.(Only write Theory of DDL and DML,Lists the commands and write the syntax of each
command)

Structured Query Language(SQL) as we all know is the database language by the use of which we can perform certain
operaons on the exisng database and also we can use this language to create a database. SQL uses certain
commands like Create, Drop, Insert, etc. to carry out the required tasks.

These SQL commands are mainly categorized into five categories as:

a) DDL – Data Definion Language


b) DQL – Data Query Language
c) DML – Data Manipulaon Language
d) DCL – Data Control Language
e) TCL – Transacon Control Language

DDL (Data Definion Language):

Data Definion Language actually consists of the SQL commands that can be used to define the
database schema. It simply deals with descripons of the database schema and is used to create
and modify the structure of database objects in the database. DDL is a set of SQL commands used
to create, modify, and delete database structures but not data. These commands are normally not
used by a general user, who should be accessing the database via an applicaon.

List of DDL commands:

1 CREATE: This command is used to create the database or its objects (like table, index, funcon, views, store
procedure, and triggers).
. DROP: This command is used to delete objects from the database.
ALTER: This is used to alter the structure of the database.
2 TRUNCATE: This is used to remove all records from a table, including all spaces allocated for the records
are removed.
. COMMENT: This is used to add comments to the data diconary.
RENAME: This is used to rename an object exisng in the database.
.3
4
DQL. (Data Query Language):

5
DQL statements are used for performing queries on the data within schema objects. The purpose of
the.DQL Command is to get some schema relation based on the query passed to it. We can define
DQL6 as follows it is a component of SQL statement that allows getting data from the database and
imp.osing order upon it. It includes the SELECT statement. This command allows getting the data out
of the database to perform operations with it. When a SELECT is fired against a table or tables the
result is compiled into a further temporary table, which is displayed or perhaps received by the
program i.e. a front-end.

EN21CS301318 HIMANSHU MANDAL 17


List of DQL:
SEL ECT:It is used to retrieve data from the database.

DML(Data Manipulaon Language):

The SQL commands that deals with the manipulation of data present in the database belong to DML
or Data Manipulation Language and this includes most of the SQL statements. It is the component
of the SQL statement that controls access to data and to the database. Basically, DCL statements are
grouped with DML statements.
List of DML commands:
INSERT: It is used to insert data into a table.
UPDATItEi:s used to update existing data within a table.
DELETE : It is used to delete records from a database table.
LO CK:Table control concurrency.
CALL: Call a PL/SQL or JAVA subprogram.
EXPLAIItNdePsLcAriN
b:es the access path to data.

DCL (Data Control Language):

DCL includes commands such as GRANT and REVOKE which mainly deal with the rights, permissions,
and other controls of the database system.
List of DCL commands:
GRANT: This command gives users access privileges to the database.
REVOKE: This command withdraws the user’s access privileges given by using the GRANT
command.

TCL (Transacon Control Language):

Transactions group a set of tasks into a single execution unit. Each transaction begins with a specific
task and ends when all the tasks in the group successfully complete. If any of the tasks fail, the
transaction fails. Therefore, a transaction has only two results: success or failure. You can explore
more about transactions here. Hence, the following TCL commands are used to control the execution
of a transaction:

BEGIN:Opens a Transaction.
COMMICTo:mmits a Transaction.
ROLLBARC
oKllb: acks a transaction in case of any error occurs.
SAVEPOINsTa: save point within a transaction.
Se t
SET TRANSACTIOS Np: eciies characteristics for the transaction.

EN21CS301318 HIMANSHU MANDAL 18


Practical 5

Aim= Creang new tables, Adding data, updang data, altering tables, deleng data.

1. Create a table called EMP with the following structure.


Name Type

EMPNO NUMBER(6)
ENAME VARCHAR2(20)
JOB VARCHAR2(10)
DEPTNO NUMBER(3)
SAL NUMBER(7,2)

query=
create table empp(
emp_no int ,
emp_Name varchar(20),
emo_job varchar(10) primary key,
deptno int,
sal int
);

Output=

2: Add a column experience to the emp table. experience numeric null allowed.

Query=alter table empp

3: Modify the column width of the job field of emp table.


query=alter table emply modify emp_name varchar(80);

4: Create dept table with the following structure.

EN21CS301318 HIMANSHU MANDAL 19


Name Type

DEPTNO NUMBER(2)
DNAME VARCHAR2(10)
LOC VARCHAR2(10)
Deptno as the primary key

5: create the emp1 table with ename and empno, add constraints to check the empno value while
entering (i.e) empno > 10
query=
create table empiy(
emp_no int ,
emp_Name varchar(20),

check(emp_no>100)
);

6: drop a column experience to the emp table.


query=alter table emply drop column emp_no ;

output=

7: Truncate the emp table and drop the dept table


query=truncate table emply;
output=

8. Create a table “Products” with column name given below using CREATE TABLE command;
query=create table product(
pro_id int,
pro_name varchar(80),
stock_qly int,
dep_name varchar(10)
);
insert into product values(101,'keyword',15,'IT');
insert into product values(102,'mouse',16,'IT');
insert into product values(103,'monitor',11,'CS');
insert into product values(104,'printer',20,'CS');
insert into product values(105,'switch',15,'IT');
insert into product values(106,'router',04,'IT');
insert into product values(107,'machine',02,'store');
select * from product;

output=

EN21CS301318 HIMANSHU MANDAL 20


9. Add the Price Attribute to the above tablE
eNa2n1d
CSi3
n0s1e3r0
t 5values into it.
query=alter table product add price int;
output=

10. Modify the record of a product whose product name is “Monitor” with an increase in stock
quantity as 30.
query=update product set stock_qly=30 where pro_name='monitor';

11. Modify the record of a product whose product id is 103 with an increase in price like 3000
query=update product set price=3000 where pro_id=103;

12. Modify the record of the product whose department is stored with change in stock quantity as
04
query=update product set stock_qly=04 where dep_name='store';

13. Delete the record of Product whose product id is 102 from the table.
query=delete from product where pro_id=102;

14. Add a record for a new product whose product name is Scanner.
query=insert into product values(108,'scanner',20,'CS',1000);

15. Add the attribute Specification into the “Products” Table and insert values for it.
query=alter table product1 add specification varchar(80);

HIMANSHU MANDAL EN21CS301318 21


Practical 7

Aim= Perform operaons like Natural Join, equijoin, le outer join, right outer join, full outer join,
intersecon, union and union all on given relaons

Q.1 Create a table that are given below.


First table= course1.
Second table =Student.

create table course1(


course_id int,
roll_no int
);
insert into course1 values(1,1);
insert into course1 values(2,2);
insert into course1 values(2,3);
insert into course1 values(3,4);
insert into course1 values(1,5);
insert into course1 values(4,9);
insert into course1 values(5,10);
insert into course1 values(4,11);
select *from course1;

output=

create table student1


(
Roll_no int,
Name varchar(40),
Address varchar(80),
Phone_no int,
Age int
);
insert into student1 values(1,'Harsh','Delhi',1234,18);
insert into student1 values(2,'Pratik','Bihar',9876,19);
insert into student1 values(3,'Riyanka','Siliguri',6789,20);
insert into student1 values(4,'Deep','Ramnagar',7654,18);
insert into student1 values(5,'Ayush','Kolkata',2816,19);
insert into student1 values(6,'Dhanashri','Indore',9898,19);
insert into student1 values(7,'Rohan','Harda',7654,19);
insert into student1 values(8,'Neeraj','Alirajpur',8765,19);

HIMANSHU MANDAL EN21CS301318 22


select * from student1;

output=

Inner join
Q.2 This query will show the names and age of students enrolled in different courses.
Query=select course1.course_id,course1.roll_no from course1 inner join student1 on
course1.roll_no = student1.roll_no;
output=

Q.4 Write query to implement theta join.

Query=select course1.course_id,course1.roll_no from course1 inner join


student1 on course1.roll_no > student1.roll_no;
output=

Q.5 Write query to implement left outer join

select course1.course_id,course1.roll_no from course1 left join student on course1.roll_no =


st ud ent1. roll_no;
output=

Q 5. Write query to implement rigth outer join


select course1.course_id,course1.roll_no from course1 right join student1 on course1.roll_no =
student1.roll_no;
output=

HIMANSHU MANDAL EN21CS301318 23


Q 6. Write query to implement full outer join
Query=select course1.course_id,course1.roll_no from course1 full join student1 on course1.roll_no
= student1.roll_no;
output=

Q.7 Write query to implement union.


Query=select course_id from course1 union select Roll_no from student1;
output=

Q.8 Write query to implement intersection.


Query=select course_id from course1 intersect select Roll_no from student1;
Output=

Q.9 Write query to implement union all.


Query=select course_id , roll_no from course1 union select Roll_no ,Age from student1;
Output=

HIMANSHU MANDAL EN21CS301318 24


Practical 8

Aim=Sub-queries: Single-Row Sub-queries, Mulple row Sub-queries, Sub queries in


other DML statements., nested queries.

1. Whose EMP job is the same as the job of ‘SMITH’?


Query: select * from emp where job=(select job from emp where ename='SMITH');

Output=

2. Whose salary is more than the max salary of the job is “SALESMAN”?
Query=select * from emp where sal > (select max(sal) from emp where job='SALESMAN');
Output=

3. Whose EMP job is same as the job of “BLAKE” and who are earning Salary more than
“?BLAKE” salary

Query=select * from emp where job=(select job from emp where ename='BLAKE') AND sal > (select
max(sal) from emp where ENAME='BLAKE');
Output=

4u.Deirsyp=lay senior EMP?


Q
SELECT * FROM EMP WHERE HIREDATE=(SELECT MIN(HIREDATE) FROM EMP);
Output=

HIMANSHU MANDAL EN21CS301318 25


5. Find the second-highest Salary from the EMP table?
Query= select max(sal) from emp where sal<(select max(sal) from emp);

Output=

6. Display EMP details who are getting the Second highest Salary in the EMP table?
seuleecrty*=from emp where sal=(select max(sal) from emp where sal<(select max(sal) from
Q
emp));

Output=

7. Display EMP details who are getting 3rd highest Salary in EMP table?
Query=select * from emp where sal=(select max(sal) from emp where sal<(select max(sal) from
emp where sal<(select max(sal) from emp)));

Output=

8. Sum of salary of jobs if the sum of salary of jobs are more than the sum of salary of the
job is ‘clerk’?
Query=select job,sum(sal) from emp group by job having sum(sal)>(select sum(sal) from EMP where
job='CLERK');

Output=

Example of Multiple Row Subquery in Oracle

9. EMP details whose EMP job is same as the job of the EMP “SMITH”, and “CLARK”?
Query=select * from emp where job IN(select job from emp where ename='SMITH' or ename='CLARK');

Output=

HIMANSHU MANDAL EN21CS301318 26


10. Display EMP details who are getting min, max salaries?
Query= select * from emp where sal IN(select max(sal) from emp union select min(sal) from emp );

Output=

11. Display all EMPs, whose salary is more than any “SALESMAN” salary?
Query=select * from emp where sal > ANY(select sal from emp wherejob='SALESMAN');

Output=

12u.eDriys=play all EMPs, whose salary is more than all “SALESMAN” salaries?
Q
select * from emp where sal > ALL(select sal from emp where job='SALESMAN');
Output=

Example Multiple Column Subquery in Oracle

13.WAQ to display EMP whose JOB, MGR is same as the JOB, MGR of the EMP “CLARK”?
Q uer y=select * from emp where(job,mgr) IN(select job,mgr from emp where ename='CLARK');

Output=

HIMANSHU MANDAL EN21CS301318 27


Practical 9

Aim=Creang views, modifying views, dropping views, inserng and updang data using view

Query related to View

1.Creating a View

SELECT * FROM EMP;


create table std5(

sname varchar(30),
sid int

);

insert into std5 values('KING',101);


insert into std5 values('CLARK',102);
insert into std5 values('nitya',103);
insert into std5 values('ford',104);

create view example2 as select ename,empno from emp ;


create view example1 as select ename,empno from emp where sal>3000;

create view example6 as select


emp.ename,std5.sname from emp,std5 where emp.ename = std5.sname;

select * from example1;

select * from example2;


select * from example6;

HIMANSHU MANDAL EN21CS301318 28


create OR REPLACE view example3 as select
ename,empno from emp with read only;
select * from example3;
insert into example2(empno,ename)
values(122,'pakhi');

drop view example1;

HIMANSHU MANDAL EN21CS301318 29


Practical 10

Aim=Study and apply different Data control Language commands like grant, revoke,
create user roles and privilages , remove privilege.

1.Grant command
Query- GRANT CONNECT, RESOURCE TO U1;

Query- GRANT INSERT, SELECT, DELETE ON STUD TO MANAGER;

2.Create role-
Query- CREATE ROLE MANAGER;

HIMANSHU MANDAL EN21CS301318 39


3.Drop Role command-
Query- Drop role manager;

4. REVOKE Command
Query- REVOKE CONNECT,RESOURCE FROM U1;

HIMANSHU MANDAL EN21CS301318 31

You might also like