SQL

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.

idfirstNamelastNameage
1JohnSmith21
2PatrickCasey26
3SamAdams19
4MikeGarciaNULL
5PatrickDumont21

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;
idfirstNamelastName
1JohnSmith
2PatrickCasey

Next: SQL Functions