Open In App

Queries using AND ,OR ,NOT operators in MySQL

Last Updated : 21 Jun, 2022
Like Article

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:


Step-2:Using the database university:

USE university;

Step-3:Creating a 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;

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

    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;

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

    FROM student
    WHERE branch='CSE' OR branch='ECE';

All the students with branch either CSE or ECE.

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

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

      FROM student
      WHERE NOT state='PUNJAB';

All the students were from states other than Punjab.

Previous Article
Next Article

Similar Reads

MySQL | Common MySQL Queries
MySQL server is a open-source relational database management system which is a major support for web based applications. Databases and related tables are the main component of many websites and applications as the data is stored and exchanged over the web. Even all social networking websites mainly Facebook, Twitter, and Google depends on MySQL dat
9 min read
Identify and Kill Queries with MySQL Command
In SQL, some unnecessary processes can degrade your system's performance. Over time, threads pile up and stall your server, preventing users from accessing tables and executing requests. When resource usage is extremely high, you may need to kill MySQL processes. To do this, we first need to identify the processes that are running. We will use the
3 min read
MySQL - Queries on Date Manipulation
In MySQL date is a data type and by default follows the format 'YYYY-MM-DD' but by using date formatting functions we can format date and can get the required format of date in the tables and can even manipulate the date by using different date functions. In this article let us execute some queries on date formatting in detail step-by-step: Creatin
2 min read
How to Connect to Mysql Server Using VS Code and Fix errors?
MySQL is a relational database management system based on SQL-Structured Query Language used for accessing and managing records in a database. It can be easily connected with programming languages such as Python, Java, and PHP to serve various purposes that require CRUD ( Create,Read,Update,Delete) operations. MySQL is based on a client – server ar
4 min read
How to add Image to MySql database using Servlet and JDBC
Structured Query Language or SQL is a standard Database language which is used to create, maintain and retrieve the data from relational databases like MySQL, Oracle, SQL Server, PostGre, etc. In this article, we will understand how to add an image to the MYSQL database using servlet. MYSQL is a relational database. Relational database means the da
6 min read
How to Display Recent Posts in DOM using PHP and MySQL ?
Many times, we are very familiar with "latest blogs" or "recent posts” in the websites. So now, here is a simple solution for displaying the recent blogs on the DOM by using PHP and MySQLi. Requirements: Database and tablesXAMPP ServerEditor (VS Code in this article). Note: The bootstrap required in the code can be downloaded from Bootstrap Downloa
5 min read
Creating an activate/deactivate button using PHP and MySQL
In this article, we will discuss how to create an Activate/Deactivate button using PHP. When a particular status is set, the same gets updated in the database. Approach: In order to illustrate the example, let's say that there are a few courses that can be set as active or inactive and need to have appropriate colored buttons for the same. The impl
4 min read
How to Make a Project Using Spring Boot, MySQL, Spring Data JPA, and Maven?
For the sample project, below mentioned tools got used Java 8Eclipse IDE for developmentHibernate ORM, Spring framework with Spring Data JPAMySQL database, MySQL Connector Java as JDBC driver.Example Project Using Spring Boot, MySQL, Spring Data JPA, and Maven Project Structure: [caption width="800"] [/caption] As this is getting prepared as a mave
4 min read
How to Create Todo List API using Spring Boot and MySQL?
Spring Boot is built on the top of the spring and contains all the features of spring. And is becoming a favorite of developers these days because of its rapid production-ready environment which enables the developers to directly focus on the logic instead of struggling with the configuration and setup. Spring Boot is a microservice-based framework
6 min read
Hibernate Example using JPA and MySQL
Hibernate is a framework that provides some abstraction layer, meaning that the programmer does not have to worry about the implementations, Hibernate does the implementations for you internally like writing queries to perform CRUD operations, establishing a connection with the database, etc. It is a Java framework that is used to develop persisten
4 min read
Article Tags :