SQL AND Condition in SQL Queries

SQL AND Condition in SQL Queries

SQL AND Condition

The SQL AND condition is a powerful operator used in SQL queries to ensure that multiple conditions must be met simultaneously. It is commonly used in SELECT, INSERT, UPDATE, and DELETE statements.

Complete Python Course with Advance topics:-Click Here
SQL Tutorial :-Click Here

Syntax for SQL AND:

SELECT columns FROM tables WHERE condition_1 AND condition_2;

The AND condition requires both conditions to be true for the record to be included in the results.

Additionally, the AND condition can be used to join multiple tables in an SQL statement.

To better understand its application, let’s consider an example with an employees table that contains the following data:

Sample Employee Table

ID First_Name Last_Name Department Location
1 Rahul Sharma HR Mumbai
2 Neha Verma IT Delhi
3 Aditya Joshi IT Bangalore
4 Priya Mehta IT Bangalore
5 Rakesh Sinha Marketing Pune
6 Anjali Gupta Finance Kolkata
7 Suman Das Finance Kolkata

SQL AND with SELECT Statement

Example 1: Retrieving Employees in IT Department Located in Bangalore

SELECT * FROM employees WHERE Department = 'IT' AND Location = 'Bangalore';

Output:

ID First_Name Last_Name Department Location
3 Aditya Joshi IT Bangalore
4 Priya Mehta IT Bangalore

Here, only two employees match both conditions: belonging to the IT department and being located in Bangalore.

Example 2: Retrieving Employees in IT Department Located in Delhi

SELECT * FROM employees WHERE Department = 'IT' AND Location = 'Delhi';

Output:

ID First_Name Last_Name Department Location
2 Neha Verma IT Delhi

Only one employee in the IT department is located in Delhi, so only one record is returned.

SQL AND with UPDATE Statement

Example 1: Updating Employee’s Location from Pune to Hyderabad in the Marketing Department

UPDATE employees SET Location = 'Hyderabad' WHERE Department = 'Marketing' AND First_Name = 'Rakesh';

Verifying the Update

SELECT * FROM employees;

Updated Table:

ID First_Name Last_Name Department Location
1 Rahul Sharma HR Mumbai
2 Neha Verma IT Delhi
3 Aditya Joshi IT Bangalore
4 Priya Mehta IT Bangalore
5 Rakesh Sinha Marketing Hyderabad
6 Anjali Gupta Finance Kolkata
7 Suman Das Finance Kolkata

Example 2: Updating Employee’s Department from Finance to HR

UPDATE employees SET Department = 'HR' WHERE Department = 'Finance' AND ID = 7;

Verifying the Update

SELECT * FROM employees;

Updated Table:

ID First_Name Last_Name Department Location
1 Rahul Sharma HR Mumbai
2 Neha Verma IT Delhi
3 Aditya Joshi IT Bangalore
4 Priya Mehta IT Bangalore
5 Rakesh Sinha Marketing Hyderabad
6 Anjali Gupta Finance Kolkata
7 Suman Das HR Kolkata

SQL AND with DELETE Statement

Example 1: Deleting an Employee with Last Name ‘Das’ and Located in Kolkata

DELETE FROM employees WHERE Last_Name = 'Das' AND Location = 'Kolkata';

Verifying the Deletion

SELECT * FROM employees;

Updated Table:

ID First_Name Last_Name Department Location
1 Rahul Sharma HR Mumbai
2 Neha Verma IT Delhi
3 Aditya Joshi IT Bangalore
4 Priya Mehta IT Bangalore
5 Rakesh Sinha Marketing Hyderabad
6 Anjali Gupta Finance Kolkata

The employee with the last name ‘Das’ and location ‘Kolkata’ has been successfully deleted.

Example 2: Deleting an Employee from IT Department Located in Delhi

DELETE FROM employees WHERE Department = 'IT' AND Location = 'Delhi';

Verifying the Deletion

SELECT * FROM employees;

Updated Table:

ID First_Name Last_Name Department Location
1 Rahul Sharma HR Mumbai
3 Aditya Joshi IT Bangalore
4 Priya Mehta IT Bangalore
5 Rakesh Sinha Marketing Hyderabad
6 Anjali Gupta Finance Kolkata

Since only one employee in the IT department was located in Delhi, only that record was deleted.

Download New Real Time Projects :-Click here
Complete Advance AI topics:- CLICK HERE

Conclusion

The SQL AND condition is crucial for filtering records based on multiple conditions. It ensures that only those records that meet all specified criteria are retrieved, updated, or deleted. Using AND effectively can enhance the accuracy and efficiency of your SQL queries.

For more such SQL tutorials, visit UpdateGadh!


sql where multiple conditions
SQL AND Condition in SQL Queries
sql or statement multiple values
not operator in sql
sql like
sql not equal
sql contains
sql and or precedence
sql between
sql and condition in oracle
sql and condition in dbms
sql and condition w3schools
sql and condition examples

Post Comment