Cursussen/Courses Codesnippets     Top 
SQL select - Conditions


Conditions
When selecting, you will be able to specify conditions to have specific records retrieved. These conditions are composed of field names, operators, and values.
You can use the following operators:
operatordescription
=equal to
!=not equal to
<less than
>greater than
<=less than or equal to
>=greater than or equal to
BETWEEN … AND …between start and end value
LIKEaccording to a letter pattern
INlisted
In the example below, the "wine_id" and "cost" of the "inventory" table are selected whose "cost" is less than 5.1.
select wine_id, cost
from inventory
where cost < 5.1
Note that decimal numbers must be entered with a decimal point.
You can request a telephone number of a vineyard ("winery") by comparing the name with a given name in the condition.
select winery_id, winery_name, region_id, phone
from winery
where winery_name = 'Binns Group'
If you do not know the exact name, you can also formulate a question in which you e.g. requests all vineyards whose first letter of name is greater than or equal to the letter "W".
select winery_name, phone
from winery
where winery_name >= 'W'
All vineyards whose name starts with a W, X, Y or Z are retrieved.
With the operators "Between…and…" you can specify a range between which the contents of a field must be in order to be selected.
In this example, the customers are retrieved whose last name starts with a D, E or F. The end value is not included in the result. However, the initial value does.
select surname, firstname
from customer
where surname between 'D' and 'G'
The list is automatically sorted by the field from the condition ("where" clause).
If you use the operator "like" you can use wildcards. In SQL, a "%" sign stands for replacing any number of characters. The "_" character (underscore) stands for replacing 1 character in the indicated place.
In the first example, the names of the wines starting with "Wood" are selected. The names are further completed with any characters.
select wine_name
from wine
where wine_name like 'Wood%'
The second example searches for the word "err" in every possible position in the wine name. So the name must contain "err".
select wine_name
from wine
where wine_name like '%err%'
The "IN" operator compares the contents of the field to values ​​in an enumeration. The content and value must be exactly the same. If so, the record is included in the result.
select wine_name
from wine
where wine_name in ('Chester', 'Sorrenti', 'Chemnis')
Operations
While executing a query you can make edits with the contents of the fields.
In the example below, a list is requested of wines with a cost price less than 5.1. The calculated column shows how much the cost would be if it were increased by 5%.
select wine_id, cost/100 * 105
from inventory
where cost < 5.1
Multiple conditions
You can specify multiple conditions in the "where" clause of a query.
To combine these conditions you use the logical operators AND, OR or NOT.
select wine_name, year
from wine
where type = 'Red'
and year < 1971
In this example, all red wines are selected AND the vintage of those wines is less than 1971. Only the records where both conditions were met are included in the result.
When using the "or" operator you indicate that one of the two conditions must be met to be included in the result.
In the example below, all white OR red wines are selected where the vintage of those wines is less than 1971. You use parentheses to indicate which condition should be combined with which other condition.
select wine_name, type, year
from wine
where (type = 'Red' or type = 'White')
and year < 1971
You may be wondering if there are other wines (other than red or white) from 1970.
For this you can adjust the query so that the red or white wines are NOT included in the result.
select wine_name, type, year
from wine
where not(type = 'Red' or type = 'White')
and year < 1971


X

Paragraphs

Conditions