0% found this document useful (0 votes)
27 views

SQL and Python Questions

Uploaded by

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

SQL and Python Questions

Uploaded by

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

Q1) Write a query to create table CABHUB and CUSTOMER with following fields

Table:- CABHUB
Vcod VehicleName Make Color Capacity Charges
e
100 Innova Toyota WHITE 7 15
102 SX4 Suzuki BLUE 4 14
104 C Class Mercedes RED 4 35
105 A-Star Suzuki WHITE 3 14
108 Indigo Tata SILVER 3 12

Table:- CUSTOMER
Ccode CName VCode
1 Hemant Sahu 101
2 Raj Lal 108
3 Feroza Shah 105
4 Ketan Dhal 104

create table CABHUB (vcode integer PRIMARY KEY, VehicleName varchar(20), make
varchar(20), color varchar(20), capacity integer, charges float );
insert into cabhub values(100,'Innova','Toyota','white',7,15);
insert into cabhub values(102,'SX4','Suzuki','blue',4,14);
insert into cabhub values(104,'C Class','Mercedes','red',4,35);
insert into cabhub values(105,'A-star','Suzuki','white',3,14);
insert into cabhub values(108,'Indigo','Tata','silver',3,12);

create table CUSTOMER (CCode integer, CName varchar(20), Vcode integer);


insert into customer values (1,'Hemant sahu', 101);
insert into customer values (2,'Raj lal', 108);
insert into customer values (3,'Feroza Shah', 105);
insert into customer values (4,'Ketan Dhal', 104);

(i) To display the names of all the white colored vehicles.


Answer: select vehiclename from cabhub where color='white';
(ii) To display name of vehicle, make and capacity of vehicles in ascending order of their
capacity.
Answer: SELECT vehiclename, make, capacity from cabhub ORDER BY capacity;
(iii) To display the highest charges at which a vehicle can be hired from CABHUB
Answer: SELECT max(charges) from CABHUB;
(iv) To display the customer name and the corresponding name of the vehicle hired by them.
Answer: select cname, vehiclename from customer, cabhub where
customer.vcode=cabhub.vcode;
Q2) Write a query to create table Employee with following fields

Ecode – Integer (Primary Key), Ename – Varchar(20),


Gender – Char(1), Grade Char(2), Gross Integer
Insert following records in it.
Table: employee1
ECODE ENAME GENDER GRAD GROSS
E
1001 Cherry F E4 50000
1002 Vedika F E2 40000
1003 Atharv M E1 90000
1004 Prajakta F E3 55000
1005 Samir M E2 60000

CREATE TABLE employee1 ( ECODE integer, ENAME varchar(20), GENDER char(1),


GRADE char(2), GROSS integer, PRIMARY KEY (ecode) ) ;
INSERT INTO employee1 VALUES(1001 , 'Cherry' , 'F' , 'E4' , 50000);
INSERT INTO employee1 VALUES(1002 , 'Vedika' , 'F' , 'E2' , 40000);
INSERT INTO employee1 VALUES(1003 , 'Atharv' , 'M' , 'E1' , 90000);
INSERT INTO employee1 VALUES(1004 , 'Prajakta' , 'F' , 'E3' , 55000);
INSERT INTO employee1 VALUES(1005 , 'Samir' , 'M' , 'E2' , 60000);
Answers
i) Write a query to show records of all female employees.
Answer:
Answer: select *from employee where gender='F';

ii) Write a query to change Gross salary of Vedika to 70,000


Answer: update employe1e set gross=70000 where ENAME="Vedika";

iii) Write a query to show all records in descending order of gross salary.
Answer: select *from employee1 order by gross desc;

iv) Write a query to add a new column designation in employee1 table


Answer: alter table employee1 add Designation varchar (20);
Q3) Write a query to create table Library with following fields
Book_Id – Integer (Primary Key), Book_Title – Varchar(20)
Author_name – Varchar(30), Price Float, Quantity Integer

Insert following records in it.


Table: LIBRARY
Book_I Book_Title Author_name Price Quantity
d
101 ANSI C E Balagursamy 350.00 20
102 OOP using C++ Robert Lafore 425.00 15
103 Data Structure using C E Balagursamy 400.00 18
104 Let Us C Yeshwant Kanitkar 520.50 22
105 Computer Network Andrew Tanenbaum 605.00 25

CREATE TABLE Library (Book_Id integer, Book_Title varchar(30), Author_name


varchar(30), Price float, quantity integer, PRIMARY KEY (Book_Id) );
INSERT INTO library VALUES(101 , 'ANSI C' , 'E Balagurusamy' , '350.00' , 20);
INSERT INTO library VALUES(102 , 'OOP using C++' , 'Robert Lafore' , '425.00' , 15);
INSERT INTO library VALUES(103 , 'Data Structure using C' , 'E Balagurusamy' , '400.00' ,
18);
INSERT INTO library VALUES(104 , 'Let Us C' , 'Yeshwant Kanitkar’ , '520.50' , 22);
INSERT INTO library VALUES(105 , 'Computer Network' , 'Andrew Tanenbaum’ , '605.00' ,
25);

i) Write a query to show all records of author E Balagurusamy.


