SQL Tutorial
SQL Clauses / Operators
SQL-Injection
SQL Functions
SQL Queries
PL/SQL
MySQL
SQL Server
Misc
The WHERE
clause in SQL is used to filter records based on specific conditions. It allows you to specify the criteria to select, update, or delete data from tables. The WHERE
clause is typically used in conjunction with SQL statements like SELECT
, UPDATE
, and DELETE
.
For the SELECT
statement:
SELECT column1, column2, ... FROM table_name WHERE condition;
For the UPDATE
statement:
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
For the DELETE
statement:
DELETE FROM table_name WHERE condition;
Selecting with a Simple Condition: To select all employees with a salary greater than 50,000:
SELECT * FROM employees WHERE salary > 50000;
Using Logical Operators: To select all employees with a salary between 30,000 and 50,000:
SELECT * FROM employees WHERE salary >= 30000 AND salary <= 50000;
Combining Conditions: To select all employees in department 'Sales' with a salary greater than 40,000:
SELECT * FROM employees WHERE department = 'Sales' AND salary > 40000;
Using IN
:
To select employees in either department 'Sales' or 'Marketing':
SELECT * FROM employees WHERE department IN ('Sales', 'Marketing');
Using LIKE
:
To select employees whose names start with 'J':
SELECT * FROM employees WHERE name LIKE 'J%';
Using BETWEEN
:
To select employees with salaries between 30,000 and 50,000:
SELECT * FROM employees WHERE salary BETWEEN 30000 AND 50000;
WHERE
clause with the UPDATE
and DELETE
statements. Without a WHERE
clause, these statements will update or delete all rows in the table.WHERE
clause can improve the performance of queries.In conclusion, the WHERE
clause is a fundamental component of SQL, providing powerful filtering capabilities to ensure that operations are applied to the appropriate data sets.
How to Use WHERE in SQL:
WHERE
clause is used in SQL to filter rows based on a specified condition.-- Basic WHERE usage SELECT column1, column2 FROM table_name WHERE condition;
Common Conditions in WHERE Clauses:
-- Common conditions SELECT * FROM employees WHERE salary > 50000 AND department_id = 10;
Combining Conditions with AND and OR in WHERE:
AND
and OR
operators.-- Combining conditions SELECT * FROM products WHERE category = 'Electronics' AND price > 1000;
Using Comparison Operators in WHERE Clause:
=
, <>
, <
, >
, <=
, and >=
are used in WHERE clauses.-- Using comparison operators SELECT * FROM orders WHERE order_date >= '2022-01-01';
Handling NULL Values with WHERE in SQL:
IS NULL
and IS NOT NULL
conditions are used to filter NULL values.-- Handling NULL values SELECT * FROM customers WHERE email IS NULL;
WHERE Clause with LIKE for Pattern Matching:
LIKE
operator is used for pattern matching with wildcard characters %
and _
.-- Using LIKE for pattern matching SELECT * FROM products WHERE product_name LIKE 'Laptop%';
BETWEEN and IN Operators in WHERE Clause:
BETWEEN
is used to filter within a range, and IN
is used for matching any value in a set.-- Using BETWEEN and IN SELECT * FROM orders WHERE order_date BETWEEN '2022-01-01' AND '2022-12-31' OR customer_id IN (1, 2, 3);
WHERE Clause with Aggregate Functions in SQL:
SUM
, AVG
, COUNT
, etc.-- Using WHERE with aggregate functions SELECT department_id, AVG(salary) AS avg_salary FROM employees GROUP BY department_id HAVING AVG(salary) > 50000;
Subqueries in WHERE Clauses:
-- Using subqueries in WHERE SELECT * FROM employees WHERE department_id IN (SELECT department_id FROM departments WHERE location_id = 1700);
EXISTS and NOT EXISTS in WHERE Clause:
-- Using EXISTS and NOT EXISTS SELECT * FROM customers c WHERE EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.customer_id);
WHERE vs. HAVING in SQL:
WHERE
is used for filtering rows, and HAVING
is used for filtering groups when using aggregate functions.-- Using WHERE and HAVING SELECT department_id, AVG(salary) AS avg_salary FROM employees WHERE salary > 50000 GROUP BY department_id HAVING AVG(salary) > 60000;
Dynamic Values in WHERE Clause:
DECLARE @salary_threshold INT = 50000; SELECT * FROM employees WHERE salary > @salary_threshold;
WHERE Clause with Date Conditions in SQL:
=
, <
, >
, BETWEEN
, etc.-- Using WHERE with date conditions SELECT * FROM orders WHERE order_date >= '2023-01-01' AND order_date < '2023-02-01';