WHERE clause in sql

What is a WHERE clause?
We covered how to query data from a database using the SELECT statement in the previous tutorial. The SELECT statement returned all results from the requested database table.
These are, however, times when we want to restrict the results of a query to a specified condition. The SQL WHERE clause comes in handy in these situations.
WHERE Sentence syntax
The basic syntax for the WHERE clause when used in a SELECT statement is as follows.
SELECT * FROM tableName WHERE condition;
HERE
- "SELECT * FROM tableName" is a standard SELECT statement
- "WHERE" is a keyword that limits our select query result set, and "condition" is a filter that will be applied to the results. The filter can be a range, a single value, or a subquery.
Let's now look at a practical example .
Suppose we want to get member's personal details from member table given member number 1, for this we will use the following script.
SELECT * FROM `members` WHERE `membership_number` = 1;
Running the above script in MySQL Workbench on "myflixdb" will produce the following results.
membership_number | full_names | gender | date_of_birth | physical_address | postal_address | contct_number | |
---|---|---|---|---|---|---|---|
one | Janet Jones | Female | 21-07-1980 | First Street Plot No 4 | Private Bag | 0759 253 542 | janetjones@yagoo.cm |
WHERE clause combined with operator - AND LOGICAL
The WHERE clause, when used in conjunction with the logical AND operator, is only executed if all of the specified filter criteria are met.
SELECT * FROM `movies` WHERE `category_id` = 2 AND `year_released` = 2008;
Running the above script in MySQL Workbench against "myflixdb" produces the following results.
movie_id | title | director | year_released | category_id |
---|---|---|---|---|
2 | Forgetting Sarah Marshall | Nicholas Stoller | 2008 | 2 |
WHERE clause combined with OR OR LOGICAL operator
SELECT * FROM `movies` WHERE `category_id` = 1 OR `category_id` = 2;
Running the above script in MySQL Workbench against "myflixdb" produces the following results.
movie_id | title | director | year_released | category_id |
---|---|---|---|---|
one | Pirates of the Caribbean 4 | Rob Marshall | 2011 | one |
2 | Forgetting Sarah Marshall | Nicholas Stoller | 2008 | 2 |
WHERE clause combined with the IN keyword
SELECT * FROM `members` WHERE `membership_number` IN (1,2,3);
Running the above script in MySQL Workbench against "myflixdb" produces the following results.
membership_number | full_names | gender | date_of_birth | physical_address | postal_address | contct_number | |
---|---|---|---|---|---|---|---|
one | Janet Jones | Female | 21-07-1980 | First Street Plot No 4 | Private Bag | 0759 253 542 | <a href='mailto:janetjones@y& #97;goo.cm'>janetjones@y&# 97;goo.cm</a> |
2 | Janet Smith Jones | Female | 23-06-1980 | Melrose 123 | NULL | NULL | <a href='mailto:jj@fstreet.com' >jj@fstreet.com</a> |
3 | Robert Phil | Male | 12-07-1989 | 3rd Street 34 | NULL | 12345 | <a href='mailto:rm@tstreet.com' >rm@tstreet.com</a> |
WHERE clause combined with the NOT IN keyword
SELECT * FROM `members` WHERE `membership_number` NOT IN (1,2,3);
Running the above script in MySQL Workbench against "myflixdb" produces the following results.
membership_number | full_names | gender | date_of_birth | physical_address | postal_address | contct_number | |
---|---|---|---|---|---|---|---|
4 | Gloria Williams | Female | 14-02-1984 | 2nd Street 23 | NULL | NULL | NULL |
WHERE clause combined with - COMPARATIVE OPERATORS
= Equal
SELECT * FROM `members` WHERE `gender` = 'Female';
Running the above script in MySQL Workbench against "myflixdb" produces the following results.
membership_number | full_names | gender | date_of_birth | physical_address | postal_address | contct_number | |
---|---|---|---|---|---|---|---|
one | Janet Jones | Female | 21-07-1980 | First Street Plot No 4 | Private Bag | 0759 253 542 | <a href='mailto:janetjones@y& #97;goo.cm'>janetjones@y&# 97;goo.cm</a> |
2 | Janet Smith Jones | Female | 23-06-1980 | Melrose 123 | NULL | NULL | <a href='mailto:jj@fstreet.com' >jj@fstreet.com</a> |
4 | Gloria Williams | Female | 14-02-1984 | 2nd Street 23 | NULL | NULL | NULL |
> more than
payment_id | membership_number | payment_date | description | amount_paid | external_reference_number |
---|---|---|---|---|---|
one | one | 23-07-2012 | Movie rental payment | 2500 | eleven |
3 | 3 | 30-07-2012 | Movie rental payment | 6000 | NULL |
<> Not Equal
SELECT * FROM `movies` WHERE `category_id`<> 1;
movie_id | title | director | year_released | category_id |
---|---|---|---|---|
2 | Forgetting Sarah Marshall | Nicholas Stoller | 2008 | 2 |
5 | Daddy's Little Girls | NULL | 2007 | eight |
6 | Angels and Demons | NULL | 2007 | 6 |
7 | Davinci Code | NULL | 2007 | 6 |
9 | honey mooners | John Schultz | 2005 | eight |
Summary
- The SQL WHERE clause is used to limit the number of rows affected by a SELECT, UPDATE, or DELETE query.
- The WHERE clause can be used along with logical operators like AND and OR, comparison operators like =, etc.
- When used with the logical AND operator, all criteria must be met.
- When used with the logical OR operator, either of the criteria must be met.
- The IN keyword is used to select rows that match a list of values.
brain teaser
SELECT * FROM `movierentals` WHERE `return_date` < '2012-06-25' AND movie_returned = 0;
reference_number | transaction_date | return_date | membership_number | movie_id | movie_returned |
---|---|---|---|---|---|
14 | 21-06-2012 | 24-06-2012 | 2 | 2 | 0 |