|
| 1 | +# Introduction to SQL WHERE Clause |
| 2 | + |
| 3 | +The WHERE clause in SQL is used to filter records from a result set. It specifies the conditions that must be met for the rows to be included in the result. The WHERE clause is often used in SELECT, UPDATE, DELETE, and other SQL statements to narrow down the data returned or affected. |
| 4 | + |
| 5 | +# Syntax |
| 6 | +`SELECT column1, column2, ... |
| 7 | +FROM table_name |
| 8 | +WHERE condition;` |
| 9 | + |
| 10 | +# Operators Used in the Where Clause |
| 11 | +1. `= : Equal`. |
| 12 | +2. `> : Greater than`. |
| 13 | +3. `< : Less than`. |
| 14 | +4. `>= : Greater than or equal`. |
| 15 | +5. `<= : Less than or equal`. |
| 16 | +6. `<> : Not equal. Note: In some versions of SQL this operator may be written as !=`. |
| 17 | +7. `BETWEEN : Between a certain range`. |
| 18 | +8. `LIKE : Search for a pattern`. |
| 19 | +9. `IN : To specify multiple possible values for a column`. |
| 20 | +# Advantage of SQL WHERE Clause |
| 21 | + |
| 22 | +**1. Filtering Rows:** The WHERE clause evaluates each row in the table to determine if it meets the specified condition(s). Only rows that satisfy the condition are included in the result set. |
| 23 | + |
| 24 | +**2. Conditions:** Conditions in the WHERE clause can use comparison operators like =, <> (not equal), >, <, >=, <=. |
| 25 | +Logical operators such as AND, OR, and NOT can be used to combine multiple conditions. |
| 26 | +**3. Pattern Matching:** The LIKE operator can be used for pattern matching. For example, LIKE 'A%' matches any string that starts with the letter 'A'. |
| 27 | +**4. Range Checks:** The BETWEEN operator checks if a value is within a range of values. For example, BETWEEN 10 AND 20. |
| 28 | +**5. Null Values:** The IS NULL and IS NOT NULL operators are used to filter records with null values. |
| 29 | + |
| 30 | +# Example of Where Clause in SQL |
| 31 | + |
| 32 | +Syntax: SELECT * from <Table_Name>; |
| 33 | +Example : SELECT * from Students where marks >50; |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +**Where Clause use in Update Statement** |
| 38 | +`UPDATE employees SET salary = salary * 1.10 |
| 39 | +WHERE performance_rating = 'Excellent';` |
| 40 | + |
| 41 | +**Where Clause use in Delete Statement** |
| 42 | +`DELETE FROM employees |
| 43 | +WHERE last_login < '2023-01-01';` |
| 44 | + |
| 45 | + |
| 46 | +**Where Clause use in Like Statement** |
| 47 | +`SELECT * FROM customers |
| 48 | +WHERE name LIKE 'J%';` |
| 49 | + |
| 50 | + |
| 51 | +**Where Clause use in Between Statement** |
| 52 | +`SELECT * FROM orders |
| 53 | +WHERE order_date BETWEEN '2023-01-01' AND '2023-12-31';` |
| 54 | + |
| 55 | + |
| 56 | +**Where Clause use in IS NULL Statement** |
| 57 | + |
| 58 | +`SELECT * FROM employees |
| 59 | +WHERE manager_id IS NULL;` |
| 60 | + |
| 61 | +# Conclusion |
| 62 | +The WHERE clause in SQL is a powerful tool for filtering data in various SQL statements. It allows you to specify conditions that rows must meet to be included in the result set, thereby enabling precise data retrieval and manipulation. By using comparison operators, logical operators, pattern matching, range checks, and handling null values, you can create complex queries tailored to your specific data requirements. Mastering the WHERE clause is essential for efficient database management and analysis, providing the ability to focus on relevant data and perform targeted updates and deletions. |
0 commit comments