The SELECT
statement in SQL is used to retrieve data from a database table. To further refine your query and retrieve more specific results, you can use the WHERE
clause. The WHERE
clause allows you to filter data based on specified conditions.
Syntax
The basic syntax of the SELECT
statement with the WHERE
clause is as follows:
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Here, column1, column2, ...
represents the columns that you want to retrieve from the table, table_name
is the name of the table from which data is retrieved, and condition
is the filter criteria.
Examples
Example 1: Retrieving specific rows from a table
Suppose we have a table named users
with columns id
, name
, age
, and country
. To retrieve all rows where the age
is greater than 30, the SQL query would be:
SELECT * FROM users
WHERE age > 30;
This query will return all rows from the users
table where the age
column has a value greater than 30.
Example 2: Using multiple conditions
In some cases, you may want to use multiple conditions to filter data. For instance, let’s say you want to retrieve all rows where the age
is greater than 30 and the country
is ‘USA’. The SQL query would be:
SELECT * FROM users
WHERE age > 30 AND country = 'USA';
This query will return all rows where the age
is greater than 30 and the country
is ‘USA’.
Example 3: Using wildcard characters
You can also use wildcard characters like %
and _
in the WHERE
clause to perform pattern matching. For instance, to retrieve all rows where the name
starts with ‘J’, the SQL query would be:
SELECT * FROM users
WHERE name LIKE 'J%';
This query will return all rows where the name
column starts with the letter ‘J’.
Conclusion
The WHERE
clause in the SQL SELECT
statement allows you to filter and retrieve specific data from a database table based on the specified conditions. By using the WHERE
clause effectively, you can narrow down the results and extract the data you need for your application.
#SQL #SELECT #WHERE