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:
- SQL Table
- SQL Code
- Output
| name | year | major |
|-------------|------|---------|
| Ava Smith | 1 | Biology |
| Luis Garcia | 1 | Physics |
| Lin Wong | 3 | Biology |
SELECT *
FROM Students
WHERE major = 'Biology';
| name | year | major |
|---|---|---|
| Ava Smith | 1 | Biology |
| 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:
- SQL Table
- SQL Code
- Output
| 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 |
SELECT *
FROM Students
WHERE type = 'pro';
| name | type | |
|---|---|---|
| Re my | rem@mail.com | pro |
| Kim | kimz@mail.com | pro |
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.
- SQL Table
- SQL Code
- Output
| name | year | major |
|-------------|------|---------|
| Ava Smith | 1 | Biology |
| Luis Garcia | 1 | Physics |
| Lin Wong | 3 | Biology |
SELECT *
FROM Students
WHERE major = 'Biology';
| name | year | major |
|---|---|---|
| Ava Smith | 1 | Biology |
| 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.
- SQL Table
- SQL Code
- Output
| name | year | major |
|-------------|------|---------|
| Ava Smith | 1 | Biology |
| Luis Garcia | 1 | Physics |
| Lin Wong | 3 | Biology |
| Ava Smith | 1 | Biology |
SELECT DISTINCT name, year, major
FROM Students;
| name | year | major |
|---|---|---|
| Ava Smith | 1 | Biology |
| Luis Garcia | 1 | Physics |
| Lin Wong | 3 | 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.
- SQL Table
- SQL Code
- Output
| name | year | major |
|-------------|------|---------|
| Ava Smith | 1 | Biology |
| Luis Garcia | 1 | Physics |
| Lin Wong | 3 | Biology |
SELECT name, year
FROM Students;
| name | year |
|---|---|
| Ava Smith | 1 |
| Luis Garcia | 1 |
| Lin Wong | 3 |
β 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
SELECTstatement 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
DISTINCTto eliminate duplicate records and identify unique entries within your dataset.