| 1. |
Explain OR, AND and NOT operator in SQL? |
|
Answer» The WHERE clause can be combined with AND, OR, and NOT operators. The AND and OR operators are used to filter records based on more than one condition. In this example you are going to display the details of students who have scored other than ‘A’ , or ‘B’ from the “student table” Example for WHERE WITH NOT Operator Example import sqlite3 connection= sqlite3.connect(“Academy.db”) cursor = connection.cursor( ) cursor.execute(“SELECT *FROM student where grade<>’A’ and Grade< >’B'”) result = cursor. fetchall( ) print(*result,sep= ”\n”) OUTPUT (3, ‘BASKAR’, ’C, ’M’ , 75.2, T998-05-17′) (7, ‘TARUN’ , ‘D’ , ‘M’ , 62.3, ‘1999-02-01’) Example for WHERE WITH AND OperatorIn this example we are going to display the name, Rollno and Average of students who have scored an average between 80 to 90% (both limits are inclusive) Example import sqlite3 connection = sqlite3.connect(“Academy.db”) cursor = connection.cursor( ) cursor.execute(“SELECT Rollno, Same, Average FROM student WHERE (Average>=80 AND Average<=90)”) result = cursor.fetchall( ) print(*result,sep= ”\n”) OUTPUT (1, ‘Akshay’ , 87.8) (5, ‘VARUN’ , 80.6) Example for WHERE WITH OR Operator In this example we are going to display the name and Rollno of students who have not scored an average between 60 to 70% Example import sqlite3 connection = sqlite3.connect(“Academy.db”) cursor = connection.cursor( ) cursor.execute(“SELECT Rollno,sname FROM student WHERE (Average<60 OR Average> 70)”) result = cursor. fetchall( ) print(*result,sep= ”\n”) OUTPUT (1, ‘Akshay’) (2, ‘Aravind’) (3, ‘BASKAR’) (4, ‘SAJINI’) (5, ‘VARUN’) (6, ‘PRIYA’) |
|