|
| 1 | +--- |
| 2 | +id: sql-where-clause |
| 3 | +title: Where Clause in SQL |
| 4 | +sidebar_label: Where Clause |
| 5 | +sidebar_position: 3 |
| 6 | +tags: [sql, database, clause] |
| 7 | +description: In this tutorial, you will learn how to build queries with conditions to get the desired output. |
| 8 | +--- |
| 9 | + |
| 10 | +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. |
| 11 | + |
| 12 | +## Syntax |
| 13 | +```sql |
| 14 | +SELECT column1, column2, ... |
| 15 | +FROM table_name |
| 16 | +WHERE condition; |
| 17 | +``` |
| 18 | + |
| 19 | +## Operators Used in the WHERE Clause |
| 20 | +1. `=` : Equal |
| 21 | +2. `>` : Greater than |
| 22 | +3. `<` : Less than |
| 23 | +4. `>=` : Greater than or equal |
| 24 | +5. `<=` : Less than or equal |
| 25 | +6. `<>` : Not equal (Note: In some versions of SQL, this operator may be written as `!=`) |
| 26 | +7. `BETWEEN` : Between a certain range |
| 27 | +8. `LIKE` : Search for a pattern |
| 28 | +9. `IN` : To specify multiple possible values for a column |
| 29 | + |
| 30 | +## Advantages of SQL WHERE Clause |
| 31 | + |
| 32 | +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. |
| 33 | +2. **Conditions:** Conditions in the WHERE clause can use comparison operators like `=`, `<>` (not equal), `>`, `<`, `>=`, `<=`. Logical operators such as `AND`, `OR`, and `NOT` can be used to combine multiple conditions. |
| 34 | +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'. |
| 35 | +4. **Range Checks:** The BETWEEN operator checks if a value is within a range of values. For example, `BETWEEN 10 AND 20`. |
| 36 | +5. **Null Values:** The `IS NULL` and `IS NOT NULL` operators are used to filter records with null values. |
| 37 | + |
| 38 | +## Examples of WHERE Clause in SQL |
| 39 | + |
| 40 | +### Basic Select Query |
| 41 | +```sql |
| 42 | +SELECT * FROM Students WHERE marks > 50; |
| 43 | +``` |
| 44 | + |
| 45 | +### WHERE Clause in UPDATE Statement |
| 46 | +```sql |
| 47 | +UPDATE employees SET salary = salary * 1.10 |
| 48 | +WHERE performance_rating = 'Excellent'; |
| 49 | +``` |
| 50 | + |
| 51 | +### WHERE Clause in DELETE Statement |
| 52 | +```sql |
| 53 | +DELETE FROM employees |
| 54 | +WHERE last_login < '2023-01-01'; |
| 55 | +``` |
| 56 | + |
| 57 | +### WHERE Clause with LIKE Statement |
| 58 | +```sql |
| 59 | +SELECT * FROM customers |
| 60 | +WHERE name LIKE 'J%'; |
| 61 | +``` |
| 62 | + |
| 63 | +### WHERE Clause with BETWEEN Statement |
| 64 | +```sql |
| 65 | +SELECT * FROM orders |
| 66 | +WHERE order_date BETWEEN '2023-01-01' AND '2023-12-31'; |
| 67 | +``` |
| 68 | + |
| 69 | +### WHERE Clause with IS NULL Statement |
| 70 | +```sql |
| 71 | +SELECT * FROM employees |
| 72 | +WHERE manager_id IS NULL; |
| 73 | +``` |
| 74 | + |
| 75 | +## Conclusion |
| 76 | +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. |
| 77 | + |
| 78 | +--- |
| 79 | + |
| 80 | +## Authors: |
| 81 | + |
| 82 | +<div style={{display: 'flex', flexWrap: 'wrap', justifyContent: 'space-between', gap: '10px'}}> |
| 83 | + {['damini-chachane'].map(username => ( |
| 84 | + <Author key={username} username={username} /> |
| 85 | + ))} |
| 86 | +</div> |
0 commit comments