SQL Filters
SQL Filters
This tutorial covers SQL Filters.
In this section, filters refer to statements that help with the retrieval of specific information
We cover Select Distinct and Select Top in this page. These filters work well with the “select” and “where” statements.
This page encompasses:
- selecting only distinct data
- selecting a specific number of records
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 |
Select Distinct
As the name suggest, this statement filters only distinct (or different) data records.
By different, it means that duplicate values will not display.
The keyword is “distinct” and it is placed after the “select” statement.
The following is an example.
SELECT DISTINCT firstName FROM student_info;
firstName |
---|
John |
Patrick |
Sam |
Mike |
Patrick |
The above statement lists all unique names (no duplicates).
Select Top
You will often have to work with very large databases.
And sometimes, we need only a few records. Instead of displaying hundreds or thousands of data records, we can use the “select top”.
The “select top” filters data and outputs a pre-specified number of records.
SELECT TOP 2 id, firstName, lastName FROM student_info;
id | firstName | lastName |
---|---|---|
1 | John | Smith |
2 | Patrick | Casey |
Next: SQL Functions