Answer: select *from library where author_name='E Balagurusamy';
ii) Write a query to change Price of the book ANSI C to Rs. 380.50
Answer: update library set price=380.50 where book_title="ANSI C";
iii) Write a query to show all records where price is greater than Rs 500 and quantity is more
than 20
Answer: select *from library where price>500 and quantity>20;
iv) Write a query to delete a record whose book_id is 105
Answer: delete from library where book_id=105;
Q4) Write a query to create table Item with following fields and data.

TABLE : PRODUCT
P_ID ProductName Manufacturer Price
TP01 Talcum Powder LAK 40
FW0 Face Wash ABC 45
5
BS01 Bath Soap ABC 55
SH06 Shampoo XYZ 120
FW1 Face Wash XYZ 95
2

create table product (P_ID varchar(4),ProductName varchar(20), Manufacturer varchar(4),


Price integer );
insert into product values('TP01','Talcum Powder','LAK',Price integer );
insert into product values('FW05','Face Wash','ABC',45);
insert into product values('BS01','Bath Soap','ABC',55);
insert into product values('SH06','Shampoo','XYZ',120);
insert into product values('FW12','Face Wash','XYZ',95);

i) Write a query to show all the product names only.


Answer: select productname from product;

ii) Write a query to show all the records in their descending order of price.
Answer: select *from product order by price desc;

iii) Write a query to change price of Talcum powder to Rs. 50/-


Answer: update product set set price=50 where productname= “Talcum Powder”

iv) Write a query to add a new column “man_date” for manufacturing date.
Answer: alter table product ADD man_date date;

PYTHON PROGRAMS
Q.1. A Program to read the content of file and display the total number of consonants, vowels,
uppercase letters and lowercase letters. Show output to the examiner.
Q.2. Write a Program in Python to read & display file content line by line with each word separated
by “#” symbol. Show output to the examiner.
Q.3. A Program to generate random number 1-6, simulating a dice.
Q.4. A Program in Python which will create a stack and perform PUSH & POP operation
Q.5. A Program to connect with database and create a new table ‘student’ with following fields.
1) Admission Number 2) Student Name 3) gender and 4) marks

Q.1. A Program to read the content of file and display the total number of
consonants, vowels, uppercase letters and lowercase letters. Show output to the
examiner.

Answer:
# Program to read content of file & display total number of vowels, consonants,
lowercase and uppercase characters

f = open("abc.txt")
v=0
c=0
u=0
l=0
o=0
data = f.read()
vowels=['a','e','i','o','u']
for ch in data:
if ch.isalpha():
if ch.lower() in vowels:
v+=1
else:
c+=1
if ch.isupper():
u+=1
elif ch.islower():
l+=1
elif ch!=' ' and ch!='\n':
o+=1
print("Total Vowels in file :",v)
print("Total Consonants in file n :",c)
print("Total Capital letters in file :",u)
print("Total Small letters in file :",l)
print("Total Other than letters :",o)
f.close()

Q.2. Write a Program in Python to read & display file content line by line with each
word separated by “#” symbol. Show output to the examiner.
Answer:
#Program to read content of file line by line and display each word separated by '#'
f = open("file1.txt")
for line in f:
words = line.split()
for w in words:
print(w+'#',end='')
print()
f.close()
Q.3. A Program to generate random number 1-6, simulating a dice.
Answer:
import random
import time
print("Press CTRL+C to stop the dice ")
play='y'
while play=='y':
try:
while True:
for i in range(10):
print()
n = random.randint(1,6)
print(n,end='')
time.sleep(.00001)
except KeyboardInterrupt:
print("Your Number is :",n)
ans=input("Play More? (Y) :")
if ans.lower()!='y':
play='n'
break

Q.4. A Program in Python which will create a stack and perform PUSH & POP
operation
Answer:
mystack=[ ]
length = int(input("Enter size of stack : "))
for i in range(0,length):
n=int(input("Enter elements in stack : "))
mystack.append(n)

print("\n Stack is as follows : ")


print(mystack)
print("\n After POP operation last element of stack is deleted")
print(" Stack after POP operation is as follows :")
mystack.pop()
print(mystack)
Q.5. A Program to connect with database and create a new table ‘student’ with following fields.
1) Admission Number 2) Student Name 3) gender and 4) marks

# A Program to connect with database and create a new table.


import mysql.connector
demodb=mysql.connector(host="localhost", user="root",password="tiger",
database="mydatabase")
democursor=demodb.cursor()
democursor.execute("CREATE TABLE STUDENT (admn_no int primary key, sname
varchar(30),
gender char(1), marks float(4,2))")
demodb.commit( )

You might also like