SSA - Class 12 Practical - 25-26
SSA - Class 12 Practical - 25-26
SCHOOL ROLL NO :
CBSE ROLL NO :
NAME :
HOUSE :
1
SAINIK SCHOOL AMARAVATHINAGAR
CERTIFICATE
(PM Jigajinni)
PGT for Computer Science
Examiners:
1 Name: PM Jigajinni Signature:
(Internal)
2 Name: Signature:
(External)
2
TABLE OF CONTENTS
PAGE
S.NO DATE PROGRAMS SIGN
NO
WRITE A PYTHON PROGRAM THAT WILL ACCEPT THE
3
PAGE
S.NO DATE PROGRAMS SIGN
NO
WRITE A PROGRAM TO COPY ALL THE LINES THAT
25 SQL COMMANDS 47
26 SQL JOINS 71
4
1. WRITE A PYTHON PROGRAM THAT WILL ACCEPT THE BASE AND HEIGHT
OF A TRIANGLE AND COMPUTE THE AREA.
area = b*h/2
5
2. WRITE A PROGRAM TO IMPLIMENT A CALCULATOR
# This function adds two numbers
def add(x, y):
return x + y
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
while True:
# take input from the user
choice = input("Enter choice(1/2/3/4): ")
6
if choice == '1':
print(num1, "+", num2, "=", add(num1, num2))
7
3. WRITE A FUNCTION TO CALCULATE THE FACTORIAL OF GIVEN NUMBER
factorial = 1
8
4. WRITE A FUNCTION WHICH TAKES N AS AN ARGUMENT AND PRINT FIBONACCI
NUMBERS UPTO N
def gen_fibonacci(nterms):
n1, n2 = 0, 1
count = 0
if nterms<= 0:
print("Please enter a positive integer")
# if there is only one term, return n1
elifnterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
else:
print("Fibonacci sequence:")
while count <nterms:
print(n1)
nth = n1 + n2
n1 = n2
n2 = nth
count += 1
def main():
n=int(input("Please enter a positive integer"))
gen_fibonacci(n)
main()
9
5 WRITE A PROGRAM IN PYTHON TO FIND GIVEN STRING IS PALINDROME OR
NOT
#function which return reverse of a string
def isPalindrome(s):
return s == s[::-1]
# Driver code
s = "malayalam"
ans = isPalindrome(s)
if ans:
print("Yes")
else:
print("No")
10
6. WRITE A FUNCION TO ILLUSTRATE DEFAULT ARGUMENT
def greet_msg(name="Mohan"):
print("Hello ", name)
def driver_code():
greet_msg("Karthick")
greet_msg()
driver_code()
11
7. WRITE A FUNCTION TO ILLUSTRATE THE VARIABLE LENGTH ARGUMRNTS
def sum(*n):
total=0
for i in n:
total+=i
print("Sum = ", total)
def driver_code():
sum() #o/p sum=0
sum(10) #o/p sum=10
sum(10,20,30,40) #o/p sum=100
driver_code()
12
8. WRITE A FUNCTION TO ILLUSTRATES USE OF KEYWORD ARGUMENTS
# 2 keyword arguments
student(firstname ='Rajesh', standard ='Eight')
# 2 keyword arguments
student(lastname ='Keerthipriyan', firstname ='Prakash')
13
9. WRITE A FUNCTION TO ILLUSTRATES USE OF RETURN STATEMENT
def adder(x,y):
add=x+y
return add
def driver_code():
a=56
b=34
result=adder(a,b)
print("The result is = ",result)
driver_code()
14
10. WRITE A PROGRAM TO ILLISTRATE THE USE OF try….except Block –
Exception Handling
try:
numerator = 10
denominator = 0
result = numerator/denominator
print(result)
except:
print("Error: Denominator cannot be 0.")
15
11. WRITE A PROGRAM TO ILLISTRATE THE USE of Catching Specific
Exceptions
try:
even_numbers = [2,4,6,8]
print(even_numbers[5])
except ZeroDivisionError:
print("Denominator cannot be 0.")
except IndexError:
print("Index Out of Bound.")
16
12. PYTHON PROGRAM TO COUNT VOWELS, LINES, CHARACTERS IN TEXT
FILE
def counting(filename):
17
elif alpha == "\n":
line += 1
18
13. WRITE A PYTHON PROGRAM TO READ A RANDOM LINE FROM A FILE.
import random
def Read_Random_Lines(fname):
lines = open(fname).read().splitlines()
return random.choice(lines)
#Drive Code
def main():
print(Read_Random_Line('robotics.txt'))
main()
19
14 WRITE A PROGRAM TO COPY ALL THE LINES THAT CONTAIN THE CHARACTER
`A' IN A FILE AND WRITE IT TO ANOTHER FILE.
def Write_Lines(Line):
myfile= open("Newtest.txt","a+")
myfile.write("\n"+Line)
myfile.close()
def Read_Lines():
myfile=open("test.txt","r")
Line=myfile.readline()
print(" First Letter : ",Line[0])
while (Line!=""):
if (Line[0]=='a' or Line[0]=='A'):
Write_Lines(Line)
Line=myfile.readline()
print("File Created! Lines Copied")
myfile.close()
#Driver Code
def main():
Read_Lines()
main()
20
OUTPUT:
TEST.txt CONTAINS:
NEW FILE CREATED NAMED NEWTEST.txt AND COPIED LINES STARTS WITH a
OR A LETTER FROM TEST.txt FILE
21
15. WRITE A PROGRAM TO CREATE BINARY FILE AND DISPLAY THE CONTENTS
STORED IN BINARY FILE (MAKE USE OF PICKLE MODULE).
import pickle
def Bin_Write():
import pickle
Mylist=[68,43,42,78,90,53,26,81,90,345]
f=open("myfile.dat",'wb')
pickle.dump(Mylist,f)
print("Binary File Created and List is added!")
f.close()
def Bin_read():
import pickle
f=open("myfile.dat",'rb')
Mylist=pickle.load(f)
print("List stored in Binary File is…………..",Mylist)
f.close()
# Driver Code:
def main():
Bin_Write()
Bin_read()
main()
22
16. WRITE A PROGRAM TO CREATE A BINARY FILE WITH NAME AND ROLL
NUMBER. SEARCH FOR A GIVEN ROLL NUMBER AND DISPLAY NAME, IF NOT
FOUND DISPLAY APPROPRIATE MESSAGE.
import pickle
#creating the file and writing the data
f=open("records.dat", "wb")
pickle.dump(["Wakil", 1], f)
pickle.dump(["Tanish", 2], f)
pickle.dump(["Priyashi", 3], f)
pickle.dump(["Kanupriya", 4], f)
pickle.dump(["Aaheli", 5], f)
f.close()
#opeining the file to read contents
f=open("records.dat", "rb")
n=int(input("Enter the Roll Number: "))
flag = False
while True:
try:
x=pickle.load(f)
if x[1]==n:
print("Name: ", x[0])
flag = True
except EOFError:
break
if flag==False:
print("This Roll Number does not exist")
23
24
17. WRITE A PROGRAM TO UPDATE THE MARKS OF PERTICULAR RECORD OF
STUDENT USING BINARY FILES.
import pickle
def Writerecord(sroll,sname,sperc,sremark):
with open ('StudentRecord.dat','ab') as Myfile:
srecord={"SROLL":sroll,"SNAME":sname,"SPERC":sperc,
"SREMARKS":sremark}
pickle.dump(srecord,Myfile)
def Readrecord():
with open ('StudentRecord.dat','rb') as Myfile:
print("\n-------DISPALY STUDENTS DETAILS--------")
print("\nRoll No.",' ','Name','\t',end='')
print('Percetage',' ','Remarks')
while True:
try:
rec=pickle.load(Myfile)
print(' ',rec['SROLL'],'\t ' ,rec['SNAME'],'\t
',end='')
print(rec['SPERC'],'\t ',rec['SREMARKS'])
except EOFError:
break
def Input():
n=int(input("How many records you want to create :"))
for ctr in range(n):
sroll=int(input("Enter Roll No: "))
sname=input("Enter Name: ")
sperc=float(input("Enter Percentage: "))
sremark=input("Enter Remark: ")
Writerecord(sroll,sname,sperc,sremark)
25
def Modify(roll):
with open ('StudentRecord.dat','rb') as Myfile:
newRecord=[]
while True:
try:
rec=pickle.load(Myfile)
newRecord.append(rec)
except EOFError:
break
found=1
for i in range(len(newRecord)):
if newRecord[i]['SROLL']==roll:
name=input("Enter Name: ")
perc=float(input("Enter Percentage: "))
remark=input("Enter Remark: ")
newRecord[i]['SNAME']=name
newRecord[i]['SPERC']=perc
newRecord[i]['SREMARKS']=remark
found =1
else:
found=0
if found==0:
26
def main():
while True:
print('\nYour Choices are: ')
print('1.Insert Records')
print('2.Dispaly Records')
print('3.Update Records')
print('0.Exit (Enter 0 to exit)')
ch=int(input('Enter Your Choice: '))
if ch==1:
Input()
elifch==2:
Readrecord()
elifch==3:
r =int(input("Enter a Rollno to be update: "))
Modify(r)
else:
break
main()
27
18 WRITE A PROGRAM TO ILLUSTRATE WRITE AND READ OPERATIONS ON
CSV FILE.
import csv
# opening the CSV file for writing contents in it
myfile=open('stud.csv', mode ='w')
stuwriter = csv.writer(myfile)
n=int(input("How many records do you want to enter"))
for i in range(n):
print("Student Record",(i+1))
rno=int(input("Enter Roll No"))
nm=input("Enter Name")
m=int(input("Enter Marks"))
sturec=[rno,nm,m]
stuwriter.writerow(sturec)
myfile.close()
# opening the CSV file for reading
myfile=open('stud.csv', mode ='r',newline='\r\n')
# reading the CSV file
stureader = csv.reader(myfile)
# displaying the contents of the CSV file
for lines in stureader:
print(lines)
28
19 WRITE A PROGRAM TO IMPLEMENT PUSH() AND DISPLAY FUNCTIONS OF A
STACK USING A LIST DATA-STRUCTURE.
#IMPLIMENTATION OF STACK
def isempty(stk):
if stk==[]:
return True
else:
return False
def push(stk,item):
stk.append(item)
top=len(stk)-1
def display(stk):
if isempty(stk):
print('stack is empty')
else:
top=len(stk)-1
print(stk[top],'<-top')
for i in range(top-1,-1,-1):
print(stk[i])
#Driver Code
def main():
stk=[]
top=None
while True:
print('''stack operation
1. Push
29
2. Display
3.exit''')
choice=int (input('enter choice:'))
if choice==1:
item=int(input('enter item:'))
push(stk,item)
elif choice==2:
display(stk)
elif choice==3:
break
else:
print('invalid')
exit()
main()
30
20. WRITE A PROGRAM TO IMPLEMENT POP() AND DISPLAY() FUNCTIONS OF
A STACK USING A LIST DATA-STRUCTURE.
#IMPLIMENTATION OF STACK
def isempty(stk):
if stk==[]:
return True
else:
return False
def pop(stk):
if isempty(stk):
return "underflow"
else:
item=stk.pop()
if len(stk)==0:
top=None
else:
top=len(stk)-1
return item
def display(stk):
if isempty(stk):
print('stack is empty')
else:
top=len(stk)-1
print(stk[top],'<-top')
for i in range(top-1,-1,-1):
print(stk[i])
31
#Driver Code
def main():
stk=[12,34,57,89,120]
top=None
while True:
print('''stack operation
1.pop
2.display
3.exit''')
choice=int (input('enter choice:'))
if choice==1:
item=pop(stk)
if item=="underflow":
print('stack is underflow')
else:
print('poped')
elif choice==2:
display(stk)
elif choice==3:
break
else:
print('invalid')
exit()
main()
32
33
21. INTEGRATE PYTHON WITH SQL – TO CONNECT DATABASE
else:
print(“ Connected to MySQL database”)
OUTPUT:
34
22. INTEGRATE PYTHON WITH SQL – TO FETH RECORDS FROM THE TABLE AND
DISPLAY IT ON THE SCREEN
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="school"
)
mycursor = mydb.cursor()
myresult = mycursor.fetchall()
for x in myresult:
print(x)
35
MYSQL
36
23. SQL: TO CREATE TWO TABLES FOR STATIONARY AND CONSUMER AND
EXECUTE THE GIVEN COMMANDS USING SQL.
37
CREATING STATIONARY TABLE:
SQL> CREATE TABLE STATIONARY (S_ID char(5) NOT NULL PRIMARY KEY,
StationaryName char(25), Company char(5), Price int);
38
INSERTING RECORD/TUPLE IN CONSUMER TABLE:
39
ii) SELECT * FROM STATIONARY WHERE PRICE BETWEEN 8 AND 15;
OUTPUT:
OUTPUT:
OUTPUT:
40
v) SELECT DISTINCT(COMPANY) FROM STATIONERY;
OUTPUT:
41
24. SQL: TO CREATE TWO TABLES FOR ITEM AND TRADERS AND EXECUTE THE
GIVEN COMMANDS USING SQL.
42
CREATING ITEM TABLE:
43
i) select * from ITEM order by IName;
OUTPUT:
ii) select IName , Price from ITEM where Price between 10000
and 22000;
OUTPUT:
OUTPUT:
44
iv) select Price , IName , Qty from ITEM where Qty>150;
OUTPUT:
OUTPUT:
45
SQL COMMANDS
46
25. SQL COMMANDS
1. CREATE DATABASE
AIM
Create a Database of name EMPLOYEE.
QUERY
2. OPEN COMMAND
AIM
Open the Database using the USE command.
QUERY
3. CREATE TABLE
AIM
Create table EMP with specified number of rows and columns and
apply necessary constraints.
QUERY
SQL> CREATE TABLE EMP (ENO INTEGER PRIMARYKEY, ENAME VARCHAR (20)
UNIQUE,JOB VARCHAR (20) DEFAULT “CLERK”, GENDER CHAR (1) NOT NULL,
HIRE DATE, SAL FLOAT (6,2) CHECK SAL>2000,COMM INTEGER);
4. INSERT COMMAND
AIM
Insert tuples to the table EMP.
QUERY
47
SQL> INSERT INTO EMP VALUES(3,’JASMIN’,’SALESMAN’,’F’,’1982-12-
09’,2450,300);
5. SELECT COMMAND
AIM
Display all employee details.
QUERY
OUTPUT
ENO ENAME JOB GENDER HIRE SAL COMM
AIM
Display ENO, ENAME and JOB of all Employees.
QUERY
48
OUTPUT
ENO ENAME JOB
1 KING MANAGER
2 BLAKE MANAGER
3 JASMIN SALESMAN
4 JONES SALESMAN
5 CLARK ANALYST
6 GEETHA CLERK
OUTPUT
DISTINCT JOB
MANAGER
SALESMAN
ANALYST
CLERK
49
OUTPUT
ENO ENAME JOB GENDER HIRE SAL COMM
8. RELATIONAL OPERATOR
AIM
Display the details of employee whose name is not ‘KING’.
QUERY
OUTPUT
AIM
Display the details of employee ENO,ENAME,SAL of people whose salary
is above 2500.
QUERY
50
OUTPUT
ENO ENAME SAL
1 KING 5000.00
2 BLAKE 2850.00
4 JONES 2975.00
9. LOGICAL OPERATORS
AIM
Display the details of employee whose job is manager or gender is
male.
QUERY
OUTPUT
AIM
Display the ENO, ENAME and COMM of employees with commission.
QUERY
OUTPUT
ENO ENAME SAL JOB
51
AIM
Display the details of employees whose job is not salesman.
QUERY
OUTPUT
QUERY
SQL> SELECT * FROM EMP WHERE SAL BETWEEN 2000 AND 5000;
OUTPUT
ENO ENAME JOB GENDER HIRE SAL COMM
52
AIM
Display the details of employees whose salary is not between 2000
and 5000.
QUERY
SQL> SELECT * FROM EMP WHERE SAL NOT BETWEEN 2000 AND 5000;
OUTPUT
ENO ENAME JOB GENDER HIRE SAL COMM
5 CLARK ANALYST M 1983-01-23 1250.00 NULL
6 GEETHA CLERK F 1981-02-22 1600.00 NULL
QUERY
OUTPUT
53
AIM
To display the details of employees except people having job as
Salesman, Manager and Clerk.
QUERY
OUTPUT
ENO ENAME JOB GENDER HIRE SAL COMM
5 CLARK ANALYST M 1983-01-23 1250.00 NULL
OUTPUT
ENO ENAME JOB GENDER HIRE SAL COMM
AIM
Display the details of Employees whose ENAME ends with ‘S’;
QUERY
OUTPUT
54
AIM
Display the details of Employee whose job contains “AL” string.
QUERY
OUTPUT
ENO ENAME JOB GENDER HIRE SAL COMM
3 JASMIN SALESMAN F 1982-12-09 2450.00 300
4 JONES SALESMAN M 1983-01-12 2975.00 500
5 CLARK ANALYST M 1983-01-23 1250.00 NULL
AIM
To display the details of Employees whose ENAME contains exactly 5
letters.
QUERY
OUTPUT
ENO ENAME JOB GENDER HIRE SAL COMM
AIM
To display the details of Employees whose ENAME contains at least
6 characters.
QUERY
55
OUTPUT
QUERY
OUTPUT
ENO ENAME COMM
1 KING NULL
2 BLAKE NULL
5 CLARK NULL
AIM
Display the ENO,ENAME and COMM of employees with commission.
QUERY
OUTPUT
ENO ENAME COMM
3 JASMIN 300
4 JONES 500
56
14. SORTING RESULTS BY ORDER BY
AIM
Display the details of Employees according to the alphabetical
order of their ENAME;
QUERY
OUTPUT
ENO ENAME JOB GENDER HIRE SAL COMM
AIM
Display the details of Employee by descending order of ENAME.
QUERY
OUTPUT
ENO ENAME JOB GENDER HIRE SAL COMM
57
AIM
Display the details of Employee by descending order of ENAME and
SAL in ascending order.
QUERY
OUTPUT
QUERY
OUTPUT
5*7
35
2021-08-19 10:34:20
AIM
Display the current system date.
QUERY
OUTPUT
CURDATE()
2021-08-19
AIM
Display current date and time.
QUERY
OUTPUT
NOW()
10:34:20
AGGREGATE FUNCTIONS
QUERY
59
OUTPUT
SUM(SAL)
7645.00
18. GROUP BY
AIM
Display the total salary of Employees in each Department
QUERY
SQL> SELECT JOB<SUM(SAL) FROM EMP GROUP BY JOB;
OUTPUT
JOB SUM(SAL)
ANALYST 1250.00
CLERK 1600.00
MANAGER 7850.00
SALESMAN 5245.00
AIM
Display the total Salary of Female.
QUERY
SQL> SELECT GENDER<SUM(SAL) FROM EMP GROUP BY GENDER
GENDER SUM(SAL)
F 4050.00
M 12075.00
AIM
Display Eno,Ename,Job and increase salary by 1000 whose job is
Salesman.
QUERY
SQL> SELECT ENO,ENAME,JOB,SAL+1000 FROM EMP WHERE JOB=’SALESMAN’;
OUTPUT
ENO ENAME JOB SAL+1000
3 JASMIN SALESMAN 3450.00
4 JONES SALESMAN 3975.00
OUTPUT
ENO ENAME JOB SAL*100 $
1 KING MANAGER 500000 $
2 BLAKE MANAGER 285000 $
3 JASMIN SALESMAN 245000 $
4 JONES SALESMAN 297500 $
5 CLARK ANALYST 125000 $
6 GEETHA CLERK 160000 $
61
21.CREATING TABLE FROM EXISTING TABLE
AIM
Create a Salary Table from the base table(Employee).
QUERY
SQL> CREATE TABLE SALARY1 AS(SELECT ENO,ENAME,SAL FROM EMP WHERE
COMM IS NULL);
OUTPUT
ENO ENAME SAL
1 KING 5000
2 BLAKE 2850
5 CLARK 1250
6 GEETHA 1600
22.UPDATE COMMAND
AIM
Update the salary of an employee from EMP table.
QUERY
SQL> UPDATE EMP SET SAL=2000 WHERE ENO=2;
OUTPUT
ENO ENAME JOB GENDER HIRE SAL COMM
1 KING MANAGER M 1981-11-17 5000 NULL
2 BLAKE MANAGER M 1981-03-01 2850 NULL
3 JASMIN SALESMAN F 1982-12-09 2450 300
4 JONES SALESMAN M 1983-01-12 2975 300
5 CLARK ANALYST M 1983-01-23 1250 NULL
6 GEETHA CLERK F 1981-02-22 1600 NULL
62
AIM
Display the table Salary1 after Updating the Salary as 2000 whose
Eno is 2.
QUERY
SQL> UPDATE SALARY1 SET SAL=2000 WHERE ENO=2;
OUTPUT
ENO ENAME SAL
1 KING 5000
2 BLAKE 2000
5 CLARK 1250
6 GEETHA 1600
AIM
Update the salary as 200 and Comm as 200 whose Eno is 1 and
Display it.
QUERY
SQL> UPDATE EMP SET VAL=2000,COMM=200 WHERE ENO=1;
OUTPUT
ENO ENAME JOB GENDER HIRE SAL COMM
1 KING MANAGER M 1981-11-17 5000 200
2 BLAKE MANAGER M 1981-03-01 2000 NULL
3 JASMIN SALESMAN F 1982-12-09 2450 300
4 JONES SALESMAN M 1983-01-12 2975 300
5 CLARK ANALYST M 1983-01-23 1250 NULL
6 GEETHA CLERK F 1981-02-22 1600 NULL
63
23.USING EXPRESSIONS IN UPDATE
AIM
Update the salary with current Salary+1000 whose job is Salesman
and displat it.
QUERY
SQL> UPDATE EMP SET SAL=SAL+1000 WHERE JOB=’SALESMAN’;
OUTPUT
ENO ENAME JOB GENDER HIRE SAL COMM
1 KING MANAGER M 1981-11-17 2000 200
2 BLAKE MANAGER M 1981-03-01 2000 NULL
3 JASMIN SALESMAN F 1982-12-09 3450 300
4 JONES SALESMAN M 1983-01-12 3975 300
5 CLARK ANALYST M 1983-01-23 1250 NULL
6 GEETHA CLERK F 1981-02-22 1600 NULL
OUTPUT
ENO ENAME JOB GENDER HIRE SAL COMM
1 KING MANAGER M 1981-11-17 2000 200
2 BLAKE MANAGER M 1981-03-01 2000 NULL
3 JASMIN SALESMAN F 1982-12-09 3450 300
4 JONES SALESMAN M 1983-01-12 3975 300
5 CLARK ANALYST M 1983-01-23 1250 NULL
6 GEETHA CLERK F 1981-02-22 NULL NULL
64
25.DELETE COMMAND
AIM
Remove the details of Employee whose Salary is above 3000 and
Display it.
QUERY
SQL> DELETE FROM SALARY1 WHERE SAL>3000;
OUTPUT
ENO ENAME JOB GENDER HIRE SAL COMM
2 BLAKE MANAGER M 1981-03-01 2000 NULL
5 CLARK ANALYST M 1983-01-23 1250 NULL
6 GEETHA CLERK F 1981-02-22 NULL NULL
26.VIEW COMMAND
AIM
Create a table COMM1 containing EMPNO,EMPNAME,EJOB, SALARY,
HIRE,GEN,COMM and COMM is NULL and display it.
QUERY
SQL> CREATE VIEW COMM1(EMPNO,EMPNAME,EJOB,GEN,HIRE,SALARY,COM)
OUTPUT
ENO ENAME JOB GENDER HIRE SAL COMM
2 BLAKE MANAGER M 1981-03-01 2000 NULL
5 CLARK ANALYST M 1983-01-23 1250 NULL
6 GEETHA CLERK F 1981-02-22 NULL NULL
65
AIM
Create the sample to display the detail of employee like
EMPNO,EMPNAME,SALARY and whose COMM is NULL and Display it.
QUERY
SQL> CREATE VIEW SAMPLE(EMPNO,EMPNAME,SAL) AS SELECT
ENO,ENAME,SAL+100 FROM EMP WHERE COMM IS NULL;
OUTPUT
ENO ENAME SAL
2 BLAKE 2000
5 CLARK 1250
6 GEETHA NULL
27.BUILT-IN FUNCTIONS
AIM
Convert the given string into Lower Case.
QUERY
SQL> SELECT LOWER(“HELLO”) FROM DUAL;
OUTPUT
LOWER(“HELLO”)
hello
AIM
Convert the given string into Upper case.
QUERY
SQL> SELECT UPPER(“hello”) FROM DUAL;
66
OUTPUT
LOWER(“HELLO”)
HELLO
AIM
Display the sub string of the given string.
QUERY
SQL> SELECT SUBSTR(“POINTER”,3,2) FROM DUAL;
OUTPUT
SUBSTR(“POINTER”,3,2)
IN
28.DDL COMMANDS
ALTER COMMAND
AIM
Remove column Salary from salary table and display the table
QUERY
SQL> ALTER TABLE SALARY1 DROP COLUMN SAL;
OUTPUT
ENO ENAME
2 BLAKE
5 CLARK
6 GEETHA
67
AIM
Add a new Column AGE to Employee table and Display it.
QUERY
SQL> ALTER TABLE EMP ADD(AGE INT(2));
OUTPUT
ENO ENAME JOB GENDER HIRE SAL COMM AGE
1 KING MANAGER M 1981-11-17 2000 200 NULL
68
AIM
Modify the size of the column ENAME into CHAR(25) using ALTER
command and display the table structure.
QUERY
SQL> ALTER TABLE EMP MODIFY ENAME CHAR(25);
OUTPUT
FIELD TYPE NULL KEY DEFAULT EXTRA
ENO int(11) NO PRI NULL
ENAME char(25) YES UNI NULL
JOB varchar(25) YES CLERK
GENDER char(1) NO NULL
HIRE date YES NULL
SAL float(6,2) YES NULL
COMM int(11) YES NULL
AGE int(2) YES NULL
AIM
Add Primary key constraints to the column ENO in the SALARY1 table
and Describe it.
QUERY
SQL> ALTER TABLE SALARY1 ADD PRI03Y KEY(ENO);
OUTPUT
ENO Int(11) NO PRI NULL
ENAME Varchar(20) YES NULL
69
SQL JOINS
70
JOINS
AIM
Display Cid, Ename, Pid, Pname, Manufac From client and Product
table whose having matching PID.
QUERY
SQL> SELECT CID,ENAME,CLIENT.PID,PNAME,MANUFAC
OUTPUT
71
INNER JOIN
AIM
IMPLIMENT INNER JOIN ON TABLES
CUSTOMER TABLE
ORDER TABLE
OrderID CustomerID OrderDate
10308 2 18-09-1996
10309 37 19-09-1996
10310 77 20-09-1996
QUERY
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;
OUTPUT:
72
OUTER JOIN
The LEFT JOIN keyword returns all records from the left table
(table1), and the matching records (if any) from the right table
(table2).
AIM
IMPLIMENT OUTER JOIN ON TABLES
QUERY
OUTPUT:
The LEFT JOIN keyword returns all records from the left table
(Customers), even if there are no matches in the right table
(Orders).
73
RIGHT JOIN
The RIGHT JOIN keyword returns all records from the right
table (table2), and the matching records (if any) from the left
table (table1).
AIM
IMPLIMENT RIGHT JOIN ON TABLES
QUERY
OUTPUT:
The RIGHT JOIN keyword returns all records from the right
table (Employees), even if there are no matches in the left table
(Orders).
74
CROSS JOIN
The CROSS JOIN keyword returns all records from both tables
(table1 and table2).
AIM
IMPLIMENT CROSS JOIN ON TABLES
QUERY
The CROSS JOIN keyword returns all matching records from both
tables whether the other table matches or not. So, if there are
rows in "Customers" that do not have matches in "Orders", or if
there are rows in "Orders" that do not have matches in "Customers",
those rows will be listed as well.
75