Sie sind auf Seite 1von 15

Sql queries using

Sql queries using ROLL NO. 14113063(DALJIT) Lab 1: "EMP_ID" NUMBER(4,0), "EMP_NAME" VARCHAR2(20),
ROLL NO. 14113063(DALJIT) Lab 1: "EMP_ID" NUMBER(4,0), "EMP_NAME" VARCHAR2(20), "SALARY"
ROLL NO. 14113063(DALJIT)
Lab 1:
"EMP_ID" NUMBER(4,0),
"EMP_NAME" VARCHAR2(20),
"SALARY" NUMBER(8,3),
"MANAGER_ID" NUMBER(4,0),
"D_NO" NUMBER(4,0),
PRIMARY KEY ("EMP_ID") ,
FOREIGN KEY ("MANAGER_ID")
REFERENCES "EMPLOYEE" ("EMP_ID") ,
FOREIGN KEY ("D_NO")
REFERENCES "DEPARTMENT" ("DEPT_NO")
output:

Cd 1:

CREATE TABLE "EMPLOYEE"

(

)

/

Output table created sucessfully.

Cd :2

describe employee

Cd 1: CREATE TABLE "EMPLOYEE" ( ) / Output table created sucessfully. Cd :2 describe employee

DBMS LAB

Page 1

Cd 3:

Describe department//department table looks like this

Output:

department//department table looks like this Output: Note we can insert in the tables created using insert
Note we can insert in the tables created using insert command One such illustration has
Note we can insert in the tables created using insert command
One such illustration has been made for a tuple of the relation employee
Like this:
Insert into employee values(1,’daljit singh’,23098.65,NULL,2)
Select * from employee
This how we add data to our tables(relations);
In this case employee table looks like:
to our tables(relations); In this case employee table looks like: And the department table looks like:

And the department table looks like:

DBMS LAB

Page 2

Query 1: Select * from employee where emp_name like “%ingh” Result: Query 2: select *
Query 1: Select * from employee where emp_name like “%ingh” Result: Query 2: select *
Query 1:
Select * from employee where emp_name like “%ingh”
Result:
Query 2: select * from employee where manager_id IS NULL

Query 3: select * from employee where salary between 12221 and 60000

Query :select salary from employee where d_no=(

Select d_no from employee where emp_id=5)

12221 and 60000 Query :select salary from employee where d_no=( Select d_no from employee where emp_id=5)

DBMS LAB

Page 3

Problem:To display employee id,name and department name of all Employees

Query: select emp_id,emp_name,dept_no from employee cross join department where employee.d_no=department.dept_no

cross join department where employee.d_no=department.dept_no Problem: To display details of the manager of the employee
cross join department where employee.d_no=department.dept_no Problem: To display details of the manager of the employee

Problem: To display details of the manager of the employee with employee id=3.

Query: select * from employee where emp_id =(select manager_id from employee where

emp_id=3)

Problem:to display the average salary of all employees

Query: select avg(salary) from employee

Problem to Print the details of the department whose employee has the maximum salary

