Home | | Computer Science 12th Std | The SQL AND, OR and NOT Operators

Chapter: 12th Computer Science : Chapter 15 : Integrating Python with MySql and C++ : Data Manipulation Through SQL

The SQL AND, OR and NOT Operators

The WHERE clause can be combined with AND, OR, and NOT operators.

The SQL AND, OR and NOT Operators

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, '1998-05-17')

(7, 'TARUN', 'D', 'M', 62.3, '1999-02-01')


Example for WHERE WITH AND Operator

In 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')

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
12th Computer Science : Chapter 15 : Integrating Python with MySql and C++ : Data Manipulation Through SQL : The SQL AND, OR and NOT Operators |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.