LIVE Course for free

Rated by 1 million+ students
Get app now
JEE MAIN 2023
JEE MAIN 2023 TEST SERIES
NEET 2023 TEST SERIES
NEET 2023
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
448 views
in Data Manipulation Through SQL by (53.1k points)
closed by

Explain OR, AND and NOT operator in SQL?

1 Answer

+1 vote
by (57.9k points)
selected by
 
Best 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’)

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

...