WHERE Condition in SQL
WHERE Condition in SQL
The WHERE condition in SQL is an essential clause used to filter records based on a specified condition. It helps retrieve only those rows that meet the defined criteria, ensuring precise data extraction. Whether used in a SELECT, UPDATE, or DELETE statement, the WHERE condition plays a crucial role in database operations by applying filters to retrieve only relevant records.
Complete Python Course with Advance topics:-Click Here
SQL Tutorial :-Click Here
Understanding the WHERE Condition
The WHERE condition is used when fetching data from a single table or multiple joined tables. If the given condition is met, only the qualifying rows are returned. This helps in extracting accurate data while optimizing database queries efficiently.
Syntax of WHERE Condition
SELECT column1, column2, ..... columnN
FROM table_name
WHERE {condition};
Key Points About WHERE Condition:
- The WHERE condition is not limited to the SELECT statement; it is also used with UPDATE and DELETE statements.
- It supports various comparison and logical operators such as
=
,>
,<
,LIKE
,NOT
,BETWEEN
,IN
, etc. - It is case-sensitive in some databases and requires exact string matches within single quotes (
''
).
Example of WHERE Condition in SQL
Let’s consider a sample table named EMPLOYEES, which contains employee details:
EmployeeID | EmployeeName | Department | Age | Salary |
---|---|---|---|---|
101 | Aryan Sharma | IT | 27 | 55000.00 |
102 | Meera Raj | HR | 30 | 62000.00 |
103 | Sohan Verma | Finance | 28 | 48000.00 |
104 | Kiran Das | Marketing | 32 | 72000.00 |
105 | Rohan Mehta | IT | 26 | 51000.00 |
Fetching Employees with Salary Greater than 50,000
To retrieve EmployeeID, EmployeeName, and Salary for employees earning more than 50,000, we use the following query:
SELECT EmployeeID, EmployeeName, Salary
FROM EMPLOYEES
WHERE Salary > 50000;
Output:
EmployeeID | EmployeeName | Salary |
---|---|---|
101 | Aryan Sharma | 55000.00 |
102 | Meera Raj | 62000.00 |
104 | Kiran Das | 72000.00 |
105 | Rohan Mehta | 51000.00 |
Fetching Employee Details by Name
If we need to find details of a specific employee, such as Sohan Verma, we can use the following query:
SELECT EmployeeID, EmployeeName, Salary
FROM EMPLOYEES
WHERE EmployeeName = 'Sohan Verma';
Output:
EmployeeID | EmployeeName | Salary |
---|---|---|
103 | Sohan Verma | 48000.00 |
Important Notes:
- String values should always be enclosed in single quotes (
'
). - Numeric values do not require quotes.
- The WHERE condition can be combined with logical operators like AND, OR, and NOT for more complex filtering.
Download New Real Time Projects :-Click here
Complete Advance AI topics:- CLICK HERE
Conclusion
The WHERE condition in SQL is a fundamental clause that helps filter data efficiently. Whether retrieving specific records, updating certain values, or deleting unwanted data, the WHERE clause ensures accuracy in database queries. Mastering this concept is crucial for anyone working with SQL databases.
For more SQL tutorials and updates, visit UpdateGadh!
sql where multiple conditions
where clause in sql example
having clause in sql
order by clause in sql
group by clause in sql
sql where clause multiple values
having clause with where condition in sql
select clause in sql
order by in sql
WHERE Condition in SQL
like in sql
where condition in sql w3schools
where condition in sql server
Post Comment