Skip to main content

SQL Filtering Data

πŸ“™ Selecting Data​

Welcome to the Selecting Data module! This foundational learning path is designed to help you master the basics of querying data, with a particular focus on retrieving specific information from databases effectively.

πŸ“˜ Using Conditions with the WHERE Clause​

In this tutorial, you'll learn how to interpret and work with rows in a database table. Tables are essential to storing structured data, and each row in a table represents a unique item or record.

The first step in filtering is selecting the items. We use the WHERE keyword to filter the data by applying conditions. We filter the table items, we select to get only the rows that satisfy specific conditions. The condition we want our rows to fulfill comes after the where keyword

For example, consider a table named Students. Below is how a simple table might look:

info
Students
| name        | year | major   |
|-------------|------|---------|
| Ava Smith | 1 | Biology |
| Luis Garcia | 1 | Physics |
| Lin Wong | 3 | Biology |

πŸ“˜ Practice Example​

To query data from a table, use the FROM clause followed by the table name and then the WHERE clause to specify the conditions for the data you want to retrieve.

For example, consider a table named Students. Below is how a simple table might look:

info
Students
| name | email             | type  |
|------|-------------------|-------|
| Sam | sam17@mail.com | free |
| Re my | rem@mail.com | pro |
| Luis | luis.99@mail.com | basic |
| Kim | kimz@mail.com | pro |
tip

When requesting data with SQL statements like SELECT, we say that we are making a query. From helps in selecting columns from the table we are working on. While not necessary but it's a good practice to finish the sql queries with ";"

πŸ”„ Checking Equality​

We use the = operator to check if the two values are equal.

The values like text values are written between single quotes.

We can also use numeric values, we don't need to put them in the quotes.

the = sign check if the two values are equal.

info
Students
| name        | year | major   |
|-------------|------|---------|
| Ava Smith | 1 | Biology |
| Luis Garcia | 1 | Physics |
| Lin Wong | 3 | Biology |

🧹 Selecting Unique Values with DISTINCT​

DISTINCT removes duplicate rows from the result set, returning only unique values or combinations.

In the table below, duplicate rows (if any student appears multiple times with identical name, year, and major) will be removed.


info
Students
| name        | year | major   |
|-------------|------|---------|
| Ava Smith | 1 | Biology |
| Luis Garcia | 1 | Physics |
| Lin Wong | 3 | Biology |
| Ava Smith | 1 | Biology |

🧹 Filtering columns​

When using conditions we don't have to select all columns with *, we can select only a couple like name and year.

We don't have to select all columns when filtering.


info
Students
| name        | year | major   |
|-------------|------|---------|
| Ava Smith | 1 | Biology |
| Luis Garcia | 1 | Physics |
| Lin Wong | 3 | Biology |

βœ… What You have Learnt​

This module covers four essential topics in data selection:

  • Rows and Columns

Learn how to access specific rows and columns in a dataset or table, the building blocks of any query. We selected specific rows using conditions and columns using the SELECT statement.

  • Select Data

Understand the basic SELECT statement to retrieve data from a database.

  • Select Multiple Columns

Retrieve more than one column at a time in your queries to get the information you need all at once.

  • Select Distinct Values

Use DISTINCT to eliminate duplicate records and identify unique entries within your dataset.