AND
AND is a logical operator in SQL that combines two or more conditions. It returns true only if all conditions are true.
Example
SELECT * FROM CustomersWHERE Country='Germany' AND City='Berlin';
Output
CustomerID | CustomerName | ContactName | Address | City | PostalCode | Country--- | --- | --- | --- | --- | --- | ---1 | Alfreds | Maria | Obere Str. 57| Berlin | 12209 | Germany2 | Blauer See | Henna | Forsterstr. 57| Berlin | 14163 | Germany
Explanation
The code illustrates a simple usage of the AND
logic operator in SQL. AND
is used to filter records based on multiple conditions. In this case, it selects customers from the Customers
table that are both in the Country
‘Germany’ and the City
‘Berlin’. The output provides a list of customer details who fulfill both conditions.
Example
SELECT *FROM ordersWHERE order_date >= '2020-01-01' AND total_amount > 100;
Output
| order_id | customer_id | order_date | total_amount ||----------|-------------|-------------|--------------|| 00001 | CUST002 | 2020-05-01 | 150 || 00002 | CUST001 | 2020-10-20 | 200 |
Explanation
The above SQL statement selects all columns from the orders
table where the order_date
is greater than or equal to ‘2020-01-01’ AND the total_amount
is greater than 100. Thus, it only returns records that meet both conditions.
Example
SELECT *FROM [Employees]WHERE [Salary] > 50000 AND [Age] < 30;
Output
EmployeeID FirstName LastName Salary Age----------------------------------------------3 John Doe 60000 285 Jane Smith 55000 27
Explanation
In this example, the SQL query is used to get data from the Employees
table for those employees who both earn more than 50000
and are younger than 30
years. The AND
logical operator ensures both conditions are satisfied for the rows returned from the query.
Example
SELECT *FROM employeesWHERE salary > 50000 AND job_id = 'IT_PROG';
Output
| EMPLOYEE_ID | FIRST_NAME | LAST_NAME | EMAIL | PHONE_NUMBER | HIRE_DATE | JOB_ID | SALARY | MANAGER_ID | DEPARTMENT_ID ||-------------|------------|-----------|-------|--------------|-----------|--------|--------|------------|---------------|| 200 | Jennifer | Whalen | JWHALEN| 515.123.4444 | 17-JUN-87 | IT_PROG| 60000 | 101 | 10 || 205 | Shelley | Higgins | SHIGGINS| 515.123.8080 | 07-JUN-90 | IT_PROG| 120000 | 101 | 10 |
Explanation
In the SQL query provided in the example, it selects all data from the ‘employees’ table where the salary is greater than 50000 AND job_id is ‘IT_PROG’. The AND operator combines two Boolean expressions and returns true only if both expressions are true. Therefore, only rows that fulfill both conditions are shown in the output.
Example
SELECT Firstname, LastnameFROM CustomersWHERE Country='Germany' AND City='Berlin';
Output
| Firstname | Lastname ||-----------|----------|| John | Doe |
Explanation
The above SQL statement selects customers from the “Customers” table that are located in “Berlin”, Germany. The AND operator combines two conditions that must both be true for the row to be included in the result set.