Mysql
Mysql
(EMPID CHAR(5) PRIMARY KEY, ADHAAR BIGINT, NAME VARCHAR(15), DEPT VARCHAR(10),
SALARY INT, GEN VARCHAR(6), CITY VARCHAR(20));
Q4. To display EMPID, NAME, CITY of all employee who are not belongs to Rourkela.
MySQL> SELECT EMPID, NAME, CITY FROM EMPLOYEE WHERE CITY<>'ROURKELA';
Q5. To display EMPID, NAME and DEPT of all employee whose department is IT.
MySQL> SELECT EMPID,NAME,CITY FROM EMPLOYEE WHERE DEPT='IT';
Q6. To display NAME, DEPT and SALARY of all employee whose department is ‘IT’ and salary more than
80000.
MySQL> SELECT NAME,DEPT,SALARY FROM EMPLOYEE WHERE DEPT='IT' AND SALARY>80000;
Q7. To display NAME and CITY of all employee who are belongs to ‘SMBP’ or ‘CUTTACK’ .
MySQL> SELECT NAME,CITY FROM EMPLOYEE WHERE CITY='CUTTACK' OR CITY='SMBP';
Q8. To display NAME, GEN, CITY of employee who are either from ‘CUTTACK’ or gender is ‘MALE’.
MySQL> SELECT NAME,GEN,CITY FROM EMPLOYEE WHERE GEN='MALE' OR CITY='CUTTACK';
Q9. To display the NAME and CITY of all employee who are not belongs to either ‘ROURKELA’ or’BBSR’.
MySQL> SELECT NAME,CITY FROM EMPLOYEE WHERE NOT(CITY='ROURKELA' OR CITY='BBSR');
Q10. To display EMPID and SALARY of all employee whose salary is between 40000 and 60000.
MySQL> SELECT EMPID, SALARY FROM EMPLOYEE WHERE SALARY BETWEEN 40000 AND 60000;
Q11. To display NAME and CITY of all employee whose name starts with ‘R’;
MySQL> SELECT NAME,CITY FROM EMPLOYEE WHERE NAME LIKE 'R%';
Q12. To display NAME and CITY of all employee whose name starts with ‘I’;
MySQL> SELECT NAME,CITY FROM EMPLOYEE WHERE NAME LIKE '%I';
Q13. To display NAME and CITY of all employee whose third letter of the name is ‘D’;
MySQL> SELECT NAME,CITY FROM EMPLOYEE WHERE NAME LIKE '__D%';
Q14. To display NAME and CITY of all employee whose second last letter of the name is ‘K’.
MySQL> SELECT NAME,CITY FROM EMPLOYEE WHERE NAME LIKE '%K_';
Q15. To display NAME and CITY of all employee who are belongs to ‘SMBP’, ‘CUTTACK’ and ’BBSR’ .
MySQL> SELECT NAME,CITY FROM EMPLOYEE WHERE CITY IN('BBSR','CUTTACK','SMBP');
Q16. To display NAME and CITY of all employee who are not belongs to ‘SMBP’, ‘CUTTACK’ and ’BBSR’ .
MySQL> SELECT NAME,CITY FROM EMPLOYEE WHERE CITY NOT IN('BBSR','CUTTACK','SMBP');
Q17. To display NAME and SALARY of all employee in ascending order of the SALARY.
MySQL> SELECT NAME,SALARY FROM EMPLOYEE ORDER BY SALARY ASC;
Q18. To display NAME and SALARY of all employee in descending order of the NAME.
MySQL> SELECT NAME,SALARY FROM EMPLOYEE ORDER BY NAME DESC;
Q19. To display NAME and SALARY of all ‘MGMT’ employees in descending order of their SALARY.
MySQL> SELECT NAME,SALARY FROM EMPLOYEE WHERE DEPT='MGMT' ORDER BY SALARY;
Q26. To display DEPT and total no employee working in each department in employee table.
MySQL> SELECT DEPT, COUNT(*) FROM EMPLOYEE GROUP BY DEPT;
Q27. To display DEPT and total no male employee working in each department in employee table.
MySQL> SELECT DEPT, COUNT(*) FROM EMPLOYEE
WHERE GEN='MALE'
GROUP BY DEPT;
Q28. To display DEPT and total no employee working in each department where total count is more than
three(3) in employee table.
MySQL> SELECT DEPT, COUNT(*) FROM EMPLOYEE
GROUP BY DEPT
HAVING COUNT(*)>3;
Q29. To display DEPT and total no female employee working in each department where total count is
more than one(1) in employee table.
MySQL> SELECT DEPT, COUNT(*) FROM EMPLOYEE
WHERE GEN='FEMALE'
GROUP BY DEPT
HAVING COUNT(*)>1;
Q30. To display EMPID, NAME, CITY of all employee who are belongs to ‘CUTTACK’.
MySQL> SELECT EMPID, NAME, CITY FROM EMPLOYEE WHERE CITY='CUTTACK';
Relation: COMPANY Relation: FOOD
Q32. To display NAME, CITY, CODE, PRODUCT, PRICE from company and food.
MySQL> SELECT NAME,CITY,CODE,PRODUCT, PRICE FROM COMPANY NATURAL JOIN FOOD;
Q33. To update CITY as ‘ROURKELA’ of ‘BRITANNIA’ Company.
MySQL>UPDATE COMPANY SET CITY='ROURKELA' WHERE NAME='BRITANNIA';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
Q.35. To remove all the rows / data/ elements form FOOD table.
MySQL> TRUNCATE TABLE FOOD;
Query OK, 0 rows affected (0.11 sec)
Table Creation:
import mysql.connector as con
mydb=con.connect(host="localhost",user="root",passwd="admin",database="PUBLISHER")
if mydb.is_connected():
print("Connection established.")
else:
print("Connection not established.")
cur=mydb.cursor()
try:
qr="CREATE TABLE BOOK(BOOKID CHAR(5) PRIMARY KEY,NAME VARCHAR(15) NOT NULL,
AUTHOR VARCHAR(15) NOT NULL,PRICE SMALLINT);"
cur.execute(qr)
print("Table created sucessfully.")
except:
print("This table is already exist.")
mydb.close()
Data Insertion:
import mysql.connector as con
mydb=con.connect(host="localhost",user="root",passwd="admin",database="PUBLISHER")
if mydb.is_connected():
print("Connection established.")
else:
print("Connection not established.")
cur=mydb.cursor()
while True:
id=input("Enter BookId:")
nm=input("Enter Book-Name:")
ath=input("Enter Author-Name:")
pr=int(input("Enter Book-Price:"))
qr="INSERT INTO BOOK VALUES('{}','{}','{}',{});".format(id,nm,ath,pr)
cur.execute(qr)
cur.execute("COMMIT")
ch=input("Wants to add more record(y/n):")
if ch.lower()=='n':
break
print("Rows inserted sucessfully.")
mydb.close()
Output:
Connection established.
Enter BookId:B101
Enter Book-Name:PYTHON
Enter Author-Name:S.ARORA
Enter Book-Price:576
Wants to add more record(y/n):Y
Enter BookId:B102
Enter Book-Name:PHYSICS
Enter Author-Name:B.SHARMA
Enter Book-Price:958
Wants to add more record(y/n):Y
Enter BookId:B103
Enter Book-Name:CHEMISTRY
Enter Author-Name:M.MATHOOR
Enter Book-Price:898
Wants to add more record(y/n):Y
Enter BookId:B104
Enter Book-Name:ACCOUNTS
Enter Author-Name:A.CHAWLA
Enter Book-Price:755
Wants to add more record(y/n):Y
Enter BookId:B105
Enter Book-Name:ECONOMICS
Enter Author-Name:R.SING
Enter Book-Price:555
Wants to add more record(y/n):N
Rows inserted sucessfully.
Output
('B101', 'PYTHON', 'S.ARORA', 576)
('B102', 'PHYSICS', 'B.SHARMA', 958)
('B103', 'CHEMISTRY', 'M.MATHOOR', 898)
('B104', 'ACCOUNTS', 'A.CHAWLA', 755)
('B105', 'ECONOMICS', 'R.SING', 555)
Output
('B101', 'PYTHON', 'S.ARORA', 576)
Output:
('B101', 'PYTHON', 'S.ARORA', 576)
('B102', 'PHYSICS', 'B.SHARMA', 958)
('B103', 'CHEMISTRY', 'M.MATHOOR', 898)
Total row fetched: 3
7. WAP in python to print Book name and Book price of all book whose price is more than 700.
import mysql.connector as con
mydb=con.connect(host="localhost",user="root",passwd="admin",database="PUBLISHER")
cur=mydb.cursor()
cur.execute("SELECT NAME,PRICE FROM BOOK WHERE PRICE>700;")
data=cur.fetchmany(3)
count=cur.rowcount
for row in data:
print(row)
print("Total row fetched:",count)
mydb.close()
Output
('PHYSICS', 958)
('CHEMISTRY', 898)
('ACCOUNTS', 755)
Total row fetched: 3
8. WAP in python to print Book name ,Author name and price in descending order of their price.
import mysql.connector as con
mydb=con.connect(host="localhost",user="root",passwd="admin",database="PUBLISHER")
cur=mydb.cursor()
cur.execute("SELECT NAME,AUTHOR,PRICE FROM BOOK ORDER BY PRICE DESC;")
data=cur.fetchmany(3)
count=cur.rowcount
for row in data:
print(row)
print("Total row fetched:",count)
mydb.close()
Output
('PHYSICS', 958)
('CHEMISTRY', 898)
('ACCOUNTS', 755)
Total row fetched: 3