AdBlock Detected!
Our website is made possible by displaying ads to our visitors. Please supporting us by whitelisting our website.
SQL WHERE Clause |
|
SQL > SQL Commands >
WHERE Clause
The WHERE clause is used to filter the result set based on the condition specified following the word WHERE.
Key Takeaway: The WHERE clause is how you tell SQL which rows to return or modify. Without it, SQL operates on every row in the table — a dangerous habit when using UPDATE or DELETE.
The WHERE clause can be used with the following types of SQL statements: SyntaxThe syntax for using WHERE in the SELECT statement is as follows: The syntax for using WHERE in the UPDATE statement is as follows: The syntax for using WHERE in the DELETE statement is as follows: "Condition" can include a single comparison clause (called simple condition) or multiple comparison clauses combined together using AND or OR operators (compound condition). ExamplesWe provide examples here to show how to use WHERE in the SELECT statement. Example 1: WHERE Clause With Simple ConditionTo select all stores with sales above $1,000 in Table Store_Information, Table Store_Information
we key in, Result:
Example 2: WHERE Clause With OR OperatorTo view all data with sales greater than $1,000 or with transaction date of 'Jan-08-1999', we use the following SQL, Result:
The first returned row has sales greater than $1,000. The next two returned rows have a transaction date of 'Jan-08-1999.' Using WHERE With UPDATE and DELETEAs mentioned above, the WHERE clause can be used with UPDATE and DELETE statements in addition to the SELECT statement. Examples of how to use the WHERE clause with these two commands can be seen in the UPDATE and DELETE sections. ExercisesFor these exercises, assume we have a table called Users with the following data: Table Users
1. Which of the following SQL statement is valid? (There can be more than one answer)
2. What's the result of the following query?
3. (True or False) The condition used in the WHERE clause must include a column that is part of the SELECT clause.
Frequently Asked QuestionsWhat operators can I use in a WHERE clause?Common operators include: = (equal), != or <> (not equal), >, <, >=, <=, BETWEEN, LIKE, IN, IS NULL, and IS NOT NULL. What is the difference between WHERE and HAVING?WHERE filters individual rows before grouping; HAVING filters grouped results after a GROUP BY operation. WHERE cannot reference aggregate functions like SUM() or COUNT(). Can WHERE filter by NULL values?Yes, but you cannot use = NULL. Instead, use How do I filter by a range of values in WHERE?Use BETWEEN for ranges:
|
Our website is made possible by displaying ads to our visitors. Please supporting us by whitelisting our website.