Course Content
Introduction to SQL
Introduction to SQL
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.
Let's see an example:
SELECT name, continent FROM country WHERE continent='Europe';
Explanation: The SELECT
statement gets 2 columns from the country
table and returns only rows with the continent value 'Europe'.
Note
Note that we need to enclose the value in single quotation marks('').
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:
SELECT capital, continent FROM country WHERE continent='Asia' ORDER BY continent DESC;
Swipe to show code editor
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
.
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
.
Here's a short example of the country
table:
id | name | continent | region | surfacearea | capital | population |
1 | Japan | Asia | Eastern Asia | 377829 | Tokyo | 126714000 |
2 | Latvia | Europe | NULL | 64589 | Riga | 2424200 |
3 | Mexico | North America | Central America | 1958201 | Mexico City | 98881000 |
... | ... | ... | ... | ... | ... | ... |
15 | Malta | Europe | Southern Europe | 316 | Valletta | 380200 |
Thanks for your feedback!
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.
Let's see an example:
SELECT name, continent FROM country WHERE continent='Europe';
Explanation: The SELECT
statement gets 2 columns from the country
table and returns only rows with the continent value 'Europe'.
Note
Note that we need to enclose the value in single quotation marks('').
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:
SELECT capital, continent FROM country WHERE continent='Asia' ORDER BY continent DESC;
Swipe to show code editor
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
.
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
.
Here's a short example of the country
table:
id | name | continent | region | surfacearea | capital | population |
1 | Japan | Asia | Eastern Asia | 377829 | Tokyo | 126714000 |
2 | Latvia | Europe | NULL | 64589 | Riga | 2424200 |
3 | Mexico | North America | Central America | 1958201 | Mexico City | 98881000 |
... | ... | ... | ... | ... | ... | ... |
15 | Malta | Europe | Southern Europe | 316 | Valletta | 380200 |
Thanks for your feedback!
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.
Let's see an example:
SELECT name, continent FROM country WHERE continent='Europe';
Explanation: The SELECT
statement gets 2 columns from the country
table and returns only rows with the continent value 'Europe'.
Note
Note that we need to enclose the value in single quotation marks('').
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:
SELECT capital, continent FROM country WHERE continent='Asia' ORDER BY continent DESC;
Swipe to show code editor
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
.
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
.
Here's a short example of the country
table:
id | name | continent | region | surfacearea | capital | population |
1 | Japan | Asia | Eastern Asia | 377829 | Tokyo | 126714000 |
2 | Latvia | Europe | NULL | 64589 | Riga | 2424200 |
3 | Mexico | North America | Central America | 1958201 | Mexico City | 98881000 |
... | ... | ... | ... | ... | ... | ... |
15 | Malta | Europe | Southern Europe | 316 | Valletta | 380200 |
Thanks for your feedback!
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.
Let's see an example:
SELECT name, continent FROM country WHERE continent='Europe';
Explanation: The SELECT
statement gets 2 columns from the country
table and returns only rows with the continent value 'Europe'.
Note
Note that we need to enclose the value in single quotation marks('').
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:
SELECT capital, continent FROM country WHERE continent='Asia' ORDER BY continent DESC;
Swipe to show code editor
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
.
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
.
Here's a short example of the country
table:
id | name | continent | region | surfacearea | capital | population |
1 | Japan | Asia | Eastern Asia | 377829 | Tokyo | 126714000 |
2 | Latvia | Europe | NULL | 64589 | Riga | 2424200 |
3 | Mexico | North America | Central America | 1958201 | Mexico City | 98881000 |
... | ... | ... | ... | ... | ... | ... |
15 | Malta | Europe | Southern Europe | 316 | Valletta | 380200 |