Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
547 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers 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 ...
Written by Andrew Gould
In this tutorial
This tutorial teaches you how to add criteria to your SQL queries to find specific records in a table. You'll learn about how to work with text, dates and numbers in the WHERE clause of a query.
If you've read our tutorial on basic SQL queries you'll remember that the WHERE clause is the third of six keywords in a basic query. If you didn't read that tutorial or you just need a reminder here's where it fits in:
SELECT
FROM
WHERE
GROUP BY
HAVING
ORDER BY
The most basic criteria you can write in SQL queries involve comparing the value of a field against another value that you provide. In the example below we're comparing the value of the FilmRunTimeMinutes field with the value 120 and asking if they are equal to each other:
SELECT
FilmName
,FilmRunTimeMinutes
FROM
tblFilm
WHERE
FilmRunTimeMinutes = 120
When this query is executed we'll return all of the films whose running time is equal to the number we have provided:
These films all last for 120 minutes.
You might sometimes see people writing their criteria contained within parentheses, as shown below:
SELECT
FilmName
,FilmRunTimeMinutes
FROM
tblFilm
WHERE
(FilmRunTimeMinutes = 120)
With such a simple query the brackets make no difference to the results and so you can happily leave them out.
For more complex combinations of criteria you may find brackets helpful to clarify your query for other readers. A later part of this blog series shows an example where using brackets is essential for SQL Server to understand what you're trying to do!
Now that you know the basics of where the WHERE clause fits and what it's for, it's time to look in more detail at criteria involving specific data types; starting with numbers.
You can learn more about this topic on the following Wise Owl courses:
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 2024. All Rights Reserved.