SQL Where
SQL Where
This page covers SQL Where statement.
The previous tutorials briefly explored the concept of the “where” statement.
In SQL, the “where” statement is one of the most used statements, as it works well with many other SQL statements.
Its usage encompasses the extraction of pre-specified data. In other words, it operates as a filter.
This tutorial covers:
- filtering data
Let’s use the table “student_info” from the previous tutorials.
id | firstName | lastName | age |
---|---|---|---|
1 | John | Smith | 21 |
2 | Patrick | Casey | 26 |
3 | Sam | Adams | 19 |
4 | Mike | Garcia | NULL |
5 | Patrick | Dumont | 21 |
Where
The “where” statement can filter information in various forms.
In further tutorials, we will see the combination of “where” and other statements.
This statements is placed after the main statements, such as “select”, “update”, “delete”, and so on.
The following is an example of filtering records with a specific name.
SELECT * FROM student_info WHERE lastName='Adams';
Please note that if there is no data that match the filter’s criteria, nothing will show as an output.
Let’s se the result of the above statement.
id | firstName | lastName | age |
---|---|---|---|
3 | Sam | Adams | 19 |
We can filter through any data inside the table.
Below is an instance of filtering student ids. Please note that integers (numbers) do not need quotes.
SELECT * FROM student_info WHERE id=2;
Let’s check the outcome.
id | firstName | lastName | age |
---|---|---|---|
2 | Patrick | Casey | 26 |
The above scenarios display results with only a single record.
But the “where” statement can show unlimited result points, as long as the filter’s pre-specified conditions match the information.
The following is an example of showing all 21 years old students.
SELECT * FROM student_info WHERE age=21;
id | firstName | lastName | age |
---|---|---|---|
1 | John | Smith | 21 |
5 | Patrick | Dumont | 21 |
The outcome shows 2 records. Both students match the condition of age=21.
Next: SQL Alias