CBSE Class 12 Computer Science Question 33 of 47

Interface Python with SQL — Question 11

Back to all questions
11
Question

Question 11

Write a program to display all records in ascending order of their salary from table employee.

Answer

Table Employee:

EMPNOENAMEDEPTSALARY
1RAJESHIT60000
2MANOJ KUMARHISTORY65000
3ANUSHAMARKETING70000
4ABHAYFASHION STUDIES45000
import mysql.connector
mydb = mysql.connector.connect(host = "localhost",
                                user = "root",
                                passwd = "tiger",
                                database = "School")
mycursor = mydb.cursor()
mycursor.execute("select * FROM EMPLOYEE ORDER BY SALARY")
myrecords = mycursor.fetchall()
for row in myrecords:
    print(row)
mycursor.close()    
mydb.close()
Output
(4, 'ABHAY', 'FASHION STUDIES', 45000.0)
(1, 'RAJESH', 'IT', 60000.0)
(2, 'MANOJ KUMAR', 'HISTORY', 65000.0)
(3, 'ANUSHA', 'MARKETING', 70000.0)