sqlite
  1. sqlite-and

SQLite Clauses/Conditions: AND

In SQLite, the AND operator is used to combine multiple conditions in a WHERE clause to filter rows from a table that meet all the specified conditions.

Syntax

The basic syntax for using the AND operator in a WHERE clause is as follows:

SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;

Example

Suppose we have a table called employees with columns for id, name, department, and salary. We can use the AND operator to retrieve data for employees who belong to the Sales department and earn a salary greater than $50,000.

SELECT name, department, salary
FROM employees
WHERE department = "Sales"
   AND salary > 50000;

Output

The output of the SQL query would be a table with the employee name, department, and salary for employees who belong to the Sales department and earn a salary greater than $50,000.

|  name  | department | salary |
|--------|------------|--------|
| Alice  |   Sales    | 60000  |
| Carol  |   Sales    | 55000  |

Explanation

In the example above, we use the AND operator to filter the employees table based on two conditions: department = "Sales" and salary > 50000. This means that the output will only include rows where the value in the department column is "Sales" AND the value in the salary column is greater than $50,000.

The SELECT statement then retrieves and displays the name, department, and salary columns for those filtered rows.

Use

The AND operator is commonly used in SQL queries to filter data based on multiple conditions. It allows you to narrow down your search and retrieve only the data that meets all the specified conditions.

Important Points

  • Make sure to enclose string values in single quotes when using the AND operator in a WHERE clause.
  • Use parentheses to group conditions together and control the order of evaluation.
  • You can use multiple AND operators to combine more than two conditions.
  • The AND operator has a higher precedence than the OR operator.

Summary

In this tutorial, we learned about the AND operator in SQLite and how to use it to filter data based on multiple conditions. We saw an example of using the AND operator to retrieve data for employees who belong to the Sales department and earn a salary greater than $50,000. It's important to use parentheses to group conditions together and control the order of evaluation, and to enclose string values in single quotes.

Published on: