PROGRAMS BASED ON STACKS
AND QUEUES
1. Program to implement stacks without
classes.
s=[]
c="y"
while (c=="y"):
print "1. PUSH"
print "2. POP "
print "3. Display"
choice=input("Enter your choice: ")
if (choice==1):
a=input("Enter any number :")
s.append(a)
elif (choice==2):
if (s==[]):
print "Stack Empty"
else:
print "Deleted element is : ",s.pop()
elif (choice==3):
l=len(s)
for i in range(l-1,-1,-1):
print s[i]
c=raw_input("Do you want to continue or not?(y/n)\n")
2. Program to implement stacks with classes.
class stack:
s=[]
def push(self):
a=input('Enter any number : ')
stack.s.append(a)
def display(self):
l=len(stack.s)
for i in range(l-1,-1,-1):
print stack.s[i]
a=stack()
c="y"
while (c=="y"):
print "1. PUSH"
print "2. POP "
print "3. Display"
choice=input("Enter your choice: ")
if (choice==1):
a.push()
elif (choice==2):
if (a.s==[]):
print "Stack Empty"
else:
print "Deleted element is : ",a.s.pop()
elif (choice==3):
a.display()
3. Program to implement Queues without
classes.
a=[]
c='y'
while c=='y':
print "1. INSERT"
print "2. DELETE "
print "3. Display"
choice=input("Enter your choice : ")
if (choice==1):
b=input("Enter new number : ")
a.append(b)
elif (choice==2):
if (a==[]):
print "Queue Empty"
else:
print "Deleted element is : ",a[0]
del a[0]
elif (choice==3):
l=len(a)
for i in range(0,l):
print a[i]
else:
print "Wrong input"
c=raw_input("Do you want to continue or not? (y/n)\n")
4. Program to implement Queues with classes.
class queue:
q=[]
def insertion(self):
a=input("Enter new number : ")
queue.q.append(a)
def deletion(self):
if (queue.q==[]):
print "Queue Empty"
else:
print "Deleted element is : ",queue.q[0]
del queue.q[0]
def display(self):
l=len(queue.q)
for i in range(0,l):
print queue.q[i]
a=queue()
c='y'
while c=='y':
print "1. INSERT"
print "2. DELETE "
print "3. Display"
choice=input("Enter your choice : ")
if (choice==1):
a.insertion()
elif (choice==2):
a.deletion()
elif (choice==3):
a.display()
else:
print "Wrong input"
c=raw_input("Do you want to continue or not? (y/n)\n")
SQL QUERIES
1. Display all records from table EMP.
SQL> select* from emp;
EMPNO ENAME
JOB
MGR HIREDATE
SAL
COMM
DEPTNO
--------- ---------- --------- --------- --------- --------- --------8877 SMITH
CLERK
7499 ALLEN
SALESMAN
7698 20-FEB-81
1600
300
30
7521 WARD
SALESMAN
7698 22-FEB-81
1250
500
30
7566 JONES
MANAGER
7654 MARTIN
SALESMAN
7698 BLAKE
7902 17-DEC-80
800
7839 02-APR-81
20
2975
20
7698 28-SEP-81
1250
MANAGER
7839 01-MAY-81
2850
30
7782 CLARK
MANAGER
7839 09-JUN-81
2450
10
7788 SCOTT
ANALYST
7839 KING
PRESIDENT
7844 TURNER
SALESMAN
7876 ADAMS
CLERK
7900 JAMES
CLERK
9999 FORD
ANALYST
7934 MILLER
CLERK
7566 19-APR-87
17-NOV-81
3000
7698 03-DEC-81
7566 03-DEC-81
7782 23-JAN-82
1500
1100
950
3000
1300
30
20
5000
7698 08-SEP-81
7788 23-MAY-87
1400
10
0
30
20
30
20
10
14 rows selected.
2. Display empno and ename of all employees
from table emp.
SQL> select empno, ename from emp;
EMPNO ENAME
--------- ---------8877 SMITH
7499 ALLEN
7521 WARD
7566 JONES
7654 MARTIN
7698 BLAKE
7782 CLARK
7788 SCOTT
7839 KING
7844 TURNER
7876 ADAMS
7900 JAMES
9999 FORD
7934 MILLER
14 rows selected.
3. Display ename, sal, sal added with comm
from emp.
SQL> select ename, sal, sal+comm from emp;
ENAME
SAL
SAL+COMM
---------- --------- --------SMITH
800
ALLEN
1600
1900
WARD
1250
1750
JONES
2975
MARTIN
1250
BLAKE
2850
CLARK
2450
SCOTT
3000
KING
5000
TURNER
1500
ADAMS
1100
JAMES
950
FORD
3000
MILLER
1300
14 rows selected.
2650
1500
4. Display ename joined with job with heading
"Employee", sal*12 as "Total Salary" from table
emp.
SQL> select ename ||job "Employee", sal*12 "Total
Salary" from emp;
Employee
Total Salary
------------------- -----------SMITHCLERK
9600
ALLENSALESMAN
19200
WARDSALESMAN
15000
JONESMANAGER
35700
MARTINSALESMAN
15000
BLAKEMANAGER
34200
CLARKMANAGER
29400
SCOTTANALYST
36000
KINGPRESIDENT
60000
TURNERSALESMAN
18000
ADAMSCLERK
13200
JAMESCLERK
11400
FORDANALYST
36000
MILLERCLERK
15600
14 rows selected.
5. Display distinct sal of employees from table
emp.
SQL> select distinct sal from emp;
SAL
--------800
950
1100
1250
1300
1500
1600
2450
2850
2975
3000
5000
12 rows selected.
6. Show the structure of table Dept.
SQL> describe dept;
Name
Null?
Type
------------------------------- -------- ---DEPTNO
NOT NULL NUMBER(2)
DNAME
VARCHAR(12)
LOC
VARCHAR2(13
7. Write a Query to display Ename and sal of
employees whose salary is greater than or
equal to 3000 from table emp.
SQL> select ename ,sal from emp where sal>=3000;
ENAME
SAL
---------- --------SCOTT
3000
KING
5000
FORD
3000
8. Write a query to display employee name,
salary and department number who are not
getting commission from table emp.
SQL> select ename, sal, deptno from emp where comm
is null;
ENAME
SAL
DEPTNO
---------- --------- --------SMITH
800
20
JONES
2975
20
BLAKE
2850
30
CLARK
2450
10
SCOTT
3000
20
KING
5000
10
ADAMS
1100
20
JAMES
950
30
FORD
3000
20
MILLER
1300
10
10 rows selected.
9. Write a query to display employee Number,
name , sal, sal*12 as annual Salary whose
commission is not null from table emp.
SQL> select ename, sal, deptno from emp where comm
is not null;
ENAME
SAL
DEPTNO
---------- --------- --------ALLEN
1600
30
WARD
1250
30
MARTIN
1250
30
TURNER
1500
30
10. Write a query to display employee name
and salary of those employees who do not have
their salary in the range of 1500 to 2000.
SQL> select ename, sal from emp where sal not
between 1500 and 2000;
ENAME
SAL
---------- --------SMITH
800
WARD
1250
JONES
2975
MARTIN
1250
BLAKE
2850
CLARK
2450
SCOTT
3000
KING
5000
ADAMS
1100
JAMES
950
FORD
3000
MILLER
1300
12 rows selected.
11. Write a query o display name, job, salary,
and hiredate of employees who are hired
between February 20 1981 and May 1 1981.
Order the query in ascending order of
HireDate.
SQL>select ename, job, hiredate from emp where
hiredate between '20-FEB-1981' and '1-MAY-1981'
>order by hiredate;
ENAME
JOB
HIREDATE
---------- --------- --------ALLEN
SALESMAN
20-FEB-81
WARD
SALESMAN
22-FEB-81
JONES
MANAGER
02-APR-81
BLAKE
MANAGER
01-MAY-81
12. Write a query to display the name and
hiredate of all the employees who were hired
in 1982.
SQL> select ename , hiredate from emp where
hiredate between '1-JAN-1982' and '31-DEC-1982';
ENAME
HIREDATE
---------- --------MILLER
23-JAN-82
13. Write a query to display the name, job title
and salary of those employees who do not have
manager.
SQL> select ename, job, sal from emp where mgr is
null;
ENAME
JOB
SAL
---------- --------- --------KING
PRESIDENT
5000
14. Write a query to display the name of
employee whose name contains 'A' as third
alphabet.
SQL>
%';
select ename from emp where ename like '__A
ENAME
---------BLAKE
CLARK
ADAMS
15. Write a query to display the name of the
employee whose name contains 'T' as the last
alphabet.
SQL>
select ename from emp where ename like '%T';
ENAME
---------SCOTT
16. Write a Query to display the name of the
employee whose name contains 'M' as first
alphabet and 'L' as third alphabet.
SQL> select ename from emp where ename like 'M_L%';
ENAME
---------MILLER
17. Write a Query to display the name of the
employee who is having 'L' as any alphabet of
the name.
SQL> select ename from emp where ename like '%L%';
ENAME
---------ALLEN
BLAKE
CLARK
MILLER
18. WAQ to display employee name, salary,
salary increased by 15% expressed as a whole
number Label the column as 'New Salary'.
SQL> select ename,sal, round (sal + sal * 0.15,0)
"New Salary" from emp;
ENAME
SAL New Salary
---------- --------- ---------SMITH
800
920
ALLEN
1600
1840
WARD
1250
1438
JONES
2975
3421
MARTIN
1250
1438
BLAKE
2850
3278
CLARK
2450
2818
SCOTT
3000
3450
KING
5000
5750
TURNER
1500
1725
ADAMS
1100
1265
JAMES
950
1093
FORD
3000
3450
MILLER
1300
1495
14 rows selected.
19. WAQ to display ename, salary review date
which is the date after six months of hiredate
in format of 'Sunday, 7 SEP, 1981'.
SQL> SELECT ENAME, TO_CHAR ( ADD_MONTHS
(HIREDATE,6), 'DAY, DD,MON, YYYY') "REVIEW DATE"
FROM EMP;
ENAME
REVIEW DATE
---------- ----------------------SMITH
WEDNESDAY, 17,JUN, 1981
ALLEN
THURSDAY , 20,AUG, 1981
WARD
SATURDAY , 22,AUG, 1981
JONES
FRIDAY
, 02,OCT, 1981
MARTIN
SUNDAY
, 28,MAR, 1982
BLAKE
SUNDAY
, 01,NOV, 1981
CLARK
WEDNESDAY, 09,DEC, 1981
SCOTT
MONDAY
, 19,OCT, 1987
KING
MONDAY
, 17,MAY, 1982
TURNER
MONDAY
, 08,MAR, 1982
ADAMS
MONDAY
, 23,NOV, 1987
JAMES
THURSDAY , 03,JUN, 1982
FORD
THURSDAY , 03,JUN, 1982
MILLER
FRIDAY
, 23,JUL, 1982
14 rows selected.
20. WAQ to display ename and total no of
weeks lapsed between hiredate and today.
SQL> SELECT ENAME, ROUND((SYSDATE- HIREDATE)/7)
FROM EMP
ENAME
ROUND((SYSDATE-HIREDATE)/7)
---------- --------------------------SMITH
1272
ALLEN
1263
WARD
1263
JONES
1257
MARTIN
1232
BLAKE
1253
CLARK
1247
SCOTT
942
KING
1224
TURNER
1234
ADAMS
937
JAMES
1222
FORD
1222
MILLER
1215
14 rows selected.
21. WAQ to display ename and total no of days
lapsed between hiredate and today.
SQL> SELECT ENAME, ROUND (SYSDATE- HIREDATE) FROM
EMP;
ENAME
ROUND(SYSDATE-HIREDATE)
---------- ----------------------SMITH
8906
ALLEN
8841
WARD
8839
JONES
8800
MARTIN
8621
BLAKE
8771
CLARK
8732
SCOTT
6592
KING
8571
TURNER
8641
ADAMS
6558
JAMES
8555
FORD
8555
MILLER
8504
14 rows selected.
22. Create a query that produces display in the
following format:
<employee name> EARNS<salary> Monthly
and Working as<Job>
SQL> SELECT ENAME ||'EARNS $'||SAL||'MONTHLY AND
WORKING AS' || JOB FROM EMP;
ENAME||'EARNS$'||SAL||'MONTHLYANDWORKINGAS'||JOB
--------------------------------------------------------------------------------------SMITHEARNS $800MONTHLY AND WORKING ASCLERK
ALLENEARNS $1600MONTHLY AND WORKING ASSALESMAN
WARDEARNS $1250MONTHLY AND WORKING ASSALESMAN
JONESEARNS $2975MONTHLY AND WORKING ASMANAGER
MARTINEARNS $1250MONTHLY AND WORKING ASSALESMAN
BLAKEEARNS $2850MONTHLY AND WORKING ASMANAGER
CLARKEARNS $2450MONTHLY AND WORKING ASMANAGER
SCOTTEARNS $3000MONTHLY AND WORKING ASANALYST
KINGEARNS $5000MONTHLY AND WORKING ASPRESIDENT
TURNEREARNS $1500MONTHLY AND WORKING ASSALESMAN
ADAMSEARNS $1100MONTHLY AND WORKING ASCLERK
JAMESEARNS $950MONTHLY AND WORKING ASCLERK
FORDEARNS $3000MONTHLY AND WORKING ASANALYST
MILLEREARNS $1300MONTHLY AND WORKING ASCLERK
14 rows selected.
23. WAQ to display ename with first letter
capitalized and other letter lowercase and
length of their name string.
SQL> select initcap (ename) "name", length (ename)
from emp;
name
LENGTH(ENAME)
---------- ------------Smith
Allen
Ward
Jones
Martin
Blake
Clark
Scott
King
Turner
Adams
James
Ford
Miller
14 rows selected.
24. WAQ to display the ename, dname and all
the employees that work in the same dept as
the given employee.
SQL> select ename, deptno from emp where deptno in
(select deptno from emp)
ENAME
DEPTNO
---------- --------CLARK
10
KING
10
MILLER
10
SMITH
20
ADAMS
20
FORD
20
SCOTT
20
JONES
20
ALLEN
30
BLAKE
30
MARTIN
30
JAMES
30
TURNER
30
WARD
30
14 rows selected.