🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
In this chapter, we will learn about the NOT operator in MySQL. The NOT operator is used to negate a condition in SQL queries. It is useful for filtering data that does not meet a specified condition. We will cover the syntax for the NOT operator, a complete example of its usage, and important considerations for using it in MySQL.
Syntax
The NOT operator can be used in various SQL clauses to negate conditions. Here are some common usages:
- With WHERE Clause:
SELECT column1, column2
FROM table_name
WHERE NOT condition;
- With BETWEEN Operator:
SELECT column1, column2
FROM table_name
WHERE column1 NOT BETWEEN value1 AND value2;
- With IN Operator:
SELECT column1, column2
FROM table_name
WHERE column1 NOT IN (value1, value2, ...);
- With LIKE Operator:
SELECT column1, column2
FROM table_name
WHERE column1 NOT LIKE pattern;
Complete Example
Let's go through a complete example where we create a database and table, insert data, and demonstrate the usage of the NOT operator.
- Create a Database and Table
CREATE DATABASE company;
USE company;
CREATE TABLE employees (
employee_id INT PRIMARY KEY AUTO_INCREMENT,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
department VARCHAR(50),
salary DECIMAL(10, 2)
);
INSERT INTO employees (first_name, last_name, department, salary) VALUES
('Rahul', 'Sharma', 'Sales', 50000.00),
('Priya', 'Singh', 'Marketing', 60000.00),
('Amit', 'Kumar', 'IT', 70000.00),
('Neha', 'Verma', 'Sales', 55000.00),
('Sahil', 'Mehta', 'IT', 62000.00);
- Using NOT with WHERE Clause
We will use the NOT operator to select employees who are not in the 'Sales' department.
SELECT first_name, last_name, department
FROM employees
WHERE NOT department = 'Sales';
Output:
| first_name | last_name | department |
|---|---|---|
| Priya | Singh | Marketing |
| Amit | Kumar | IT |
| Sahil | Mehta | IT |
- Using NOT with BETWEEN Operator
We will use the NOT operator with the BETWEEN operator to select employees whose salary is not between 55000 and 65000.
SELECT first_name, last_name, salary
FROM employees
WHERE salary NOT BETWEEN 55000 AND 65000;
Output:
| first_name | last_name | salary |
|---|---|---|
| Rahul | Sharma | 50000.00 |
| Amit | Kumar | 70000.00 |
- Using NOT with IN Operator
We will use the NOT operator with the IN operator to select employees whose department is not 'Sales' or 'IT'.
SELECT first_name, last_name, department
FROM employees
WHERE department NOT IN ('Sales', 'IT');
Output:
| first_name | last_name | department |
|---|---|---|
| Priya | Singh | Marketing |
- Using NOT with LIKE Operator
We will use the NOT operator with the LIKE operator to select employees whose first name does not start with 'S'.
SELECT first_name, last_name
FROM employees
WHERE first_name NOT LIKE 'S%';
Output:
| first_name | last_name |
|---|---|
| Rahul | Sharma |
| Priya | Singh |
| Amit | Kumar |
| Neha | Verma |
Important Considerations
- Logical Negation: The
NOToperator is used to negate conditions. Ensure that the logic of your query is correctly structured to get the desired results. - Performance: Using the
NOToperator in queries can sometimes impact performance, especially on large datasets. Optimize your queries by ensuring that the conditions are well-indexed. - Combining Conditions: The
NOToperator can be combined with other logical operators likeANDandORto form complex conditions.
Conclusion
The NOT operator in MySQL is used for negating conditions in SQL queries. This chapter covered the syntax for using the NOT operator, provided a complete example of its usage, and discussed important considerations. By mastering the NOT operator, you can efficiently filter data that does not meet specific conditions in your databases.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment