TOP 2
* FROM Persons
TOP 50 PERCENT
* FROM Persons
Example 1
Now, we want to choose from the "Persons" table above for people living in cities that start with "N":
We can use the following SELECT statement:
SELECT * from Personswhere city like ' N% '
Tip: "%" can be used to define wildcard characters (missing letters in the pattern).
Example 2
Next, we want to select from the "Persons" table the people who live in cities that end with "G":
We can use the following SELECT statement:
SELECT * from Persons
WHERE city like '%g '
Example 3
Next, we want to select from the "Persons" table the people who live in cities that contain "lon":
We can use the following SELECT statement:
SELECT * from Personswhere city like '%lon% '
Example 4
By using the NOT keyword, we can select people residing in cities that do not contain "lon" from the "Persons" table:
We can use the following SELECT statement:
SELECT * from Personswhere City does like '%lon% '
SQL wildcard characters
The SQL wildcard can override one or more characters when searching for data in a database.
SQL wildcard characters must be used with the LIKE operator.
In SQL, you can use the following wildcard characters:
wildcard characters |
Description |
% |
Override one or more characters |
_ |
Replaces only one character |
[Charlist] |
Any single character of the word columns |
[^charlist] Or [!charlist] |
Any single character that is not in the word columns |
In operator
The in operator allows us to specify multiple values in the WHERE clause.
SQL in syntax
SELECT column_name (s) from Table_namewhere column_name in (value1,value2,...)
Between operator
operator between ... and selects a range of data between two values. These values can be numeric, text, or date.
SQL between syntax
SELECT column_name (s) from Table_namewhere Column_namebetween value1 and value2
SQL Advanced Statement