Query: select * from department where dept_no=(select d_no from employee where employee.salary=(select max(salary) from employee)

DBMS LAB

Page 4

Using initcap() sql function Query: select initcap (emp_name) from employee Result: Query: creating views Cmd:

Using initcap() sql function Query: select initcap (emp_name) from employee

Result: Query: creating views Cmd: create view friends as select d_no,salary from employee Desc friends
Result:
Query: creating views
Cmd: create view friends as select d_no,salary from employee
Desc friends
Result:
Query: creating views Cmd: create view friends as select d_no,salary from employee Desc friends Result: DBMS

DBMS LAB

Page 5

Lab 2

Number functions in sql

1. Floor()

select floor (salary ) from friends

result:

sql 1. Floor() select floor (salary ) from friends result: Similarly ceil() for ceiling values 2.Mod()
Similarly ceil() for ceiling values 2.Mod() select mod(salary,10) from friends result: //prints 10% of the
Similarly ceil() for ceiling values
2.Mod()
select mod(salary,10) from friends
result: //prints 10% of the salary from friends view
3. Logarithmic natural function
Query:SELECT LN(95) "Natural log of 95" FROM DUAL;
Result:
LN(95) "Natural log of 95" FROM DUAL; Result: 4. Power function Query: SELECT POWER(3,2) "Raised"

4. Power function

Query: SELECT POWER(3,2) "Raised" FROM DUAL;

DBMS LAB

Page 6

5. Max() function Query: select max(salary) from employee Result: Similar functins are median(),cos(),min(),sum(),avg()

5. Max() function

Query: select max(salary) from employee

Result:

function Query: select max(salary) from employee Result: Similar functins are median(),cos(),min(),sum(),avg() Lab
Similar functins are median(),cos(),min(),sum(),avg() Lab queries Result: Query: select avg(salary), count(distinct
Similar functins are median(),cos(),min(),sum(),avg()
Lab queries
Result:
Query: select avg(salary), count(distinct manager_id )from employee
Result:

Query: select avg(salary),emp_name from employee where d_no=(select dept_no from department where location like '%jalandhar%')

from department where location like '%jalandhar%') Query: select manager.manager_id,employee.* from

Query: select manager.manager_id,employee.* from manager,employee where manager.manager_id=employee.emp_id

Result:

DBMS LAB

Page 7

Query: select d_no,count(*)from employee group by d_no having count(*)>1 Result: Some more queries e.g select

Query: select d_no,count(*)from employee group by d_no having count(*)>1

Result:

Some more queries e.g select dept_no||dept_name from department Result: Query to find the length of
Some more queries
e.g select dept_no||dept_name from department
Result:
Query to find the length of each employee name
select length(emp_name),dept_name from employee,department where
department.dept_no=employee.d_no

For concatenation of two or more attributes use || operator

For concatenation of two or more attributes use || operator Query: To print the dno and

Query: To print the dno and count of employee not from department 1 and 3

DBMS LAB

Page 8

select d_no,count(*) from employee group by d_no having count(*)>1 and d_no not in(1 ,3)

group by d_no having count(*)>1 and d_no not in(1 ,3) Lab 3 Using intersection minus and

Lab 3 Using intersection minus and union

salary>5000 salary>5000;
salary>5000
salary>5000;

Problem-Find out details of the employee who works for dept1 or having salary more than 5000 using union

Query: select * from employee where d_no in(1) union select * from employee where

Problem: Find out details of the employee who works for dept1 or having salary more than 5000 using intersection

Query: select * from employee where d_no=1 intersect select * from employee where

Problem-Find out the details of the employees where deptnumber is 1 or 2 using union and minus

Query- select * from employee minus select * from employee where dno not in(1) union select * from employee minus select * from employee where dno not in(2) union select * from employee minus select * from employee where dno not in(3)

DBMS LAB

Page 9

Applying various joins Problem:Apply left outer join on employee to dept Problem:Apply right outer join
Applying various joins Problem:Apply left outer join on employee to dept Problem:Apply right outer join
Applying various joins
Problem:Apply left outer join on employee to dept
Problem:Apply right outer join on employee to dept
Query: select * from employee right outer join department on
employee.d_no=department.dept_no

Query: select * from employee left outer join dept on employee.dno=dept.dno

from employee left outer join dept on employee.dno=dept.dno Problem: Apply inner join on employee to dept
from employee left outer join dept on employee.dno=dept.dno Problem: Apply inner join on employee to dept

Problem:

Apply inner join on employee to dept

Query: select * from employee inner join department on employee.d_no=department.dept_no

DBMS LAB

Page 10

Problem: Apply natural join on employee to dept select * from employee natural join department
Problem: Apply natural join on employee to dept select * from employee natural join department
Problem: Apply natural join on employee to dept
select * from employee natural join department
Problem: Apply natural join on employee to dept select * from employee natural join department DBMS

DBMS LAB

Page 11

LAB 4

PROCEDURAL SQL CONCEPTS

Procedural sql contains declaration block declaration block ,executable block which contains the commands to be executed and a block for exception handling.

->A simple example calculating the area of the circle declare p constant number(4,2):=3.14; radius number(4,2);
->A simple example calculating the area of the circle
declare
p constant number(4,2):=3.14;
radius number(4,2);
ar number(10,2);
begin
radius:=:a;
ar:=p*power(radius,2);
insert into area values(radius,ar);
dbms_output.put_line('area is'||ar);
end;
output:For input as 3
Here the table area is initially created that is:
Create table area(radius number(4,2),ar number(10,2))
After excecuting the above code the table data is:

Using if section

Format:

If

Then

Else if

DBMS LAB

Page 12

Then

.

Endif;

An example: To find the greatest to three numbers

declare

a number(2); b number(2); c number(2); begin a:=:ad; b:=:bd; c:=:cd; if a>b then if a>c
a number(2);
b number(2);
c number(2);
begin
a:=:ad;
b:=:bd;
c:=:cd;
if a>b
then
if a>c
then
dbms_output.put_line('a is largest');
else
dbms_output.put_line('c is largest');
end if;
else
if c>b
then
dbms_output.put_line('c is largest');

else

dbms_output.put_line('b is largest');

end if;

end if;

end;

DBMS LAB

Page 13

Output:

Output: (Provided a being given largest value) Using while loop Problem: print the area of circle

(Provided a being given largest value)

Using while loop

(Provided a being given largest value) Using while loop Problem: print the area of circle continuously

Problem: print the area of circle continuously with taking radius 1 to 10

declare

p constant number(4,2):=3.14;

radius number(4,2);

ar number(10,2);

begin

radius:=1;

while radius<=10 loop

ar:=p*power(radius,2);

insert into area values(radius,ar);

dbms_output.put_line('area is'||ar);

radius:=radius+1;

end loop;end;

Output:

values(radius,ar); dbms_output.put_line('area is'||ar); radius:=radius+1; end loop;end; Output: DBMS LAB Page 14

DBMS LAB

Page 14

Contents of area table becomes:

Contents of area table becomes: 10 loop Using for loop Problem: print the area of circle
10 loop
10
loop

Using for loop

Problem: print the area of circle continuously with taking radius 1 to 10

declare

p constant number(4,2):=3.14;

radius number(4,2);

ar number(10,2);

begin

radius:=1;

for radius in 1

ar:=p*power(radius,2);

dbms_output.put_line('area is'||ar);

end loop;

end;

Output:

DBMS LAB
DBMS LAB

Page 15