SQL Tutorial

SQL Introduction SQL Aggregate Functions SQL Aliases SQL And SQL Any All SQL Avg SQL Between SQL Case SQL Comments SQL Count SQL Delete SQL Distinct SQL Exists SQL Groupby SQL Having SQL In SQL Insert_into SQL Is Not Null SQL Join SQL Full Outer Join SQL Inner Join SQL Left Join SQL Right Join SQL Self Join SQL Like SQL Min Max SQL NOT Operator SQL Null SQL Operators SQL OR operator SQL OrderBy SQL Select SQL Select Into SQL Top Limit Fetch SQL Store Procedures SQL Sum SQL Union SQL Update SQL Where SQL Wildcards

SQL Database

SQL Alter Table SQL Auto increment SQL BackupDB SQL Check SQL Constrains SQL Create View SQL CreateDB SQL CreateTable SQL Data types SQL Dates SQL DefaultConstrain SQL DropDB SQL DropTable SQL Foreign Key SQL Hosting SQL Index SQL injections SQL Not NULL SQL PrimaryKey SQL Unique SQL Views

SQL AND Tutorial

Example Table

We will use the following table named employees for our examples:


        CREATE TABLE employees (
            id INT PRIMARY KEY,
            first_name VARCHAR(50),
            last_name VARCHAR(50),
            department VARCHAR(50),
            salary DECIMAL(10, 2)
        );

        INSERT INTO employees (id, first_name, last_name, department, salary) VALUES
        (1, 'John', 'Doe', 'Engineering', 75000.00),
        (2, 'Jane', 'Smith', 'Marketing', 65000.00),
        (3, 'Sam', 'Brown', 'Engineering', 80000.00),
        (4, 'Sue', 'Johnson', 'HR', 60000.00),
        (5, 'Mike', 'Davis', 'Marketing', 70000.00);
        
id first_name last_name department salary
1 John Doe Engineering 75000.00
2 Jane Smith Marketing 65000.00
3 Sam Brown Engineering 80000.00
4 Sue Johnson HR 60000.00
5 Mike Davis Marketing 70000.00

Using SQL AND

The SQL AND operator is used to combine multiple conditions in a WHERE clause. All conditions must be true for the row to be included in the result set.

Example 1: Filtering by Department and Salary

SELECT first_name, last_name, department, salary FROM employees WHERE department = 'Engineering' AND salary > 75000;

Result:

first_name Sam
last_name Brown
department Engineering
salary 80000.00

Example 2: Filtering by Department and Last Name

SELECT first_name, last_name, department, salary FROM employees WHERE department = 'Marketing' AND last_name = 'Davis';

Result:

first_name Mike
last_name Davis
department Marketing
salary 70000.00