2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								title: SQL Where Clause
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-01-16 02:20:35 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# SQL Where Clause
  
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								### `WHERE` Clause (and/or, `IN`, `BETWEEN`, and `LIKE`)
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-01-16 02:20:35 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The `WHERE`  clause is used to filter the number of rows returned, based on one or more conditions provided by user.
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								In this case all five of these will be used is a some what ridiculous `WHERE`  clause. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Here is the current full student list to compare to the `WHERE`  clause result set:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```sql
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								select studentID, FullName, sat_score, rcd_updated from student;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```text
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+-----------+------------------------+-----------+---------------------+
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								| studentID | FullName               | sat_score | rcd_updated         |
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+-----------+------------------------+-----------+---------------------+
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								|         1 | Monique Davis          |       400 | 2017-08-16 15:34:50 |
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								|         2 | Teri Gutierrez         |       800 | 2017-08-16 15:34:50 |
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								|         3 | Spencer Pautier        |      1000 | 2017-08-16 15:34:50 |
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								|         4 | Louis Ramsey           |      1200 | 2017-08-16 15:34:50 |
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								|         5 | Alvin Greene           |      1200 | 2017-08-16 15:34:50 |
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								|         6 | Sophie Freeman         |      1200 | 2017-08-16 15:34:50 |
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								|         7 | Edgar Frank "Ted" Codd |      2400 | 2017-08-16 15:35:33 |
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								|         8 | Donald D. Chamberlin   |      2400 | 2017-08-16 15:35:33 |
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								|         9 | Raymond F. Boyce       |      2400 | 2017-08-16 15:35:33 |
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+-----------+------------------------+-----------+---------------------+
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								9 rows in set (0.00 sec)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-01-16 02:20:35 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-11-25 07:18:59 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								We 'd like to apply the following filters:
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								*  `WHERE`  Student IDs are between 1 and 5 (inclusive)  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								*  `OR`  studentID = 8  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-01-16 02:20:35 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```sql
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								select  studentID, FullName, sat_score, recordUpdated
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								from    student
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								where   (studentID between 1 and 5 or studentID = 8);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```text
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+-----------+----------------------+-----------+---------------------+
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								| studentID | FullName             | sat_score | rcd_updated         |
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+-----------+----------------------+-----------+---------------------+
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								|         1 | Monique Davis        |       400 | 2017-08-16 15:34:50 |
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								|         2 | Teri Gutierrez       |       800 | 2017-08-16 15:34:50 |
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								|         3 | Spencer Pautier      |      1000 | 2017-08-16 15:34:50 |
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								|         4 | Louis Ramsey         |      1200 | 2017-08-16 15:34:50 |
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								|         5 | Alvin Greene         |      1200 | 2017-08-16 15:34:50 |
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								|         8 | Donald D. Chamberlin |      2400 | 2017-08-16 15:35:33 |
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+-----------+----------------------+-----------+---------------------+
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								6 rows in set (0.00 sec)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Here's an updated query, where any record that has a SAT score that's in the list (1000, 1400) will not be presented in addition to the above conditions:
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```sql
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								select  studentID, FullName, sat_score, recordUpdated
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								from    student
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								where   (studentID between 1 and 5 or studentID = 8)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        and
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        sat_score NOT in (1000, 1400);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```text
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+-----------+----------------------+-----------+---------------------+
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								| studentID | FullName             | sat_score | rcd_updated         |
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+-----------+----------------------+-----------+---------------------+
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								|         1 | Monique Davis        |       400 | 2017-08-16 15:34:50 |
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								|         2 | Teri Gutierrez       |       800 | 2017-08-16 15:34:50 |
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								|         4 | Louis Ramsey         |      1200 | 2017-08-16 15:34:50 |
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								|         5 | Alvin Greene         |      1200 | 2017-08-16 15:34:50 |
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								|         8 | Donald D. Chamberlin |      2400 | 2017-08-16 15:35:33 |
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+-----------+----------------------+-----------+---------------------+
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								5 rows in set (0.00 sec)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								*As with all of these SQL things there is MUCH MORE to them than what's in this introductory guide.  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								I hope this at least gives you enough to get started.  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Please see the manual for your database manager and have fun trying different options yourself.