Skip to content
Related Articles

Related Articles

Improve Article

Queries using AND ,OR ,NOT operators in MySQL

  • Last Updated : 13 Apr, 2021

AND, OR, NOT operators are basically used with WHERE clause in order to retrieve data from table by filtering with some conditions using AND, OR, NOT in MySQL.
Here in this article let us see different queries on the student table using AND, OR, NOT operators step-by-step.

Step-1:Creating a database university:

CREATE DATABASE university;

Step-2:Using the database university:

USE university;



Step-3:Creating a table student:

CREATE TABLE student(
student_id INT
student_name VARCHAR(20)
birth_date DATE
branch VARCHAR(20)
state VARCHAR(20));

Step-4:Viewing the description of the table student:

DESCRIBE student;

Step-5:Adding rows into student table:

INSERT INTO student VALUES(194001,'PRANAB','1999-03-17','CSE','PUNJAB');
INSERT INTO student VALUES(194002,'PRAKASH','2000-08-07','ECE','TAMIL NADU');
INSERT INTO student VALUES(194003,'ROCKY','2000-03-10','ECE','PUNJAB');
INSERT INTO student VALUES(194004,'TRIBHUVAN','1999-03-15','CSE','ANDHRA PRADESH');
INSERT INTO student VALUES(194005,'VAMSI','2000-04-19','CSE','TELANGANA');

Step-6:Viewing the rows in the table:

SELECT * FROM student;



syntax for AND operator:
SELECT * FROM table_name
WHERE condition1 AND condition2 AND ….CONDITIONn;

Example-1:
Query to find student records with a branch with CSE and  state with PUNJAB using AND operator in MySQL:

SELECT *
    FROM student
    WHERE branch='CSE'ANDstate='PUNJAB'

All the students with branch CSE and from Punjab.

syntax for OR operator:
SELECT * FROM table_name
WHERE condition1 OR condition2 OR….CONDITIONn;

Example-2:
Query to find student records with a branch with CSE or ECE using OR operator in MySQL:

All the student with branch either CSE or ECE.

syntax for NOT operator:
SELECT * FROM table_name
WHERE NOT condition1 NOT condition2 NOT...CONDITIONn;

Example-3:
Query to find student records who were out of PUNJAB using NOT operator in MySQL:

SELECT *
      FROM student
      WHERE NOT state='PUNJAB';

All the students from states other than Punjab.

Attention reader! Don’t stop learning now. Learn SQL for interviews using SQL Course  by GeeksforGeeks.

My Personal Notes arrow_drop_up
Recommended Articles
Page :