| Start | |||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Use BETWEEN to Filter Results The following examples use the Item Sales (opens new window) and Customers (opens new window) sample databases. Note: The BETWEEN operator is inclusive. Using the BETWEEN operator with Numbers: This query will return all
Using the BETWEEN operator with Date Values: This query will return all
When comparing datetime values instead of dates, you may need to convert the datetime values into a date values, or add or subtract 24 hours to get the correct results. Using the BETWEEN operator with Text Values: Live example: SQL fiddle (opens new window) This query will return all customers whose name alphabetically falls between the letters 'D' and 'L'. In this case, Customer #1 and #3 will be returned. Customer #2, whose name begins with a 'M' will not be included.
|
|||||||||||||||||||||||||||||||||||||||||||||||||
| Use HAVING with Aggregate Functions Unlike the An aggregate function is a function where the values of multiple rows are grouped together as input on certain criteria to form a single value of more significant meaning or measurement (Wikipedia (opens new window)). Common aggregate functions include This example uses the Car Table (opens new window) from the Example Databases. This query will return the The results will look like:
|
|||||||||||||||||||||||||||||||||||||||||||||||||
WHERE clause with NULL/NOT NULL values This statement will return all Employee (opens new window) records where the value of the The result will be: This statement will return all Employee (opens new window) records where the value of the The result will be: Note: The same query will not return results if you change the WHERE clause to |
|||||||||||||||||||||||||||||||||||||||||||||||||
| The WHERE clause only returns rows that match its criteria Steam has a games under $10 section of their store page. Somewhere deep in the heart of their systems, there's probably a query that looks something like: |
|||||||||||||||||||||||||||||||||||||||||||||||||
Equality This statement will return all the rows from the table Using a Will only return the rows where the |
|||||||||||||||||||||||||||||||||||||||||||||||||
| AND and OR You can also combine several operators together to create more complex AND Will return: OR Will return: |
|||||||||||||||||||||||||||||||||||||||||||||||||
| Use IN to return rows with a value contained in a list This example uses the Car Table (opens new window) from the Example Databases. This query will return Car #2 which costs 200 and Car #3 which costs 100. Note that this is equivalent to using multiple clauses with |
|||||||||||||||||||||||||||||||||||||||||||||||||
| Use LIKE to find matching strings and substrings See full documentation on LIKE operator (opens new window). This example uses the Employees Table (opens new window) from the Example Databases. This query will only return Employee #1 whose first name matches 'John' exactly. Adding
In this case, the query will return Employee #2 whose name is 'John' as well as Employee #4 whose name is 'Johnathon'. |
|||||||||||||||||||||||||||||||||||||||||||||||||
| Use HAVING to check for multiple conditions in a group Orders Table
To check for customers who have ordered both - ProductID 2 and 3, HAVING can be used Return value:
The query selects only records with the productIDs in questions and with the HAVING clause checks for groups having 2 productIds and not just one. Another possibility would be This query selects only groups having at least one record with productID 2 and at least one with productID 3. |
|||||||||||||||||||||||||||||||||||||||||||||||||
| Where EXISTS Will select records in SyntaxFROM table_name WHERE column_name operator value FROM table_name GROUP BY column_name HAVING aggregate_function(column_name) operator value |
Read Article
https://develop.horje.com/learn/1434/reference