Read This

Before you build your query, read this:

Databases use Structured Query Language (SQL) to retrieve data from them.  For the most part it's just plain English structured in a logically consistent way for retrieving data from a defined data source using the names of the data source elements and the data contained in them.  A query statement is also called a “SELECT” statement as it represents the primary action of the query – selecting data from the database. 

"SELECT column1, column2, column3 FROM table_name"

If we apply this statement structure to a database table called "Employees" the statement might look like this:

"SELECT FirstName, LastName, Email, StartDate FROM Employees"

This would return all records in the Employees table for just those four columns. If instead we only want records containing the last name Smith then we need to add a filter to the statement.

Filters

A filter is comprised of a WHERE Clause, the name of the column we’re looking in, an Operator that defines how we’re searching, and the value we’re searching for:

SELECT FirstName, LastName, Email, StartDate FROM Employees WHERE LastName = 'Smith'

This returns results for the four columns specified where LastName equals Smith.

More often than not query statements include multiple filters. Using multiple filters lets you get as granular as you want in your search depending on how many fields are in your database.  You can search for the last name Smith, excluding all ‘gmail’ emails, for employees who started working between a specific date range, all with one query statement.  The query statement described above contains three filters and anytime more than one filter is applied to a query statement they need to be joined by an AND/OR Operator.

The AND and OR Operators

Using either AND or OR all depends on the results you want.

AND:

if you want results where the criteria is met for ALL the filters then you would use AND to join the filters:

SELECT FirstName, LastName Email, StartDate FROM Employees WHERE LastName = ‘Smith’ AND Email DOES NOT CONTAIN ‘gmail’ AND StartDate >= ‘7/9/2010’

This statement would only return results where all three filter’s conditions were met: the employee last name equals Smith, their email address is not gmail, and they started working on or before 7/9/2010.  Any record where all three of those conditions are not met will not show up in the results.


OR:

 let’s say you’re looking for employees that have either ‘gmail’ email accounts or ‘yahoo’ email accounts:

SELECT FirstName, LastName, Email, StartDate FROM Employees WHERE Email CONTAINS ‘gmail’ OR Email CONTAINS ‘yahoo’

The results returned by this statement would include records where either filter’s condition is met: employees that have either gmail or yohoo email accounts.

Filter Groups

For more complex queries, sometimes you need several filters to work together as one condition while another filter or filters works together as another condition.  Let’s say you want to find out how many employees have the last name Smith who have either gmail or yahoo accounts.  You would NOT get your desired results if your statement looked like this:

SELECT FirstName, LastName, Email, StartDate FROM Employees WHERE LastName = ‘Smith’ AND Email CONTAINS ‘gmail’ OR Email CONTAINS ‘Yahoo’

Your result set would include Smiths with gmail accounts but it would also include all yahoo accounts whether they’re Smith or not.  In order to get your desired results, the two Email filters need to work together as one condition.  This is accomplished with a simple set of parentheses:

SELECT FirstName, LastName, Email, StartDate FROM Employees WHERE LastName = ‘Smith’ AND (Email CONTAINS ‘gmail’ OR Email CONTAINS ‘yahoo’)

Because the Email filters have been grouped together to work as a single condition, this statement returns the desired results: Smiths with either gmail or yahoo email accounts.


For more information on SQL visit the W3C Schools:

https://www.w3schools.com/sql/default.asp