Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Using the WHERE Clause | Filtering Data
Introduction to SQL
course content

Kursusindhold

Introduction to SQL

Introduction to SQL

1. Retrieving Data
2. Sorting Retrieved Data
3. Filtering Data
4. Advanced Data Filtering
5. Aggregate Functions

book
Using the WHERE Clause

In databases, tables typically hold substantial volumes of data. However, frequently we're interested in retrieving specific portions of the data rather than the entirety. To accomplish this, we need to define the conditions for data retrieval, which are referred to as filtering criteria.

Data is filtered using a WHERE clause specifying the search criteria in a SELECT statement. The WHERE clause appears immediately after the table name.

When specifying a string value, such as a country name, we need to enclose the text in single quotes (').

Let's see an example:

123
SELECT name, continent FROM country WHERE continent='Europe';
copy

Explanation: The SELECT statement gets 2 columns from the country table and returns only rows with the continent value 'Europe'.

Clause Position

When we use the ORDER BY and WHERE clauses, we ensure the ORDER BY comes after the WHERE clause.

Let's see the following example:

1234
SELECT capital, continent FROM country WHERE continent='Asia' ORDER BY continent DESC;
copy

Here is the country table we are working with:

Opgave

Swipe to start coding

Write an SQL query to retrieve the id, name, and region columns from the country table (please retrieve these columns in this order), returning only rows with the 'North America' continent.

Note

Please note that North America should be correctly capitalized, and north america is not the same. So, be careful and write it as North America.

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 1
toggle bottom row

book
Using the WHERE Clause

In databases, tables typically hold substantial volumes of data. However, frequently we're interested in retrieving specific portions of the data rather than the entirety. To accomplish this, we need to define the conditions for data retrieval, which are referred to as filtering criteria.

Data is filtered using a WHERE clause specifying the search criteria in a SELECT statement. The WHERE clause appears immediately after the table name.

When specifying a string value, such as a country name, we need to enclose the text in single quotes (').

Let's see an example:

123
SELECT name, continent FROM country WHERE continent='Europe';
copy

Explanation: The SELECT statement gets 2 columns from the country table and returns only rows with the continent value 'Europe'.

Clause Position

When we use the ORDER BY and WHERE clauses, we ensure the ORDER BY comes after the WHERE clause.

Let's see the following example:

1234
SELECT capital, continent FROM country WHERE continent='Asia' ORDER BY continent DESC;
copy

Here is the country table we are working with:

Opgave

Swipe to start coding

Write an SQL query to retrieve the id, name, and region columns from the country table (please retrieve these columns in this order), returning only rows with the 'North America' continent.

Note

Please note that North America should be correctly capitalized, and north america is not the same. So, be careful and write it as North America.

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 1
Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Vi beklager, at noget gik galt. Hvad skete der?
some-alt