Search⌘ K
AI Features

Restricting the Specific Rows

Explore how to restrict data retrieval in SQL by applying the WHERE clause with various comparison and boolean operators. Learn to filter rows based on conditions, combine criteria with AND and OR, and count matching entries using select count(*). This lesson helps you handle targeted data queries effectively in relational databases.

Use of the where clause

The select statements returned all the customers stored in our customers table. This isn’t usually the case, though. Generally, the customers table contains many rows, the majority of which we aren’t interested in listing. We usually want to retrieve a subset of the customers that satisfy specific criteria.

Let’s suppose that we want to retrieve the customer named John Woo. To do that, we have to use the where clause. The where clause is used with the select statement after the table’s name from which we want to retrieve data.

Try the following command.

MySQL
select * from customers where name = 'John Papas';

After we run the query above in the terminal, we get the following output:

1 +----+------------+
2 | id |  name      |
3 +----+------------+
4 |  2 |  John Papas|
5 +----+------------+
6 1 row in set (0.01 sec)

The rows returned match the criteria that we gave: name = 'John Papas'.

The criteria are given after the where reserved word.

Different operators

The criteria are usually ...