562 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owls only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Using Criteria in SQL Queries Part two of a six-part series of blogs |
---|
When you write queries in SQL it's immensely useful to be able to show records matching criteria that you've set. You can do this using the WHERE clause and this blog teaches you how to use it!
This blog is part of our online SQL tutorial blog - however, Wise Owl also run SQL courses for up to 6 people.
|
Numbers are perhaps the easiest type of data for which to write criteria, which is why we're using them as a starting point.
You can use a range of different symbols, known as operators, for comparing numbers: the table below shows you the main ones you're likely to use:
Operator | Alternative | What it means |
---|---|---|
= | Equal to | |
<> | != | Not equal to |
> | Greater than | |
< | Less than | |
>= | !< | Greater than or equal to (or not less than) |
<= | !> | Less than or equal to (or not greater than) |
The operator always sits between the two values you are comparing. For example, the query below compares the value of the FilmRunTimeMinutes field with the value 180:
SELECT
FilmName
,FilmRunTimeMinutes
FROM
tblFilm
WHERE
FilmRunTimeMinutes >= 180
The query shows all of the films whose running time is greater than or equal to 180:
The shortest film in this list is 180 minutes long.
You can use the BETWEEN keyword to find numbers that fall between an upper and lower limit. The example below would find all of the films whose running time is between 90 and 100 minutes.
SELECT
FilmName
,FilmRunTimeMinutes
FROM
tblFilm
WHERE
FilmRunTimeMinutes BETWEEN 90 AND 100
A selection of the results from this query are shown below:
You can see from the diagram that films lasting exactly 90 and 100 minutes are included in the results of the query.
You can use the IN keyword to find records that match any values in a list of numbers. The example below looks for films whose running time is exactly 100, 150 or 200 minutes:
SELECT
FilmName
,FilmRunTimeMinutes
FROM
tblFilm
WHERE
FilmRunTimeMinutes IN (100,150,200)
Note that the list of numbers must be enclosed in a set of parentheses. The results of this query are shown in the diagram below:
There aren't any films with a running time of exactly 200 minutes.
Now that you've seen how easy it is to write criteria using numbers it's time to see a few of the quirks of writing criteria involving text.
Parts of this blog |
---|
|
Some other pages relevant to the above blogs include:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2023. All Rights Reserved.