SQL Tutorial

SQL Clauses / Operators

SQL-Injection

SQL Functions

SQL Queries

PL/SQL

MySQL

SQL Server

Misc

SQL | WHERE Clause

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.

Basic Syntax:

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;

Examples:

  1. Selecting with a Simple Condition: To select all employees with a salary greater than 50,000:

    SELECT * FROM employees
    WHERE salary > 50000;
    
  2. 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;
    
  3. 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;
    
  4. Using IN: To select employees in either department 'Sales' or 'Marketing':

    SELECT * FROM employees
    WHERE department IN ('Sales', 'Marketing');
    
  5. Using LIKE: To select employees whose names start with 'J':

    SELECT * FROM employees
    WHERE name LIKE 'J%';
    
  6. Using BETWEEN: To select employees with salaries between 30,000 and 50,000:

    SELECT * FROM employees
    WHERE salary BETWEEN 30000 AND 50000;
    

Tips:

  • Always be cautious when using the WHERE clause with the UPDATE and DELETE statements. Without a WHERE clause, these statements will update or delete all rows in the table.
  • Use parameterized queries or prepared statements in application code to avoid SQL injection when conditions are derived from user input.
  • Proper indexing on columns used in the 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.

  1. How to Use WHERE in SQL:

    • Description: The WHERE clause is used in SQL to filter rows based on a specified condition.
    • Code:
      -- Basic WHERE usage
      SELECT column1, column2
      FROM table_name
      WHERE condition;
      
  2. Common Conditions in WHERE Clauses:

    • Description: Common conditions include equality, inequality, greater than, less than, etc.
    • Code:
      -- Common conditions
      SELECT * FROM employees
      WHERE salary > 50000 AND department_id = 10;
      
  3. Combining Conditions with AND and OR in WHERE:

    • Description: Multiple conditions can be combined using AND and OR operators.
    • Code:
      -- Combining conditions
      SELECT * FROM products
      WHERE category = 'Electronics' AND price > 1000;
      
  4. Using Comparison Operators in WHERE Clause:

    • Description: Comparison operators like =, <>, <, >, <=, and >= are used in WHERE clauses.
    • Code:
      -- Using comparison operators
      SELECT * FROM orders
      WHERE order_date >= '2022-01-01';
      
  5. Handling NULL Values with WHERE in SQL:

    • Description: The IS NULL and IS NOT NULL conditions are used to filter NULL values.
    • Code:
      -- Handling NULL values
      SELECT * FROM customers
      WHERE email IS NULL;
      
  6. WHERE Clause with LIKE for Pattern Matching:

    • Description: The LIKE operator is used for pattern matching with wildcard characters % and _.
    • Code:
      -- Using LIKE for pattern matching
      SELECT * FROM products
      WHERE product_name LIKE 'Laptop%';
      
  7. BETWEEN and IN Operators in WHERE Clause:

    • Description: BETWEEN is used to filter within a range, and IN is used for matching any value in a set.
    • Code:
      -- 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);
      
  8. WHERE Clause with Aggregate Functions in SQL:

    • Description: Filtering based on aggregate functions like SUM, AVG, COUNT, etc.
    • Code:
      -- Using WHERE with aggregate functions
      SELECT department_id, AVG(salary) AS avg_salary
      FROM employees
      GROUP BY department_id
      HAVING AVG(salary) > 50000;
      
  9. Subqueries in WHERE Clauses:

    • Description: Using subqueries to filter rows based on the results of another query.
    • Code:
      -- Using subqueries in WHERE
      SELECT * FROM employees
      WHERE department_id IN (SELECT department_id FROM departments WHERE location_id = 1700);
      
  10. EXISTS and NOT EXISTS in WHERE Clause:

    • Description: Checking for the existence or non-existence of rows in a subquery.
    • Code:
      -- Using EXISTS and NOT EXISTS
      SELECT * FROM customers c
      WHERE EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.customer_id);
      
  11. WHERE vs. HAVING in SQL:

    • Description: WHERE is used for filtering rows, and HAVING is used for filtering groups when using aggregate functions.
    • Code:
      -- 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;
      
  12. Dynamic Values in WHERE Clause:

    • Description: Using variables or parameters to dynamically change the conditions.
    • Code: (Syntax varies based on the database system; here's a generic example)
      DECLARE @salary_threshold INT = 50000;
      SELECT * FROM employees
      WHERE salary > @salary_threshold;
      
  13. WHERE Clause with Date Conditions in SQL:

    • Description: Filtering rows based on date conditions using operators like =, <, >, BETWEEN, etc.
    • Code:
      -- Using WHERE with date conditions
      SELECT * FROM orders
      WHERE order_date >= '2023-01-01' AND order_date < '2023-02-01';