| id | filtering-data | |||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| title | SQL Filtering Data | |||||||||||||||||||||
| sidebar_label | SQL Filtering Data | |||||||||||||||||||||
| sidebar_position | 3 | |||||||||||||||||||||
| tags |
|
|||||||||||||||||||||
| description | This document is a tutorial on SQL data filtering techniques, designed for beginners learning database querying. It covers the fundamental concepts and practical applications of retrieving specific data from database tables. | |||||||||||||||||||||
| keywords |
|
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.
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
| name | year | major |
|-------------|------|---------|
| Ava Smith | 1 | Biology |
| Luis Garcia | 1 | Physics |
| Lin Wong | 3 | Biology |SELECT *
FROM Students
WHERE major = 'Biology';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
| 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';:::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 ";" :::
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
| name | year | major |
|-------------|------|---------|
| Ava Smith | 1 | Biology |
| Luis Garcia | 1 | Physics |
| Lin Wong | 3 | Biology |SELECT *
FROM Students
WHERE major = 'Biology';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
| 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;:::
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
| name | year | major |
|-------------|------|---------|
| Ava Smith | 1 | Biology |
| Luis Garcia | 1 | Physics |
| Lin Wong | 3 | Biology |SELECT name, year
FROM Students;:::
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.