Page 75 - SQL
P. 75
SELECT *
FROM Employees
WHERE FName LIKE 'John'
This query will only return Employee #1 whose first name matches 'John' exactly.
SELECT *
FROM Employees
WHERE FName like 'John%'
Adding % allows you to search for a substring:
• John% - will return any Employee whose name begins with 'John', followed by any amount of
characters
• %John - will return any Employee whose name ends with 'John', proceeded by any amount of
characters
• %John% - will return any Employee whose name contains 'John' anywhere within the value
In this case, the query will return Employee #2 whose name is 'John' as well as Employee #4
whose name is 'Johnathon'.
WHERE clause with NULL/NOT NULL values
SELECT *
FROM Employees
WHERE ManagerId IS NULL
This statement will return all Employee records where the value of the ManagerId column is NULL.
The result will be:
Id FName LName PhoneNumber ManagerId DepartmentId
1 James Smith 1234567890 NULL 1
SELECT *
FROM Employees
WHERE ManagerId IS NOT NULL
This statement will return all Employee records where the value of the ManagerId is not NULL.
The result will be:
Id FName LName PhoneNumber ManagerId DepartmentId
2 John Johnson 2468101214 1 1
3 Michael Williams 1357911131 1 2
4 Johnathon Smith 1212121212 2 1
Note: The same query will not return results if you change the WHERE clause to WHERE ManagerId
= NULL or WHERE ManagerId <> NULL.
https://riptutorial.com/ 57

